【问题标题】:multiple cell identifier in tableview in iOSiOS中tableview中的多个单元格标识符
【发布时间】:2016-04-18 01:40:26
【问题描述】:

我正在使用一个包含来自两个字典的对象的数组,并在具有不同标识符的第一个单元格上显示第一个字典的对象,并与第二个字典对象相同。

在 cellforrowatindexpath 中查看我的代码

static NSString *cellIdentifier = @"cellIdentifier1";
    static NSString *customCellIdentifier = @"cellIdentifiercustom";

    if (indexPath.row == 0) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        //        if (cell==nil)
        {
            cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];

        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        ExcName = [[UILabel alloc] initWithFrame:CGRectMake(10.0, 2.0, 250.0, 36.0)];
        ExcName.font = [UIFont systemFontOfSize:12.0];
        ExcName.textColor = [UIColor blackColor];
        ExcName.textAlignment = NSTextAlignmentLeft;
        ExcName.text=[[exerciseAr objectAtIndex:indexPath.row] objectForKey:@"activityname"];
        ExcName.numberOfLines=3;
        [cell.contentView addSubview:ExcName];

        return  cell;
    }
    else{
        UITableViewCell *customcell = [tableView dequeueReusableCellWithIdentifier:customCellIdentifier];

        //        if (cell==nil)
        {
            customcell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:customCellIdentifier];

        }
        customcell.selectionStyle = UITableViewCellSelectionStyleNone;
        ExcName = [[UILabel alloc] initWithFrame:CGRectMake(10.0, 2.0, 250.0, 36.0)];
        ExcName.font = [UIFont systemFontOfSize:12.0];
        ExcName.textColor = [UIColor blackColor];
        ExcName.textAlignment = NSTextAlignmentLeft;
        ExcName.text=[[exerciseAr objectAtIndex:indexPath.row] objectForKey:@"exercisename"];
        //    ExcName.text=[[exerciseAr objectAtIndex:indexPath.row] objectForKey:@"exercisename"];

        ExcName.numberOfLines=3;
        [customcell.contentView addSubview:ExcName];

                return  customcell;
    }

    return nil;
}

现在我想如果任何字典为空,对应于该字典的单元格不应该是可见的,现在是可见的。

【问题讨论】:

  • 如果在 heightforrowatindexpath 委托方法中特定字典为空,您可以将高度更改为 0
  • 那么,你应该把这个逻辑放在-tableView:numberOfRowsInSection:
  • @Jaimish 不建议这样做(运行时会抱怨)。不要折叠单元格,而是告诉表格视图您需要显示的实际行数,并在每次调用 -tableView:cellForRowAtIndexPath:` 时返回一个完全配置的单元格。
  • @NicolasMiari 你能用编码解释一下吗
  • 好的,然后@NavjotSingh 请告诉我你的 numberOfRowinSection 方法

标签: ios objective-c uitableview dictionary


【解决方案1】:

这实际上取决于您的字典为空是什么意思。它可能是 nil,或者它只是没有任何键值,例如键的值是一个空字符串。因此,为了补充 NibolasMiari 的答案,我想说,检查这两种情况。

- (void) filterDictionaries:(NSArray*) exerciseAr{
    filteredArray = [[NSMutableArray alloc]init];

    for (NSDictionary* myDict in exerciseAr) {
        if ([myDict isKindOfClass:[NSNull class]]) {
           continue;
        }
        else if ([myDict count] == 0) {
            continue;
        }
        else{
            [filteredArray addObject: myDict];
        }
    }   
}

- (NSInteger) tableView:(UITableView*) tableView numberOfRowsInSection:(NSInteger) section
{
    return [filteredArray count];
}

现在在您的 cellforIndexPath 方法中,一切都将相同,只需更改以下行-

在 if 块中,你有

ExcName.text=[[exerciseAr objectAtIndex:indexPath.row] objectForKey:@"activityname"];

制作它-

ExcName.text=[[filteredArray objectAtIndex:indexPath.row] objectForKey:@"activityname"];

在 else 块中,你有

ExcName.text=[[exerciseAr objectAtIndex:indexPath.row] objectForKey:@"exercisename"];

制作它-

ExcName.text=[[filteredArray objectAtIndex:indexPath.row] objectForKey:@"exercisename"];

【讨论】:

    【解决方案2】:

    一些事情:

    1. 不要从-tableView:cellForRowAtIndexPath: 返回nil。对于每个(可见)行,该方法只调用一次,并且该表假定它具有您通过-tableView:numberOfRowsInSection: 的返回值告知的行数。默认为nil 表明您的代码存在不一致。

    2. 因此,单元出队永远不会失败。我建议您切换到较新的方法:-dequeueReusableCellWithIdentifier:forRowAtIndexPath:。当重用池中没有单元时,它会为您分配新单元。

    3. 细胞正在被重复使用。一个单元格可能会重复使用多次:如果每次添加一个新标签,它们就会堆积起来。相反,在界面构建器中将标签添加到您的单元格,并设置一个出口以在配置单元格时引用它们。


    解决方案:

    不要“跳过行”,而是让您的数据模型与您要显示的内容保持一致。如果您有一个包含要跳过的对象的数组,请先过滤您的数组,然后将过滤后的数组用作数据源。

    // Call this method once before reloading the table view
    - (void) filterDictionaries
    {
        // (filteredArray is an ivar defined as NSMutableArray*)
    
        for (NSDictionary* dictionary in exerciseAr) {
        
            if ([dictionary  objectForKey:@"exercisename"] != nil) {
                [filteredArray addObject: dictionary];
            }
        }   
        // Now you can use filteredArray as the data source 
        // instead of exerciseAr, without worrying about skipping 
        // entries.
    }
    
    - (NSInteger) tableView:(UITableView*) tableView numberOfRowsInSection:(NSInteger) section
    {
        return [filteredArray count];
    }
     
    - (UITableViewCell*) tableView:(UITableView*) tableView cellForRowAtIndexPath:(NSIndexPath*) indexPath
    {
        static NSString *cellIdentifier = @"cellIdentifier1";
        static NSString *customCellIdentifier = @"cellIdentifiercustom";
    
        UITableViewCell* cell;
    
        if (indexPath.row == 0) {
            cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forRowAtIndexPath:indexPath];
        }
        else{
            cell = [tableView dequeueReusableCellWithIdentifier:customCellIdentifier forRowAtIndexPath:indexPath];
        }
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
          
        cell.excLabel.text =  [[filteredArray objectAtIndex:indexPath.row]
        
        return  cell;
    }
    

    【讨论】:

    • 请先明白我在问什么
    • 你的意思是这部分:“现在我想要如果任何字典为空,对应于该字典的单元格不应该是可见的,现在是可见的。”
    • 是的,那么我如何过滤它来制作过滤数组
    • 查看我的更新答案。我在代码中明确说明了仅在文本中提及的内容。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多