【问题标题】:UITableView with two custom cells (multiple identifiers)UITableView 有两个自定义单元格(多个标识符)
【发布时间】:2012-12-27 13:12:31
【问题描述】:

我正在尝试将一个单元格作为每个单元格之间的空格 - 通过设置 alpha = 0 将隐藏该空格。在我的表格中,空格单元格将用于奇数行。

注意,实际单元格高度为85,而隐藏单元格高度(即单元格之间的间距)为20。

问题是空间单元格高度是85,而不是20,不知道为什么。可能单元格没有正确加载。

Cell 这里是UITableViewCell - 实际的单元格 - 标识符为“Cell”。

Cell2 是标识符为“Space”的空间。

上面的每个类都有自己的UITableViewCell 类,XIB 文件也被分配给每个类。 标识符也在IB中为每个Xib设置。

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

Cell *cell = (Cell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier1];

if(!cell)
{
    NSArray *ar = [[NSBundle mainBundle] loadNibNamed:@"CellView" owner:nil options:nil];
    for (id obj in ar)
    {
        if ([obj isKindOfClass:[Cell class]])
        {
            cell = (Cell *)obj;
            break;
        }
    }
}

if (indexPath.row % 2 == 1)
{
    Cell2 *cell2 = (Cell2 *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier2];

    if (!cell2)
    {
        NSArray *ar = [[NSBundle mainBundle] loadNibNamed:@"Cell2" owner:nil options:nil];
        for(id obj in ar)
        {
            if([obj isKindOfClass:[Cell2 class]])
            {
                cell2 = (Cell2 *)obj;
                break;
            }
        }

        // Method 1
        cell2 = [[Cell2 alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier2];
         // Method 2
        //cell2 = [[Cell2 alloc] init];
        // Method 3
        //cell2 = (Cell2 *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier2];

        [cell2.contentView setAlpha:0];
        // prevent selection and other stuff
        [cell2 setUserInteractionEnabled:NO];
    }
    return cell2;
}
else
{
    // Configure the actual cell
}


return cell;

}

【问题讨论】:

  • 如果您的目标只是增加间距,您可以将每一行设为一个部分。见this answer

标签: ios objective-c uitableview


【解决方案1】:

* 为了更好地理解,我重命名了您的一些 NIB/类名称。 *

首先,您应该注册每个单元格的 NIB:

- (void)viewDidLoad{
    [super viewDidLoad];

    static NSString *CellIdentifier1 = @"ContentCell";
    static NSString *CellIdentifier2 = @"SpaceCell";

    UINib *nib = [UINib nibWithNibName:@"CellViewNIBName" bundle:nil];
    [self.tableView registerNib:nib forCellReuseIdentifier:CellIdentifier1];

    nib = [UINib nibWithNibName:@"CellSpaceNIBName" bundle:nil];
    [self.tableView registerNib:nib forCellReuseIdentifier:CellIdentifier2];

    self.contentView.hidden = YES;
    [self loadData];
}

因为您已经注册了 NIB,dequeueReusableCellWithIdentifier: 将始终返回一个单元格:

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath      *)indexPath
{
    static NSString *CellIdentifier1 = @"ContentCell";
    static NSString *CellIdentifier2 = @"SpaceCell";

    // Space Cell
    if (indexPath.row % 2 == 1) {
        CellSpace *cell = (CellSpace *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier2];
        return cell;
    }

    // Content cell
    else {
        CellView *cell = (CellView *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
        // Configure cell
        return cell;
    }
}

最后,确保实现以下委托方法:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Space cell's height
    if (indexPath.row % 2 == 1) {
        return 20.0f;
    }

    // Content cell's height
    else {
        return 80.0f;
    }
}

【讨论】:

  • 我已经尝试过您并更改了单元格 id 和 nib 名称以匹配我的代码中的名称,但我收到此错误:'*** 由于未捕获的异常 'NSInternalInconsistencyException' 而终止应用程序,原因: '为标识符(单元格)注册的无效笔尖 - 笔尖必须包含一个必须是 UITableViewCell 实例的顶级对象'***'
  • 自定义 UITableViewCell 的 NIB 文件应该只包含一个顶级视图。
  • 感谢您的回复,但是我不知道“一个顶级视图”是什么意思?
  • 这意味着你应该为每个自定义 UITableViewCell 有一个(单独的)XIB。
  • 哦,我已经这样做了,,,CellSpace有自己的XIB也有CellView
【解决方案2】:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *returncell;

    static NSString *cellIdentifier ;
    if(indexPath.section == 0)
    {
        cellIdentifier = @"cell1";
    }
    else if (indexPath.section == 1)
    {
        cellIdentifier = @"cell2";
    }
    UITableViewCell *myCell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    MapTableViewCell *myCustomCell = (MapTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if(indexPath.section == 0)
    {
        if(myCell == nil)
        {
            myCell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
            getLocationBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            getLocationBtn.frame = CGRectMake(myCell.frame.origin.x,myCell.frame.origin.y+5 , 200, 30);
            [getLocationBtn setTitle:@"your button title" forState:UIControlStateNormal];
            [getLocationBtn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
            [getLocationBtn addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
    
        }
    
        [myCell.contentView addSubview:getLocationBtn];
        returncell = myCell;
    }
    else if (indexPath.section == 1)
    {
        if (myCustomCell == nil)
        {
            myCustomCell = [[MapTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        }
    
        myCustomCell.nearbyLocation.text = @"demo Text";
        returncell = myCustomCell;
    }

    return returncell;
}

//我的自定义表格视图单元格

导入“MapTableViewCell.h”

@implementation MapTableViewCell

@synthesize nearbyLocation;

-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self)
    {    
        self.backgroundColor = [UIColor groupTableViewBackgroundColor];
        nearbyLocation = [[UILabel alloc]initWithFrame:CGRectMake(10, 5, 200, 30)];
    
        [self addSubview:nearbyLocation];
    }

    return self;
}
@end

使用默认单元格的自定义单元格数量的最佳方式

【讨论】:

    【解决方案3】:

    除了提供的答案,我想强调每个不同的自定义单元格的单元格标识符也必须不同

    例如,带有标识符 "Cell" 的自定义 cellA 和带有标识符 "Cell2" 的自定义 cellB

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-05
      • 2015-08-26
      • 1970-01-01
      • 2018-12-19
      相关资源
      最近更新 更多