设计一个UIView作为单独的xib文件的tableview单元格

问题描述:

我需要设计一个UIView作为单独的xib文件的tableview单元格。 我也尝试过创建一个单独的xib并将其视图匹配到tableview单元格类型。 但它失败了,有任何编程指南来创建自定义,可重用的tableview单元格创建?设计一个UIView作为单独的xib文件的tableview单元格

创建自定义tableview单元格后,我们如何将它添加到表视图?

+0

这应该是有帮助的:[ios步骤来创建与xib文件的自定义UITableViewCell](http:// *。com/questions/4917027/ios-steps-to-create-custom-uitableviewcell -with-xib-file) – Amar

  1. 你需要继承UITableViewCell,那么你已经添加了新的“查看”文件(参见下图) enter image description here

  2. 删除UIView文件中.xibobject library添加Table View Cell

3.设置为你的自定义类

enter image description here

CustomCell *cell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:identifier]; 
if (cell == nil) { 
    // Load the top-level objects from the custom cell XIB. 
    NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil]; 
    // Grab a pointer to the first object (presumably the custom cell, as that's all the XIB should contain). 
    cell = [topLevelObjects objectAtIndex:0]; 
} 
+0

@TheDoctor我遵循用xib @ http://www.weheartswift.com/swifting-around创建customCell。在本教程中,他没有实现'func ... heightForRowAtIndexPath'。但是如果我不使用这个函数,我的单元格就会重叠。我的代码有什么问题? – Emadpres

+0

@EmAdpres:单元格的默认高度是44,如果您的自定义单元格的高度不同,则需要在界面构建器中实现'heightForRowAtIndexPath'或为tableview设置单元格高度。 –

+0

@TheDoctor我在界面构建器中为tableview设置单元格高度,并且它仍然重叠。我想我没有其他的选择比执行'heightForRowAtIndexPath'因为我的列表不是静态的[根据这个答案](http://*.com/questions/8615862/custom-cell-row-height-setting-in-storyboard -is-未响应)。对吗?或者我仍然有机会在界面构建器中设置单元格高度? – Emadpres

cellForRowAtIndexPath委托方法,请执行下列操作:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"customCellIdentifier"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"customCellIdentifier"]; 

    if (cell == nil) { 
     cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 


    UIView *view =[[UIView alloc]init]; 
    view.frame = cell.contentView.frame; 

    view.backgroundColor = [UIColor redColor];//To be sure that the custom view in the cell 
    [cell addSubview:view]; 

    return cell; 
} 
+0

这是否也加载自定义单元格? – Alix

您可以创建一个单独的NIB来存储你的表格单元格。在viewDidLoad中注册您的NIB文件每种细胞类型:

#define kMyCellIdentifier @"kMyCellIdentifier" 

.... 

- (void)viewDidLoad 
{ 
    UINib *tableCellNib = [UINib nibWithNibName:@"MyCell" bundle:nil]; 
    [self.tableView tableCellNib forCellReuseIdentifier:kMyCellIdentifier]; 
} 

然后,当询问您可以创建一个单元格。既然你已经注册的笔尖的iOS将处理为您创建的细胞,如果有尚未一个出队:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kMyCellIdentifier]; 

    // Configure cell... 

    return cell; 
} 

您还可以创建原型单元故事板直接在IB,但FF你有一个通用应用程序,这是一个痛苦,因为你需要维护一个iPad和同一个单元的iPhone版本。

+0

我编辑它,并以某种方式结束了2职位。我已删除该副本。 – tarmes

你可以找到一个演示here并且在共享侧customcell文件夹。希望能你会能够找到所有的东西,并根据您的需要进行修复。

在委托方法cellforrowindexpath方法

添加如下: -

UIView *myView = [UIView alloc]init]; //or initwithframe 
myview.frame = cell.contentView.frame; 
如果要确保再设置研究背景颜色

myview.backgroundColor = [UIColor yellowColor]; //Or any other color. 
[cell addsubview :myview]; 

谢谢

。 如果您有任何疑问,请随时联系。

首先,您必须创建一个文件,配置您的单元类类,然后从UITableViewCell类中接收。创建一个仅具有表格视图单元格的XIB文件。然后将您的自定义单元格类文件导入到视图或视图控制器中,实施以下方法。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
NSString *CellIdentifier = [NSString stringWithFormat:@"Cell%d",indexPath.row]; 
      YOUR_CUSTOM_CELL_CLASS * cell = (YOUR_CUSTOM_CELL_CLASS *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
      return [self createCustomCell:cell cellForRowAtIndexPath:indexPath]; 
} 


-(YOUR_CUSTOM_CELL_CLASS *)createCustomCell:(YOUR_CUSTOM_CELL_CLASS *)cell cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
if (cell == nil) 
    { 
     cell = (YOUR_CUSTOM_CELL_CLASS *)[[[NSBundle mainBundle] loadNibNamed:@"YOUR_CUSTOM_CELL_CLASS_XIB_FILE_NAME" owner:self options:nil] objectAtIndex:0]; 
    } 
    return cell; 
}