开发者

Adding custom UITableViewCell crashes the simulator

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 = [[[UI开发者_如何学运维TableViewCell 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 Golf[26935:40b] * Assertion failure in -[UITableView _createPreparedCellForGlobalRow:withIndexPath:], /SourceCache/UIKit_Sim/UIKit-984.38/UITableView.m:4709 2010-04-23 11:17:33.164 Golf[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 Golf[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


You should also make sure to set the Custom Class type in Interface Builder to the corresponding class name ( which is MyTableCell in your example ), otherwise casting won't work either.


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.


I created this define to help


#define LoadViewNib(__NIBNAME__) [[[NSBundle mainBundle] loadNibNamed:__NIBNAME__ owner:self options:nil] objectAtIndex:0]

for use, you can only do this



 if(cell == nil) {

        cell = LoadViewNib(@"MyCellNibName");
 }


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


Reference: A Closer Look at Table-View Cells

If you are loading your own custom cell then, "An important attribute to set for the programmatic portion of this procedure is each object’s tag property. Find this property in the View section of the Attributes pane and assign each object a unique integer."

Now retrieve your label like this (I suppose you assign tag=1 to label)

UILabel *cellLabel = (UILabel *)[cell viewWithTag:1];
cellLabel.text = @"Your text goes here"
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜