有时当我在UITableView中选择1行时,另一行也会变为选定状态。哪个可能是虫子?
在 didSelectRowAtIndexPath :
中cartCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (cell.accessoryType == UITableViewCellAccessoryNone) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
cell.crossImage.hidden = NO;
}
else {
cell.accessoryType = UITableViewCellAccessoryNone;
cell.crossImage.hidden = YES;
}
在我的 cellForRowAtIndexPath
中static NSString *CellIdentifier = @"cellCart";
cartCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[cartCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
Join *join = [_fetchByRecipe objectAtIndexPath:indexPath];
cell.ingredientName.text = join.ingredient.name;
cell.ingredientCount.text = [NSString stringWithFormat:@"%@", join.count];
cell.ingredientUnit.text = join.ingredient.units;
if (cell.accessoryType == UITableViewCellAccessoryNone) {
cell.crossImage.hidden = YES;
}
else {
cell.crossImage.hidden = NO;
}
return cell;
答案 0 :(得分:0)
您可以尝试下面的
在 didSelectRowAtIndexPath :
中Join *join = [_fetchByRecipe objectAtIndexPath:indexPath.row];
join.isSelected = !join.isSelected;
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
在我的 cellForRowAtIndexPath
中static NSString *CellIdentifier = @"cellCart";
cartCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[cartCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
Join *join = [_fetchByRecipe objectAtIndexPath:indexPath.row];
cell.ingredientName.text = join.ingredient.name;
cell.ingredientCount.text = [NSString stringWithFormat:@"%@", join.count];
cell.ingredientUnit.text = join.ingredient.units;
cell.accessoryType = join.isSelected?UITableViewCellAccessoryNone:UITableViewCellAccessoryCheckmark;
return cell;
已编辑1
只需将BOOL
属性作为Join
添加到您的课程isSelected
..并使用我编辑的答案,它应该有效..
已编辑2
[_fetchByRecipe objectAtIndexPath:indexPath];
到
[_fetchByRecipe objectAtIndexPath:indexPath.row];