【发布时间】:2013-10-16 10:23:39
【问题描述】:
我需要设计一个 UIView 作为带有单独 xib 文件的 tableview 单元格。 我还尝试创建一个单独的 xib 并将其视图匹配设计为 tableview 单元格类型。 但是失败了,有没有什么编程指南可以创建自定义的、可重用的、tableview cell 创建?
创建自定义表格视图单元格后,我们如何将其添加到表格视图中?
【问题讨论】:
我需要设计一个 UIView 作为带有单独 xib 文件的 tableview 单元格。 我还尝试创建一个单独的 xib 并将其视图匹配设计为 tableview 单元格类型。 但是失败了,有没有什么编程指南可以创建自定义的、可重用的、tableview cell 创建?
创建自定义表格视图单元格后,我们如何将其添加到表格视图中?
【问题讨论】:
首先你必须创建一个文件,你的单元类类的配置应该从 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;
}
【讨论】:
在委托方法的cellforrowindexpath方法中添加如下:-
UIView *myView = [UIView alloc]init]; //or initwithframe
myview.frame = cell.contentView.frame;
如果要确定则设置背景颜色
myview.backgroundColor = [UIColor yellowColor]; //Or any other color.
[cell addsubview :myview];
谢谢。 如有任何疑问,请随时联系。
【讨论】:
您可以找到一个演示 here,并且在共享侧有一个 customcell 文件夹,希望您能够找到所有的东西并根据您的需要进行修复。
【讨论】:
你需要继承UITableViewCell 然后你必须添加一个新的“视图”文件(参考下图)
删除.xib中的UIView文件并从object library添加Table View Cell。
3.为您的单元格设置自定义类
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];
}
【讨论】:
func ... heightForRowAtIndexPath。但是如果我不使用这个功能,我的单元格就会重叠。我的代码有什么问题?
heightForRowAtIndexPath 或在界面生成器中为表格视图设置单元格高度。
heightForRowAtIndexPath 之外别无选择,因为我的列表不是静态的according to this answer。我是对的还是我还有机会在界面生成器中设置单元格高度?
您可以创建一个单独的 NIB 来存储您的表格单元格。在 viewDidLoad 中为每种单元格类型注册您的 Nib 文件:
#define kMyCellIdentifier @"kMyCellIdentifier"
....
- (void)viewDidLoad
{
UINib *tableCellNib = [UINib nibWithNibName:@"MyCell" bundle:nil];
[self.tableView tableCellNib forCellReuseIdentifier:kMyCellIdentifier];
}
然后,您可以在需要时创建该单元格。由于您已经注册了 Nib,如果还没有要出列的单元格,iOS 将为您处理创建单元格:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kMyCellIdentifier];
// Configure cell...
return cell;
}
您也可以直接在 IB 中在 Storyboards 中创建原型单元,但是如果您有一个通用应用程序,那就很痛苦了,因为您需要同时维护同一单元的 iPad 和 iPhone 版本。
【讨论】:
在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;
}
【讨论】: