好的,这似乎解决了我的问题。如果有人知道更优雅的解决方案,请告诉我。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if ([self.tableView cellForRowAtIndexPath:indexPath].accessoryType == UITableViewCellAccessoryCheckmark) {
[self.tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryNone;
[self.tableView cellForRowAtIndexPath:indexPath].textLabel.textColor = [UIColor colorWithRed:0.0 / 255 green:0.0 / 255 blue:0.0 / 255 alpha:1];
[selectedSources removeObject:[NSNumber numberWithInt:indexPath.row]];
} else {
[self.tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark;
[self.tableView cellForRowAtIndexPath:indexPath].textLabel.textColor = [UIColor colorWithRed:72.0 / 255 green:104.0 / 255 blue:152.0 / 255 alpha:1];
[selectedSources addObject:[NSNumber numberWithInt:indexPath.row]];
}
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
}
selectedSources
是一个 NSMutableArray,然后我对其进行 JSON 编码并发送到我的服务器。根据索引路径,我可以识别哪些表格视图单元格已被选中/未选中服务器端。
谢谢,安娜!