【发布时间】: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;
}
【问题讨论】:
-
@MayYang 将 cellForRowAtIndexPath 方法中的所有代码写入 didSelectRowAtIndexPath... 除了返回语句。
标签: ios objective-c uitableview