在TableView中隐藏自定义单元格

时间:2017-08-01 04:47:49

标签: ios objective-c

我有以下实现,我将footerview添加为customcell。但是,如果没有内容,我不想显示最后一个单元格,如果有,我想显示它。

在我当前的实现中,它始终显示最后一个单元格。

fines

3 个答案:

答案 0 :(得分:2)

有一个选项可以让cellHeight为0。

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
  if(CHECK_IF_NO_CONTAIN)
    return 0;

return 40;// Normal height as you want

}

答案 1 :(得分:1)

我认为您可以在else子句中添加条件,如果满足某些条件,那么您希望以其他方式显示此单元格...

if (indexPath.row < adminOrderElements[indexPath.section].products.count)
    {
        static NSString *CellIdentifier = @"Cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
        }
        NSArray * tempArray = adminOrderElements[indexPath.section].products;
        cell.textLabel.text = [[tempArray objectAtIndex:indexPath.row] objectForKey:@"productname"];
        return cell;
    }
    else //Here add your condition here if you have content for the section like I think this condition might work [adminOrderElements[indexPath.section].notes length]>0
    {
        static NSString *CellIdentifier = @"FooterCell";
        FooterTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil)
            cell = [[FooterTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

        if(adminOrderElements[indexPath.section].expanded && [adminOrderElements[indexPath.section].notes length]>0)
        {
            cell.footerLabel.text = [NSString  stringWithFormat:@"Notes: %@", adminOrderElements[indexPath.section].notes];
        }
        else
        {
            cell.heightConstraints.constant = 1;
            cell.footerLabel.text = @"";
        }
        return cell;
    }
}

答案 2 :(得分:0)

使用UITableView委托

非常简单
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (dataObjects.count == 0){
        return 0;
    }
    return dataObjects.count;
}
相关问题