Vor*_*ork 9 cocoa-touch uitableview ios
在我的应用程序中,我使用的是故事板.但我以UITableView编程方式创建而不是从对象库中拖动.现在我想自定义以编程方式创建的单元格UITableView.任何人都可以通过提供UITableViewCell在故事板中以编程方式创建的示例来帮助我吗?
我会避免将你的细胞布局和构建cellForRowAtIndexPath.
要以编程方式创建自定义单元格,首先应创建一个UITableViewCell子类.
添加到它labels,imageViews等..添加为子视图cell.contentView.
编程
即
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
_label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 21)];
[self.contentView addSubview:_label];
}
return self;
}
Run Code Online (Sandbox Code Playgroud)
如果你想做单元格的布局,那么MyCell你可以在课堂上做...
- (void)layoutSubViews
{
[super layoutSubviews];
// layout stuff relative to the size of the cell.
}
Run Code Online (Sandbox Code Playgroud)
然后在tableViewController你需要注册细胞类......
在viewDidLoad......
[self.tableView registerClass:[MyCell class] forCellReuseIdentifier:@"MyCellIdentifier"];
Run Code Online (Sandbox Code Playgroud)
与界面建设者
仍然创建自定义子类,但也创建一个同名的xib文件.然后在你的xib文件中你可以连接出口而不必在单元的init中创建它们.(如果你这样做,那么无论如何都不会调用init).
您需要的唯一其他更改是,viewDidLoad您需要为单元格而不是类注册nib.
像这样...
UINib *cellNib = [UINib nibWithNibName:@"MyCell" bundle:nil];
[self.tableView registerNib:cellNib forCellReuseIdentifier:@"MyCellIdentifier"];
Run Code Online (Sandbox Code Playgroud)
然后其他一切都是一样的.
使用细胞
要使用已创建子类的单元格...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyCellIdentifier"];
[self configureCustomCell:(MyCell*)cell atIndexPath:indexPath];
return cell;
}
- (void)configureCustomCell:(MyCell*)cell atIndexPath:(NSIndexPath *)indexPath
{
// do all you logic of getting any info from arrays etc in here.
cell.label.text = @"Blah".
}
Run Code Online (Sandbox Code Playgroud)
摘要
这样做意味着你的tableviewcontroller只对将东西放入单元格感兴趣.如果你把所有逻辑都用于构建你的单元格,那么一切都会变得非常混乱.
这也意味着您不必处理大量不同的标签来保存和检索不同的UI元素.
我将描述两个选项:使用 Interface Builder 添加单元格(更简单),以及以编程方式添加单元格:
通过使用界面生成器
在 Interface Builder 中创建单元格并使用自定义内容添加子视图后,打开属性检查器,选择自定义样式并在重用标识符文本字段中输入唯一标识符(例如“anIdentifier”)。接下来,选择要以编程方式访问的单元格字段,并为每个字段设置唯一的标记号(位于“视图”部分下)。
然后,在您的数据源代码中实现此方法:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"anIdentifier"];
UILabel *label;
label = (UILabel *)[cell viewWithTag:1]; // Set a constant for this so your fellow developers understand this number
label.text = @"This is a test";
return cell;
}
Run Code Online (Sandbox Code Playgroud)
以编程方式
如果您想以编程方式创建单元格,则代码应类似于以下内容:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"anIdentifier";
UILabel *aLabel;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]];
aLabel = [[[UILabel alloc] initWithFrame:...]];
aLabel.tag = 1; // Set a constant for this
aLabel.font = [UIFont systemFontOfSize:14.0];
aLabel.textAlignment = UITextAlignmentRight;
aLabel.textColor = [UIColor blackColor];
aLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight;
[cell.contentView addSubview:mainLabel];
} else {
aLabel = (UILabel *)[cell.contentView viewWithTag:1];
}
aLabel.text = @"This is a test";
return cell;
}
Run Code Online (Sandbox Code Playgroud)
Apple 网站上有更多信息:http://developer.apple.com/library/ios/#documentation/userexperience/conceptual/tableview_iphone/TableViewCells/TableViewCells.html