【问题标题】:Can't uncheck checkmark set in cellForRowAtIndexPath无法取消选中 cellForRowAtIndexPath 中设置的复选标记
【发布时间】:2015-05-13 05:10:34
【问题描述】:

我有一个视图控制器,您可以在其中更改性别。我有一个 tableview 加载了一个只有两个字符串,男性或女性的数组。

如果用户最初设置为女性,则女性单元格上有一个复选标记。然后当你点击男性单元格时,男性单元格应该被选中,而女性单元格应该被取消选中。

我的逻辑几乎可以正常工作,但是当我第一次点击未选中的单元格(男性单元格)时,选中的单元格并没有取消选中(因此男性和女性都被选中)。但是之后我再次选择了一个单元格,它可以正常工作。

如何修复此错误...仅在我第一次点击未选中的单元格时发生,然后正常工作。

我浏览了很多 SO 链接,但仍然得到相同的结果。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"genderCell"];
    cell.textLabel.text = self.genderArray[indexPath.row];

    if (([UserParseHelper currentUser].gender = @"Female"))
    {
        if (indexPath.row == 1)
        {
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
        }
    }
    else if (([UserParseHelper currentUser].gender = @"Male"))
    {
        if (indexPath.row == 0)
        {
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
        }
    }

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark;
}

-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryNone;
}

【问题讨论】:

标签: ios objective-c uitableview


【解决方案1】:

一切都一样,只需在 didSelectRowAtIndexPath 方法中更改当前用户的性别。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row==0)
        [UserParseHelper currentUser].gender = @"Male";
    else if(indexPath.row==1)
        [UserParseHelper currentUser].gender = @"Female";
    [tableView reloadData];
}

不需要didDeselectRowAtIndexPath方法

/*-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryNone;
}*/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-13
    • 2021-02-12
    • 1970-01-01
    • 2015-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多