在故事板中以编程方式创build的自定义tableviewCells for UITableView
在我的应用程序中,我正在使用故事板。 但我创build了一个UITableView
编程,而不是从对象库中拖动。 现在我想定制这个程序创build的UITableView
的单元格。 任何人都可以通过提供一个在故事板中以编程方式创buildUITableViewCell
的示例来帮助我吗?
我会避免把布局和你的单元格build成cellForRowAtIndexPath
。
要以编程方式创build自定义单元格,您应该先创build一个UITableViewCell
子类。
添加到它的labels
,imageViews等…添加cell.contentView as子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; }
如果你想做的单元布局的东西,然后在MyCell
类,你可以做…
- (void)layoutSubViews { [super layoutSubviews]; // layout stuff relative to the size of the cell. }
然后在tableViewController
你需要注册单元类…
在viewDidLoad
…
[self.tableView registerClass:[MyCell class] forCellReuseIdentifier:@"MyCellIdentifier"];
与接口build造者
仍然创build自定义子类,但也创build一个相同名称的xib文件。 然后在你的xib文件中,你可以连接sockets,而不必在单元的init中创build它们。 (如果你这样做,那么init将不会被调用)。
你需要的唯一的其他改变是在viewDidLoad
你需要注册单元格而不是类的笔尖。
喜欢这个…
UINib *cellNib = [UINib nibWithNibName:@"MyCell" bundle:nil]; [self.tableView registerNib:cellNib forCellReuseIdentifier:@"MyCellIdentifier"];
那么一切工作都一样。
使用细胞
要使用您创build子类的单元格…
- (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". }
概要
这样做意味着你的tableviewcontroller只是把东西放到单元格中。 如果你把所有的逻辑build立你的单元格,所有的东西都变得非常混乱。
这也意味着你不必处理不同标签的负载来保存和检索不同的UI元素。
我将描述两个选项:使用Interface Builder(更简单)添加单元格,然后以编程方式添加单元格:
通过使用Interface Builder
在界面构build器中创build了单元格并添加了子视图后,打开“属性”检查器,select“ 自定义”样式并在“ 重用标识符”文本字段(例如“anIdentifier”)中input唯一标识符。 接下来,select您希望以编程方式访问的单元格字段,并为它们中的每一个设置唯一的标签号(位于“ 查看”部分下)。
然后,在你的数据源代码中,实现这个方法:
- (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; }
编程
如果你想以编程方式创build单元格,那么代码应该类似于以下内容:
- (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; }
有更多的信息在苹果的网站: http : //developer.apple.com/library/ios/#documentation/userexperience/conceptual/tableview_iphone/TableViewCells/TableViewCells.html