ios - 使用多个自定义 UITableViewCells

标签 ios objective-c uitableview

我正在尝试实现分组样式的 UITableView,例如联系人应用 detailedView。我希望最上面的单元格是透明的,并在底部有一个 UISegemtedControl。

当我尝试创建两种不同类型的自定义单元格时,即使我使用了两个不同的 cellIdentifier,也只有第一个被加载。

非常感谢您的指导。或者针对同一主题的一些很好的教程技巧。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    /*
    UIView *backView = [[UIView alloc] initWithFrame:CGRectZero];
    backView.backgroundColor = [UIColor clearColor];
    cell.backgroundView = backView;
    [backView release];
    */

    static NSString *cellIdentifier1 = @"DetailCellStyle1";
    static NSString *cellIdentifier2 = @"DetailCellStyle2";

    if (indexPath.section == 0) {

        // Load from nib
        DetailCellViewController *cell = (DetailCellViewController *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier1];
        if (cell == nil) {
            NSArray *topLevelObjects = [[NSBundle mainBundle]
                                        loadNibNamed:@"DetailCellView" 
                                        owner:nil 
                                        options:nil];

            for (id currentObject in topLevelObjects) {
                if ([currentObject isKindOfClass:[UITableViewCell class]]) {
                    cell = (DetailCellViewController *) currentObject;
                    break;
                }
            }
        }

        return cell;
    }
    else  {
        // Load from nib
        DetailCellViewController2 *cell = (DetailCellViewController2 *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier2];
        if (cell == nil) {
            NSArray *topLevelObjects = [[NSBundle mainBundle]
                                        loadNibNamed:@"DetailCellView" 
                                        owner:nil 
                                        options:nil];

            for (id currentObject in topLevelObjects) {
                if ([currentObject isKindOfClass:[UITableViewCell class]]) {
                    cell = (DetailCellViewController2 *) currentObject;
                    break;
                }
            }
        }

        return cell;
    }

    return nil;
}

最佳答案

通过在“DetailCellView”nib 中获取类型为 UITableViewCell 的第一个对象,您以完全相同的方式加载单元格 1 和单元格 2。因此,在这两种情况下您都会得到相同的单元格。

关于ios - 使用多个自定义 UITableViewCells,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6982419/

相关文章:

ios - 核心数据id自增

javascript - 如何在 Xcode (Objective-c) 上连接 URL 中的字符串?

objective-c - 在不同的 TableView 之间切换

ios - 高度从 0 变为默认大小后单元格不显示

ios - 如何在 TAP 上编辑 tableview 单元格而不是滑动 - Swift

ios - 在 Prototype Cell 中访问 UISwitch

ios - XCode 4.6 管理器不表示我的应用程序崩溃堆栈跟踪

ios - UIImagePickerController - 横向拍摄的照片在顶部创建黑条

ios - 如何访问存储在数组中的对象的数据?

objective-c - 在 Objective C 中启动命令行