首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在iOS 5情节串连图板中初始化自定义原型样式的表格单元?

如何在iOS 5情节串连图板中初始化自定义原型样式的表格单元?
EN

Stack Overflow用户
提问于 2012-02-18 03:27:28
回答 5查看 54.3K关注 0票数 36

我正在转换到iOS 5和故事板。当我有一个默认单元格样式的表视图时,一切都很正常。

代码语言:javascript
复制
- (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必须从tableView:cellForRowAtIndexPath中返回一个单元格:“。这不是问题,因为它的工作方式如上图所示。

我的问题是,我希望对单元格使用自定义样式,因此不能使用initWithStyle。如何初始化我在故事板上设计的自定义单元格?

旧的pre-5应用程序有一个nib和class,使用了类似这样的东西,但现在我使用的是故事板。

代码语言:javascript
复制
- (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;
}
EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2012-02-18 03:32:40

这边请

代码语言:javascript
复制
- (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;
}
票数 39
EN

Stack Overflow用户

发布于 2013-01-24 01:57:47

这非常适合我(Xcode4.5.2和iOS 6.0):

代码语言:javascript
复制
-(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;
}

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

票数 9
EN

Stack Overflow用户

发布于 2012-08-09 18:09:43

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

代码语言:javascript
复制
MyViewController *myViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"MyViewController"];

然后在cellForRowAtIndexPath中,你只需要一行:

代码语言:javascript
复制
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyIdentifierFromStoryboard"];

如果不存在单元格,dequeueReusableCellWithIdentifier将实例化一个单元格。

票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9334156

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档