43

i actually dont see my error:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  static NSString *CellIdentifier = @"Cell";

  FriendTableViewCell *cell = (FriendTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  if (cell == nil) {
      cell = [[[FriendTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
      [[NSBundle mainBundle] loadNibNamed:@"FriendTableViewCell" owner:self options:nil];
      cell = friendCell;
  }
  cell.lblNickname.text =  @"Tester";
  return cell;
}

What am i doing wrong? I checked all twice.. but dont see the error.

Thanks for your help!

Ed Marty
  • 39,590
  • 19
  • 103
  • 156
phx
  • 1,509
  • 3
  • 15
  • 18

9 Answers9

66

You're returning friendCell, and it's very likely nil.

Your code looks fine, so make sure you have your Interface Builder file set up right. In FriendTableViewCell.xib, be sure the File's Owner is your table view controller and that you correctly set the cell to be an outlet to friendCell (which I assume is a UITableViewCell).

greenisus
  • 1,707
  • 14
  • 17
  • 1
    Thanks, after checkin all again i found a missing outlet connection in Interface Builder! – phx Feb 09 '10 at 17:39
  • 1
    Thanks, your answer is just what I needed. I'd changed the name of the view controller class, breaking the connection to IB without my thinking about it. No more changing names because I don't like them! – Matthew Frederick Nov 23 '10 at 09:35
  • Great answer, usually the problem is something about returning a nil cell! – Fattie Nov 05 '13 at 16:14
  • If you have a UITableView inside a UIViewController, make sure you have created an outlet connection from your UITableView to your UIViewController. – Joseph Gill May 27 '15 at 20:29
33

For me, it worked doing this:

if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

UPDATE
Place the above code in the following method:

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

Just put it BEFORE editing the tableViewCell

Sam Spencer
  • 8,492
  • 12
  • 76
  • 133
JomanJi
  • 1,407
  • 1
  • 17
  • 27
2

I know this is an old post now but, I have just encountered this error, I found it very strange as the app was in testing so no fresh builds for a few days and it did this, all I did was reboot the phone and it solved it.

Popeye
  • 11,839
  • 9
  • 58
  • 91
2

You are creating a FriendTableViewCell and then ignoring it and setting it equal to (presumably) an instance variable named friendCell.

I assume that you expect friendCell to be set when calling the loadNibNamed method. It apparently is not being set.

So you've got two problems with this code. First, don't allocate a cell twice.

cell = [[[FriendTableViewCell ....
[[NSBundle mainBundle .....
cell = friendCell;

Obviously, the creation of a new cell and assigning it to cell is useless if you are overwriting it with the second call to assignment to cell.

Second, friendCell is probably nil. Make sure the NIB is set up correctly and has the outlets pointing to the right places.

Ed Marty
  • 39,590
  • 19
  • 103
  • 156
1

I found that this problem came when trying to create my UITableViewCell before initialising my table view:

Here registering the class before creating the tableView will cause the error, placing it after will fix the error.

[tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:bCellIdentifier]; 

tableView = [[UITableView alloc] initWithFrame:CGRectZero];
tableView.delegate = self;
tableView.dataSource = self;

[self addSubview:tableView];

tableView.keepInsets.equal = KeepRequired(0);
sam_smith
  • 6,023
  • 3
  • 43
  • 60
  • in my case i call the function to load data before the table UITableViewCell register, upvote as your answer helps me to overcome this problem – 9to5ios Aug 04 '16 at 10:28
0

Do not forget to set cell identifier in Interface Builder.

Artem Shmatkov
  • 1,434
  • 5
  • 22
  • 41
0

Use This ,

UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:kCellID];

I saved few more hours with just this lines.

Sanoj Kashyap
  • 5,020
  • 4
  • 49
  • 75
0

Make sure your reuse identifier in your NIB / Storyboard file for your prototype cell matches whatever you called CellIdentifier

 static NSString *CellIdentifier = @"Cell";
M Jesse
  • 2,213
  • 6
  • 31
  • 37
0

Look here: Loading TableViewCell from NIB

This is Apple's document for this exact subject.

//This is assuming you have tvCell in your .h file as a property and IBOutlet
//like so:
TableViewController.h
@property(nonatomic,retain) IBOutlet UITableViewCell *tvCell;
//Data Source Method...
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];

if (cell == nil) {

    [[NSBundle mainBundle] loadNibNamed:@"TVCell" owner:self options:nil];

    cell = tvCell;

    self.tvCell = nil;

use the loadNibNamed:owner:options method to load a cell in a nib. Set the cell instance to your nib object, then set the nib object to nil.

Read the rest of the documentation that I've linked to know how to access subviews inside your cell.

Daddy
  • 9,045
  • 7
  • 69
  • 98