在Xcode5上创建CustomCell

时间:2013-12-08 00:48:29

标签: ios xcode uitableview xcode5

我试图在没有故事板的情况下在Xcode 5中创建一个CustomCell(idk,从来不是我的东西),但似乎不可能。有谁知道如何解决它?

我尝试了什么:

  1. 我在ViewController笔尖中创建了一个Cell,并将其与我的Viewcontrollers .h相连。这很有效,但它只适用于多个自定义单元格(enter link for the tut)。

  2. 和以前一样但是现在我将它与UITableViewCell的子类连接起来了,但是应用程序崩溃了。

  3. 我已经设置了单元格的标识符,但没办法。有没有人有想法?

    编辑1: 我重新检查了我的代码。现在我得到了这个,我仍然认为它基于xcode 5:

    代码:

    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *CellIdentifier = @"HomeLoginCell";
    GSCustomLoginCell *tableCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    NSLog(@"%d", indexPath.row);
    NSLog(@"%@", [tableSource objectAtIndex:indexPath.row]);
    if (indexPath.row == 0) {
        tableCell.cellText.text = [tableSource objectAtIndex:indexPath.row];
        tableCell.cellIcon.image = [UIImage imageNamed:@"user_male-32"];
    }else if(indexPath.row == 1){
        tableCell.cellText.text = [tableSource objectAtIndex:indexPath.row];
        tableCell.cellIcon.image = [UIImage imageNamed:@"enter-32"];
    }
    
    return tableCell;
    

    }

    错误:

    2013-12-08 02:02:37.646 Group2Study[3921:60b] *** Assertion failure in -[UITableView _configureCellForDisplay:forIndexPath:], /SourceCache/UIKit/UIKit-2903.23/UITableView.m:6246
    

    2013-12-08 02:02:37.648 Group2Study [3921:60b] *由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'UITableView dataSource必须从tableView返回一个单元格:cellForRowAtIndexPath:' * 第一次抛出调用堆栈: (0x2d72de83 0x37a8a6c7 0x2d72dd55 0x2e0d60af 0x2ffe5649 0x2fec3cf5 0x2ffe54b9 0x2ffe432d 0x2ff8c6cd 0x2ff8bef1 0x2feb2353 0x2fb38943 0x2fb34167 0x2fb63425 0x2ff2f18f 0x2ff2d7f9 0x2ff2ca37 0x2ff2c9bf 0x2ff2c957 0x2ff25459 0x2feb8397 0x2ff2c6a9 0x2ff2c17d 0x2febd581 0x2febaae5 0x2ff2582d 0x10e2a9 0x2ff22aad 0x2ff224f3 0x2ff1cb41 0x2feb7a07 0x2feb6cfd 0x2ff1c321 0x3239c76d 0x3239c357 0x2d6f8777 0x2d6f8713 0x2d6f6edf 0x2d661471 0x2d661253 0x2ff1b5c3 0x2ff16845 0x11c661 0x37f83ab7) libc ++ abi.dylib:以NSException类型的未捕获异常终止 (lldb)

1 个答案:

答案 0 :(得分:0)

GSCustomLoginCell *tableCell = [tableView 
    dequeueReusableCellWithIdentifier:CellIdentifier];

if(!tableCell) {
   tableCell = [[GSCustomLoginCell alloc] init];
}

dequeueReusableCellWithIdentifier仅在重用已经使用过的单元格时返回一个单元格。所以......第一次尝试加载表时,它不会回收任何单元格,此方法将返回nil。因此,在调用此方法之后,您应该立即检查它是否为nil,如果它是nil,则实例化一个新对象。

错误消息说明您没有返回UITableViewCell,这是因为,正如我所解释的那样,dequeueReusableCellWithIdentifier正在返回nil