【问题标题】:Custom tableview with different cell具有不同单元格的自定义表格视图
【发布时间】:2013-11-05 11:55:33
【问题描述】:

我是 iOS 新手。我有一个表格视图,我想在其中加载不同的自定义单元格。

这是我来自tableViewCellForRowAtIndexPath的代码:

if (self.mainTableView .tag==SEARCH)
    {
        static NSString *cellId=@"Cella";
        CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:cellId];
        if (cell == nil) 
        {
            NSArray *topLevelOjects= [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
            cell=[topLevelOjects objectAtIndex:0];
        }
    //code goes here
    return cell;
}
else
{
    static NSString *mCell = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:mCell];
    if(cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
    }
    //code goes here
    return cell;
}

在方法 numberOfRowsInSections:(UITableView *) tableview 我有:

    if (tableView.tag==SEARCH)
    {
        return [firstList count];
    }
    else
    {
        return [secondList count];
    }

我遇到的问题是每次执行 ELSE 时,tableview 都包含第一个和第二个 CustomCell。为什么?

【问题讨论】:

  • 这段代码是在哪里实现的?在tableView:cellForRowAtIndexPath:?你在tableView:numberOfRowsInSection:返回什么?
  • 我建议把if(self.mainTableView.tag==SEARCH)改成if(tableView.tag == SEARCH)。那你什么时候放标签呢?
  • 如果单元格的高度不同,您还必须实现heightForRowAtIndexPath:,否则它们会重叠。
  • 我已经更新了我的问题。谢谢
  • 单元格的大小确实不同

标签: ios tableview custom-cell


【解决方案1】:

我终于找到了问题。 else 分支上使用的标识符实际上是 CustomCell *cell =.. (如果分支)使用的标识符。没有 Cella 标识符:)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-29
    • 1970-01-01
    • 2017-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多