自定义表格查看某个位置的单元格

Posted

技术标签:

【中文标题】自定义表格查看某个位置的单元格【英文标题】:Custom table View Cell in some Location 【发布时间】:2013-01-30 15:50:59 【问题描述】:

我正在构建一个自定义分组 TableView 并且需要在 indexPath.row=0 显示一个自定义 TableViewCell 当 section 为 0 时。我能够显示它但是一旦我进入 tableView 的底部所有单元格都会改变

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 


    NSArray *listData =[self.tableContents objectForKey:[self.sortedKeys objectAtIndex:[indexPath section]]];

    static NSString *simpleTableIdentifier = @"Cell";
    if (indexPath.row==0 && indexPath.section==0)
    
        RightLabelCell *cell = (RightLabelCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
        if (cell == nil)
        
             NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"RightLabelSetting" owner:self options:nil];
             cell = [nib objectAtIndex:0];
        

        cell.leftLabel.text = [listData objectAtIndex:indexPath.row];
        cell.rightLabel.text=@"RightLabel";

        return cell;
    
    else
    
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

        if (cell == nil)
        
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];

        
        [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
        cell.textLabel.text = [listData objectAtIndex:indexPath.row];

        return cell;
    

并且还会在cell.leftLabel.text = [listData objectAtIndex:indexPath.row]; 崩溃

【问题讨论】:

【参考方案1】:

转到Interface builder 并选择自定义单元格和其他单元格,并为它们提供不同的标识符单元格名称

 static NSString *simpleTableCellIdentifier = @"Cell"; 

static NSString *customTableCellIdentifier = @"CustomCell";

并适当地(有条件地)选择这些

【讨论】:

以上是关于自定义表格查看某个位置的单元格的主要内容,如果未能解决你的问题,请参考以下文章

Grid布局(四)单元格自定义布局

从 Swift 中的自定义数据单元开始

UITableview中的按钮修改当前单元格自定义

请问怎么在excel单元格格式添加自定义

如何用Apache POI读取Excel的单元格自定义名称的值

当在顶部添加新帖子时,UICollectionView 单元格自定义 FlowLayout 中断