简体   繁体   中英

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 = [[[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 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

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 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.

您还应该确保将Interface Builder中的Custom Class类型设置为相应的类名(在您的示例中为MyTableCell ),否则强制转换也将不起作用。

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"

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM