UIStoryBoard中UITableViewCell的子类

时间:2011-11-02 09:44:12

标签: objective-c ios5

我最近使用带有UIStoryBoards的iOS 5启动了一个新项目(非常棒的功能;))。 特别是在tableView中创建静态单元格的能力很棒。但这里开始我的问题。我想通过使用UITableViewCell的子类来自定义这些单元格,而不必使用静态内容自定义每个tableView中的单元格。

首先,我认为所需要的只是将TableViewCell的类设置为MyCustomClass 但设计没有使用。

长话短说:   有没有办法在UIStoryboard中使用UITableViewCell的子类作为静态内容?   故事情节如何实例化细胞? (它不是init()或initWithStyle())

提前谢谢你;)

2 个答案:

答案 0 :(得分:2)

我真的不知道是否有办法在界面构建器中使用自定义单元格(可能不可能)。 UITableViewCells通过initWithCoder:方法启动,就像符合NSCoding协议的任何其他对象一样。

答案 1 :(得分:1)

除了设置自定义类之外,在检查器面板中设置标识符也很重要。然后检索你真棒的新设计用途:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  static NSString *cellIdentifier = @"myCustomID";
  //New cell prototypes will be returned from the dequeue if none have been allocated yet.
  MySubclassCell* cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

  if(cell == nil) {
    //This shouldn't happen but you can build manually to be safe.
    cell = [[MySubclassCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
  }

  return cell;
}

Dequeueing将为您构建新单元格而不需要自己分配。

相关问题