0

有时,当我在 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;
4

1 回答 1

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..并使用我编辑的答案,它应该可以工作..JoinisSelected

已编辑 2

[_fetchByRecipe objectAtIndexPath:indexPath];

[_fetchByRecipe objectAtIndexPath:indexPath.row];
于 2013-04-22T06:59:09.163 回答