ios - 使 UITableView 行可选择

标签 ios objective-c uitableview uinavigationcontroller

我目前正在为 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.
}

第一行获取有关特定单元格的信息。
第二行是一个简单的演示,打印该单元格中的文本。
根据您的逻辑,您可以做更多的事情。

引用:https://developer.apple.com/library/ios/documentation/uikit/reference/UITableViewDelegate_Protocol/Reference/Reference.html

关于ios - 使 UITableView 行可选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20168547/

相关文章:

iOS 应用组 - 共享 CoreData 无法打开

ios - 获取与不同 UIStoryboards 一起使用的自定义 UITableViewCell 的高度

objective-c - iOS - iPhone 5 无法滚动到 TableView 的底部

ios - 不寻常的 CoreMotion Sensorfusion 数据

ios - 二元运算符 '+' 不能应用于类型 'String' 和“字符串感叹号”的操作数

ios - 接收gestureRecognizer :shouldRecognizeSimultaneouslyWithGestureRecognizer when gestureRecognizerShouldBegin: returned NO

ios - Objective C-如何在后退按钮上显示viewController的简称

iphone - UICollectionView 节中的单元格数

ios - 滑动删除动画杀死线程

ios - UITableView插入行无效更新