2

我一直在搜索,但没有找到与多个自定义行相关的任何有用信息,我需要为我的应用程序创建一个设置 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;
}
4

2 回答 2

7

首先,您创建一些自定义 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;
}
于 2012-05-05T18:37:59.153 回答
3

如果您还不熟悉从 xib 加载自定义单元格,请查看此处的文档。要将其扩展到多个自定义 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;

}
于 2012-05-05T18:37:10.150 回答