我一直在遵循这个问题中的步骤: How to add a checkbox to a UITableView on the left, while using UITableViewCell caching?
我不想要一个简单的复选框来检查是否点击了一行,我需要一个仅在点击复选框时才会更改的复选框,因为单元格本身已经加载了所选项目的详细视图。
如何将单元格“移动”到右侧,以便能够正确看到选中的按钮?(解决了这个问题:在 Storyboard 中,选择单元格,转到 Attributes Inspector,然后更改标识的宽度)
我应该为标签使用什么,因为indexPath.row不起作用?如果我选择第一个项目,它会检查第 8 个(在所选答案中解决了这个)
更新:即使我选择了正确的答案,它也只能部分解决我的问题。重新加载视图时,选中的项目将移动到该部分的末尾,而不是保持与之前相同的顺序。有谁知道为什么?
这是 cellForRowAtIndexPath 的更新代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView2 dequeueReusableCellWithIdentifier:@"Cell2"];
UIButton *checkBox;
if (cell.tag == 0) {
checkBox = [[UIButton alloc]initWithFrame:CGRectMake(5, 10, 20, 20)];
UIImage *normalImage = [UIImage imageNamed: @"unchecked.png"];
UIImage *selectedImage = [UIImage imageNamed: @"checked.png"];
[checkBox setImage:normalImage forState:UIControlStateNormal];
[checkBox setImage:selectedImage forState:UIControlStateSelected];
[checkBox addTarget:self action:@selector(makeWatched:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:checkBox];
cell.tag = 1;
}
for (id obj in cell.contentView.subviews) {
if ([obj isKindOfClass:[UIButton class]]) {
checkBox = obj;
checkBox.tag = indexPath.row;
break;
}
}
Episodi *epi = [self.fetchedResultsController objectAtIndexPath:indexPath];
checkBox.selected = ([self.selectedChecks containsObject:[NSNumber numberWithInt: (indexPath.row)]])? YES : NO;
if ([epi.watched isEqualToString:@"1"]) {
NSLog(@"Entra");
checkBox.selected = YES;
}
cell.textLabel.text = epi.episodeTitle;
return cell;
}
更新复选框操作的代码:
-(void)makeWatched:(UIButton *) checkButton {
if (checkButton.selected == NO) {
Episodi *epi = [self.fetchedResultsController objectAtIndexPath:[NSIndexPath indexPathForRow:checkButton.tag inSection:sectionNumber]];
[epi setWatched:@"1"];
checkButton.selected=YES;
NSError *error = nil;
if (![managedObjectContext save:&error]) {
NSLog(@"Saving changes failed: %@", error);
} else {
// The changes have been persisted.
}
[self.selectedChecks addObject:[NSNumber numberWithInt:(checkButton.tag)]];
[self.tableView2 reloadRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:checkButton.tag inSection:sectionNumber]] withRowAnimation:UITableViewRowAnimationNone];
}else{
Episodi *epi = [self.fetchedResultsController objectAtIndexPath:[NSIndexPath indexPathForRow:checkButton.tag inSection:sectionNumber]];
[epi setWatched:@"0"];
if ([epi.watched isEqualToString:@"0"]){
NSLog(@"Not Watched anymore");
}
checkButton.selected=NO;
NSError *error = nil;
if (![managedObjectContext save:&error]) {
NSLog(@"Saving changes failed: %@", error);
} else {
// The changes have been persisted.
}
[self.selectedChecks removeObjectIdenticalTo:[NSNumber numberWithInt:(checkButton.tag)]];
[self.tableView2 reloadRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:checkButton.tag inSection:sectionNumber]] withRowAnimation:UITableViewRowAnimationNone];
}
}