我目前正在为 iOS 平台编写一款游戏。
在开始新游戏 View 中,我在 UINavigationController
中嵌入了 UITableView
。
表格 View 中的每个单元格都有玩家好友列表中某个人的姓名,我希望玩家能够选择一组好友来邀请玩新游戏。
我的问题是:如何使 UITableView
中的行可选择,然后将选定的行添加到 NSArray
中?
提前致谢!
最佳答案
UITableViewController
的 -didSelectRowAtIndexPath:
是使行可选择的委托(delegate)方法。
根据您现在的要求,这就是您所需要的。
您可以在此委托(delegate)中连接逻辑,以对用户单击的行执行操作。
简单示例:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSLog(@"%@",cell.textLabel.text];
//declare "NSMutableArray *myMutableArray;" in the .h of this class
//now you can add objects to it with the following:
[myMutableArray addObject:cell.textLabel.text];
//note: this is not perfect. it will add the cell's text everytime you
//click any row, which means... duplicate entries.
}
第一行获取有关特定单元格的信息。
第二行是一个简单的演示,打印该单元格中的文本。
根据您的逻辑,您可以做更多的事情。
关于ios - 使 UITableView 行可选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20168547/