UITableViewCell中选定行的错误

时间:2013-04-22 06:39:19

标签: ios uitableview

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

1 个答案:

答案 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];