iphone - 自定义 UITableViewCell 只绘制一个单元格

标签 iphone objective-c ios uitableview

我想显示自定义单元格,但我的代码一次只显示一个自定义表格单元格。我做错了什么?

我在 UIView 中设置了一个 UIViewController nib 和我的 UITableView。 nib中还有一个UITableViewCell,它的类是CustomCell(UITableViewCell的子类)。 UITableView 和 Cell 都是@sythesized IBOutlet @properties。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *CellIdentifier  = @"CellIdentifier";
    CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; // CustomCell is the class for standardCell
    if (cell == nil) 
    {
        cell = standardCell; // standardCell is declared in the header and linked with IB
    }
    return cell;
}

最佳答案

您可以使用下面的示例代码;


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *CellIdentifier  = @"CellIdentifier";
    CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; // CustomCell is the class for standardCell
    if (cell == nil) 
    {
    NSArray *objectList = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
    for (id currentObject in objectList) {
        if([currentObject isKindOfClass:[UITableViewCell class]]){
            cell = (CustomCell*)currentObject;
            break;
        }
    }
    }
    return cell;
}

关于iphone - 自定义 UITableViewCell 只绘制一个单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4088412/

相关文章:

iphone - 在 iOS 上的 UITabBarController 下方添加 UIViewController 就像 youtube 应用程序

ios - iPhone 应用程序在 iPad 上的兼容性

objective-c - NSTextView 暂时禁用自动拼写更正

objective-c - 在 iOS7 的 UIWebView 中点击超链接日期会显示不正确的日期,但在 iOS 6 中却不会?

ios - CoreData 在 NSFetchRequest 找到新对象之前需要一些时间

ios - cooking 书应用程序的自定义 iOS View

iphone - 我是否通过在启动时获取一次对象并在我想保留数据时调用保存来正确使用核心数据?

iphone - UIView 动画在关闭模态视图后停止工作

ios - 更改 View 变换会更改 iOS 7 上的 View 位置,但不会更改 ios8

iOS:自动布局灵活边距