views:

140

answers:

2

Im trying to build my application using a custom UITableViewCell. This is the code in my UIViewController that adds the viewCell to the table:

  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{   
    NSLog(@"------- Tableview --------");

    static NSString *myIdentifier = @"MyIdentifier";

    MyTableCell *cell = (MyTableCell *)[tableView dequeueReusableCellWithIdentifier:myIdentifier];
    if(cell == nil) {
        NSArray *cellView = [[NSBundle mainBundle] loadNibNamed:@"tblCellView" owner:self options:nil];
        cell = [cellView objectAtIndex:0];
    }

    [cell setLabelText:[NSString stringWithFormat:@"indexpath.row: %d", indexPath.row]];


    //cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:myIdentifier] autorelease];
    cell.textLabel.text = @"bös";
    return cell;
    }

if i uncomment the line above "return cell" it returns a regular UITableViewCell without any errors, but as soon as i try to implement my custom cell it crashes with this error:

------- Tableview -------- 2010-04-23 11:17:33.163 SogetGolf[26935:40b] * Assertion failure in -[UITableView _createPreparedCellForGlobalRow:withIndexPath:], /SourceCache/UIKit_Sim/UIKit-984.38/UITableView.m:4709 2010-04-23 11:17:33.164 SogetGolf[26935:40b] * Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:' 2010-04-23 11:17:33.165 SogetGolf[26935:40b] Stack: (

...

I have configured the .xib file as one should with the proper outlets. And the identifier of the UITableViewCell corresponds with name im trying to load from NSBundle

A: 

I cannot see where tblCell is declared and that could be one of many issues with your code. First of all you need to do something like:

if (cell == nil) {
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:self options:nil];
    cell = (MyTableCell *)[nib objectAtIndex:0];
}

You are just loading the nib but never extracting the cell from it.

--Update--

You should also check your custom cell in IB if it has any errors or incorrect bindings.

willcodejavaforfood
Im sorry, that was old code. I have now updated my original question.
nevva
Try casting it to (MyTableCell *)
willcodejavaforfood
A: 

I think loadNibNamed: nethod returns nil for some reason. So resulting cell object nil too.

Skie