【问题标题】:How to initialize a custom prototype style table cell in iOS 5 storyboards?如何在 iOS 5 故事板中初始化自定义原型样式表单元格?
【发布时间】:2012-03-09 04:50:03
【问题描述】:

我正在转换到 iOS 5 和情节提要。当我有一个具有默认单元格样式的表格视图时,一切正常。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyIdentifierFromStoryboard"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MyIdentifierFromStoryboard"];
    }
    return cell;
}

我已经看到了删除“if (cell == nil)”块的示例。但是,如果我把它拿出来,我的应用程序会崩溃并显示以下消息:“UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:”。这不是问题,因为它的工作原理如上所示。

我的问题是我想为单元格使用自定义样式,因此无法使用 initWithStyle。如何初始化我在情节提要上设计的自定义单元格?

旧的 pre-5 应用有一个 nib 和 class 使用类似的东西,但现在我正在使用情节提要。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MyCustomTableCell *cell = (MyCustomTableCell *) [tableView dequeueReusableCellWithIdentifier:@"MyCustomIdentifier"];
    if (cell == nil) {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"MyCustomTableCell" owner:self options:nil];
        cell = (MyCustomTableCell *) [nib objectAtIndex:0];
    }
    return cell;
}

【问题讨论】:

  • 如果您仍然尝试将此 Nib 用作自定义 TableViewCell 怎么办?

标签: ios ios5 uitableview storyboard


【解决方案1】:

这边

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    return cell;
}

【讨论】:

  • 这会导致单元格具有默认样式,而不是故事板中的自定义样式。
  • 感谢您的链接。我让它工作。一个有点棘手的步骤是控制从视图上的空白区域拖动。
  • 这个答案已经过时,现在是错误的。 developer.apple.com/library/ios/documentation/uikit/reference/…:
  • 不。现在您提前向表格视图注册表格单元格类,并且当您出列时,单元格保证存在。
【解决方案2】:

这非常适合我(Xcode 4.5.2 和 iOS 6.0):

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

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

    UILabel *title = (UILabel*) [cell viewWithTag:1000];
    UILabel *summary = (UILabel*) [cell viewWithTag:1001];
    [title setText:[ tableMainTitle objectAtIndex:indexPath.row]];
    [summary setText:[ tableSubTitle objectAtIndex:indexPath.row]];
    return cell;
}

重要提示:不要忘记设置委托和数据源。

【讨论】:

  • 对于带有故事板的iOS6,出列后单元格永远不会为零。
  • @mskw 所以单元格将始终有效且可用?或者他们是否有其他方法来识别无效单元格(如常量或其他东西)
  • 假设你有正确的标识符是的。否则它会崩溃抱怨它。
【解决方案3】:

如果您使用以下方式加载包含 tableview 的视图控制器:

MyViewController *myViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"MyViewController"];

那么在cellForRowAtIndexPath 里面你只需要一行:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyIdentifierFromStoryboard"];

dequeueReusableCellWithIdentifier 将在不存在的情况下实例化一个单元格。

【讨论】:

  • 据我所知,这应该是公认的答案。在使用情节提要时检查零单元等是无用的样板。
【解决方案4】:

我觉得这个效果很好。一段时间以来,我一直在努力解决它,感谢它起作用了.. 好吧,我正在这样做,当我尝试分配一个单元时,我正在使用通用单元类而不是我创建的那个来分配

所以我是这样做的,所以这就是它不起作用的原因

cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

所以用你的类而不是 UITableViewCell 分配单元格,以及上面的示例“CustomCell”

再次感谢

【讨论】:

    【解决方案5】:
    static NSString *identifier=@"SearchUser";
    SearchUserCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell==nil)
    {
        cell=[[SearchUserCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    return cell;
    

    【讨论】:

    • 那段代码是什么意思?一些评论会有所帮助。
    猜你喜欢
    • 1970-01-01
    • 2013-08-22
    • 1970-01-01
    • 2015-08-07
    • 2012-09-26
    • 2013-07-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多