iphone - 获取 UITableView 中选中单元格中的值

标签 iphone ios uitableview

我有一个 UITableView每个单元格都有一个 UITableViewCellAccessoryCheckmark .用户可以根据自己的喜好选中和取消选中单元格。用户可以在表格 View 中检查/选择多个单元格。可以根据用户偏好取消选中和重新选中选中/选中的单元格。
我在 UITableViewController 中有一个完成按钮.单击它时,我需要返回上一个 View ,在此之前我必须在选中的单元格中收集文本(仅选中的单元格)。
我怎样才能做到这一点。

我正计划通过保留 NSMutableArray 来开发逻辑。并在 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 上更新它, 当一个单元格被选中/选中时。但是,当每次未选中单元格时,我都必须从数组中删除该项目,如果再次选中该单元格,则必须再次添加它。我认为这不是正确的方法。这样做的正确方法是什么。
我在 Stackoverflow 中找不到类似的问题,这很不寻常。如果有人可以发布链接,如果之前有人问过这个问题,那将会很有帮助。

最佳答案

维护两个数组,如下所示:

@property (nonatomic, retain) NSMutableArray *features, *selectedFeature;

.m 中合成它们文件。在 viewDidLoad 中初始化你的两个数组:
self.features = [NSArray arrayWithMyOwnResourceLikeDownloadedFromServerOrWhatever];
self.selectedFeature = [NSMutableArray array];

然后在 didSelectRowAtIndex 中执行类似的操作:
NSString * stirng = [features objectAtIndex:indexPath.row];
if ([self.selectedFeature containsObject:stirng]) {
    [self.selectedFeature removeObject:stirng];
}
else{
   [self.selectedFeature addObject:stirng];
}

在你的cellForRowAtIndexPath :
NSString * stirng = [features objectAtIndex:indexPath.row];
[cell.textLabel setText:stirng];
if ([self.selectedFeature containsObject:stirng]) {
   //it is selected feature
   [cell setAccessoryType:UITableViewCellAccessoryCheckmark];
}
else{
   //it is un-selected feature
   [cell setAccessoryType:UITableViewCellAccessoryNone];
}

关于iphone - 获取 UITableView 中选中单元格中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15859698/

相关文章:

iOS View Controller 底部的空白区域

iphone - cocoa - 我发现了 NSDecimalNumber 的一个错误

ios - 完成模态视图后重新加载 UICollectionView

ios - 如何检查 iPhone 之前是否安装过我的应用程序?

objective-c - 如何去除分组的 UITableView 顶部的空白区域?

ios - 使用 Swift 将按钮添加到静态 TableView 标题

启用 SSL 的 Iphone libcurl ..?

iphone - 使用协议(protocol)进行跨类通信

ios - 无法使用 iOS Estimote Indoor SDK 手动创建位置

ios - 异步查询后 UITableViewController 消失