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;

編集済み 1BOOLプロパティをクラスJoinに.. として 追加するだけisSelectedで、編集した回答を使用できます。動作するはずです..

編集済み 2

[_fetchByRecipe objectAtIndexPath:indexPath];

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