【问题标题】:Multiple Custom Rows UITableView?多个自定义行 UITableView?
【发布时间】:2012-05-05 18:22:32
【问题描述】:

我一直在搜索,但没有找到与多个自定义行相关的任何有用信息,我需要为我的应用创建一个设置 tableView,我需要在其中加载来自 xib 文件的行,例如:

第 1 行 =>> XIB 1.
第 2 行 =>> XIB 2.
第 3 行 =>> XIB 3.
第 4 行 =>> XIB 4.

我现在的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell=nil;
    //We use CellType1 xib for certain rows
    if(indexPath.row==0){
        static NSString *CellIdentifier = @"ACell";
        cell =(ACell*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if(cell==nil){
            NSArray *nib= [[NSBundle mainBundle] loadNibNamed:@"ACell" owner:self options:nil];
            cell = (ACell *)[nib objectAtIndex:0];
        }
        //Custom cell with whatever
        //[cell.customLabelA setText:@"myText"]
    }
    //We use CellType2 xib for other rows
    else{
        static NSString *CellIdentifier = @"BCell";
        cell =(BCell*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if(cell==nil){
            NSArray *nib= [[NSBundle mainBundle] loadNibNamed:@"BCell" owner:self options:nil];
            cell = (BCell *)[nib objectAtIndex:0];
        }
        //Custom cell with whatever
        //[cell.customLabelB setText:@"myText"]
    }

    return cell;
}

【问题讨论】:

    标签: iphone ios uitableview custom-cell


    【解决方案1】:

    首先,您创建一些自定义 UITableViewCell 类(.h 和 .m),与您拥有的 xib 文件一样多:
    所以你可以有 CellType1 和 CellType2 例如。
    CellType1.h 看起来像

    #import <UIKit/UIKit.h>
    @interface CellType1 : UITableViewCell
    
    @property(nonatomic,strong) IBOutlet UILabel *customLabel;
    
    @end
    

    然后您创建 xib 文件,您可以使用默认视图类型,但是,只需删除自动创建的视图,将其替换为 UITableViewCell,并将类更改为 CellType1。对 CellType2 执行相同操作。

    然后在你的 tableViewController 中,像这样写 cellForRow:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell=nil;
    //We use CellType1 xib for certain rows
    if(indexPath.row==<whatever you want>){
         static NSString *CellIdentifier = @"CellType1";
         cell =(CellType1*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
         if(cell==nil){
            NSArray *nib= [[NSBundle mainBundle] loadNibNamed:@"CellType1" owner:self options:nil];
            cell = (CellType1 *)[nib objectAtIndex:0];
          }
          //Custom cell with whatever
          [cell.customLabel setText:@"myText"]
    }
    //We use CellType2 xib for other rows
    else{
        static NSString *CellIdentifier = @"CellType2";
        cell =(CellType2*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
         if(cell==nil){
            NSArray *nib= [[NSBundle mainBundle] loadNibNamed:@"CellType2" owner:self options:nil];
            cell = (CellType2 *)[nib objectAtIndex:0];
          }
          //Custom cell with whatever
          [cell.customLabel setText:@"myText"]
    }
    
    return cell;
    }
    

    【讨论】:

    • ”是什么意思
    • 你说你想使用多个 Xib,我猜 xib1 用于第 n1 行,xib2 用于第 n2 行,等等......所以 if/else 语句检查行索引,并基于什么你想实现你在正确的行索引中选择正确的xib
    • 我应该为 UITableViewCells 更改我的 XIB 的主视图还是只调整视图的大小?
    • 只需删除该视图并添加一个 UITableViewCell,然后将类型从 UITableViewCell 更改为 WhatCustomCell
    • 您的代码仍然不适合我!您能帮我多一点吗,我真的需要这个。我会更新我的问题并添加我现在正在尝试的代码!
    【解决方案2】:

    如果您还不熟悉从 xib 加载自定义单元格,请查看 documentation here。要将其扩展到多个自定义 xib,您可以在单独的 xib 中创建每个表格单元格,给它一个唯一的单元格标识符,将表格视图控制器设置为文件的所有者,并将每个单元格连接到您定义的自定义单元格出口(在docs,他们使用tvCell 作为这个出口)。然后在您的-tableView:cellForRowAtIndexPath: 方法中,您可以通过检查您为哪一行提供单元格来加载正确的xib(或出列正确的可重用单元格)。例如:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        static NSString *MyIdentifier1 = @"MyIdentifier1";
        static NSString *MyIdentifier2 = @"MyIdentifier2";
        static NSString *MyIdentifier3 = @"MyIdentifier3";
        static NSString *MyIdentifier4 = @"MyIdentifier4";
    
        NSUInteger row = indexPath.row
    
        UITableViewCell *cell = nil;
    
        if (row == 0) {
            cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifer1];
            if (nil == cell) {
                [[NSBundle mainBundle] loadNibNamed:@"MyTableCell1" owner:self options:nil];
                cell = self.tvCell;
                self.tvCell = nil;
            }
        }
        else if (row == 1) {
            cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifer2];
            if (nil == cell) {
                [[NSBundle mainBundle] loadNibNamed:@"MyTableCell2" owner:self options:nil];
                cell = self.tvCell;
                self.tvCell = nil;
            }
        }
        else if (row == 2) {
            cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifer3];
            if (nil == cell) {
                [[NSBundle mainBundle] loadNibNamed:@"MyTableCell3" owner:self options:nil];
                cell = self.tvCell;
                self.tvCell = nil;
            }
        }
        else if (row == 4) {
            cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifer4];
            if (nil == cell) {
                [[NSBundle mainBundle] loadNibNamed:@"MyTableCell4" owner:self options:nil];
                cell = self.tvCell;
                self.tvCell = nil;
            }
        }
        // etc.
    
        // Do any other custom set up for your cell
    
        return cell;
    
    }
    

    【讨论】:

    • 这个方法对我不起作用。我得到一个 'UITableView 数据源必须从 tableView 返回一个单元格:cellForRowAtIndexPath:' 的想法?我将 ViewController 链接到每个单元格,并为每个单元格创建并连接到单元格的自定义 IBOutlet。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-27
    相关资源
    最近更新 更多