多个自定义行UITableView?

我一直在寻找很多,但没有发现任何有用的多个自定义行有关,我需要为我的应用程序创build一个设置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; } 

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

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

然后你创buildxib文件,你可以使用默认的视图types,但是只需要移除自动创build的视图,用UITableViewCellreplace它,然后将类更改为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加载自定义单元格,请查看此处的文档 。 为了将其扩展到多个定制的xib,可以在一个单独的xib中创build每个表格单元格,给它一个唯一的单元格标识符,将表格视图控制器设置为文件的所有者,并将每个单元格连接到您定义的定制单元格插口文档,他们使用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; }