ios - 如何在编辑表单时显示预先检查的 UITableViewCells。

标签 ios iphone objective-c uitableview

我有一种允许用户选择某些字段的形式,对于我使用的选定项目 [cell setAccessoryType:UITableViewCellAccessoryCheckmark]; ,我使用的逻辑是,我已经采取emptyMutableArray 并且在 didSelectRowAtIndex 委托(delegate)方法时我检查了所选的 indexPath 是否存在于该 emptyMutableArray 中,如果它存在意味着该项目已被选中然后我从该 emptyMutableArray 中删除该 indexPath,但是如果该 indexPath 不存在于其中,我在那个 emptyMutableArray 中添加了 indexPath。好的,之后我重新加载同一个 tableView,那时在方法 cellForRowAtIndexPath 中我再次有一个 if 语句用于设置单元格 AccessoryType,我检查 indexPath 是否存在于 emptyMutableArray 中,如果它在那里,我将单元格的 AccessoryType 设置为 UITableViewCellAccessoryCheckmark 否则我将它设置为 UITableViewCellAccessoryNone, 之后问题来了,当用户尝试编辑这样的表单时,我想显示已经选择的项目,这就是问题所在,你们都会从我的代码中理解这个问题。

这是 cellForRowAtIndexPath 委托(delegate)方法代码,它决定哪个单元格应该有复选标记

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    cell.selectionStyle=UITableViewCellSelectionStyleNone;

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
 cell.textLabel.font=[UIFont fontWithName:@"verdana" size:13];
 cell.backgroundColor=[UIColor clearColor];
 else if (tableView==roleTableView) {

        cell.textLabel.text = [roleAry objectAtIndex:indexPath.row];

        if([emptyMutableArray containsObject:indexPath]){
            [cell setAccessoryType:UITableViewCellAccessoryCheckmark];

        } else {
            [cell setAccessoryType:UITableViewCellAccessoryNone];
        }
    }
cell.textLabel.textColor = [UIColor darkGrayColor];
    return cell;
}

这是 tableView 的 didSelectRowAtIndexPath 方法,用户可以从中决定应该选择或取消选择哪个项目。

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
 else if (tableView==roleTableView){

        NSLog(@"IndexPath Obj:-%@",indexPath);
        if([emptyMutableArray containsObject:indexPath]){
            [emptyMutableArray removeObject:indexPath];

            [roleMutableAry removeObject:[roleIdAry objectAtIndex:[roleAry indexOfObject:[NSString stringWithFormat:@"%@",[roleAry objectAtIndex:indexPath.row]]]]];
        }
        else {
            [emptyMutableArray addObject:indexPath];

            [roleMutableAry addObject:[roleIdAry objectAtIndex:[roleAry indexOfObject:[NSString stringWithFormat:@"%@",[roleAry objectAtIndex:indexPath.row]]]]];
        }
        NSLog(@"roleMutableAry:%@",roleMutableAry);

        if ([roleMutableAry count]==0) {
            //[roleBtn setTitle:@"Select role" forState:UIControlStateNormal];
        }
        [roleTableView reloadData];
        [self loadInvitees];
        [peoplesOfDiffRoleTableView reloadData];

    }
}

我知道表达确切的问题对我来说是典型的,但是一旦您理解了代码,我将在第二次显示已选择(选中)的项目时解释问题发生的位置。

最佳答案

您不能依赖 IndexPath 来设置单元格的某些属性,因为单元格会被重用。我从你的问题中了解到,你想要跟踪已选择的单元格,然后希望表格 View 相应地显示配件类型。为此,您应该有一个数组,该数组将包含具有“isSelected”属性或任何您想要的属性的对象。此属性将帮助您根据其值设置配件类型。在您的 cellForRowAtIndex 中,您需要从数组中提取相应的对象并检查属性“isSelected”或您定义的任何内容,并相应地设置单元格的附件类型。我希望这能帮到您。干杯!

关于ios - 如何在编辑表单时显示预先检查的 UITableViewCells。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20209481/

相关文章:

ios - 在 Xcode 6.1 中设置导航栏文本字体

iphone - 如何设置日期格式MMM dd,yyyy HH :mm in iPhone

iphone - 检测 UITextView 何时完成滚动

objective-c - 从后台调用应用程序时调用方法

ios - 在 iOS9.1 中什么可以触发 dispatch_async 上的 SIGABRT?

ios - 语言设置事件在 iOS 应用程序中发生更改

ios - 解析 Facebook 登录 Swift 3.0

ios - UIScrollView 不在 UiView 中滚动

iphone - MKMapView 缩放到 viewDidLoad 上的用户位置?

ios - navigationController属性在UINavigationController.h中声明,但它是UIViewController的属性