cocoa-touch - 桌面 View :cellforrowatindexpath with multiple cell types

标签 cocoa-touch uitableview

当我构建和分析时,我被告知单元格永远不会获得值......从我的逻辑来看,这似乎是错误的,但随后应用程序在尝试加载表格时崩溃了。那么……为什么要来?

    - (UITableViewCell *)tableView:(UITableView *)tableView 
             cellForRowAtIndexPath:(NSIndexPath *)indexPath {

        static NSString *Cell1Identifier = @"Cell1";
        static NSString *Cell2Identifier = @"Cell2";
        static NSString *Cell3Identifier = @"Cell3";
        UITableViewCell *cell;

        if ([indexPath section] == 0) {
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Cell1Identifier];

            if (cell == nil) {
                cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2
                                               reuseIdentifier:Cell1Identifier] autorelease];
            }
        }

        else if ([indexPath section] == 1) {
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Cell3Identifier];
            if (cell == nil) {
                cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
                                               reuseIdentifier:Cell3Identifier] autorelease];
            }
        }

        else {
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Cell2Identifier];
            if (cell == nil) {
                cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle 
                                               reuseIdentifier:Cell2Identifier] autorelease];
            }
        }


        // Configure the cell...
        [self configureCell:cell
                atIndexPath:indexPath];

        return cell;
    }

最佳答案

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView 
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *Cell1Identifier = @"Cell1";
    static NSString *Cell2Identifier = @"Cell2";
    static NSString *Cell3Identifier = @"Cell3";
    NSString *identityString = @"";
    switch ([indexPath section]) {
        case 0: {
            identityString = Cell1Identifier;
            break;
        }
        case 1: {
            identityString = Cell3Identifier;
            break;
        }
        case 2: {
            identityString = Cell2Identifier;
            break;
        }

        default:
            break;
    }

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identityString];

    if ([indexPath section] == 0) {

        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2
                                           reuseIdentifier:Cell1Identifier] autorelease];
        }
    }

    else if ([indexPath section] == 1) {
        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
                                           reuseIdentifier:Cell3Identifier] autorelease];
        }
    }

    else {
        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle 
                                           reuseIdentifier:Cell2Identifier] autorelease];
        }
    }


    // Configure the cell...
    [self configureCell:cell
            atIndexPath:indexPath];

    return cell;
}

关于cocoa-touch - 桌面 View :cellforrowatindexpath with multiple cell types,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3900551/

相关文章:

iphone - 线程和 NSTimer

cocoa-touch - MPMoviePlayerController:如何制作我的视频循环?

iphone - 允许一个 View 支持多个方向,而其他 View 则不支持 iPhone

ios - 在 UITableView 顶部显示消息

swift - 线程 1 : Fatal error: Index out of range from tableViewCell

ios - iOS 中tableview 的分页问题

iphone - 如果单元格是第一个响应者并且不在屏幕上,则不会调用UITableViewCell setEditing:animated

iPhone 动画问题 SIMulator 与 Real Device

ios - 为什么我的 tableView 函数运行了 3 次?

ios - TextView 数组到 TableView