UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath: Exception

后端 未结 9 2062
悲&欢浪女
悲&欢浪女 2021-01-03 19:15

i actually dont see my error:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  static NSString *Cel         


        
相关标签:
9条回答
  • 2021-01-03 19:52

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

    0 讨论(0)
  • 2021-01-03 19:53

    Do not forget to set cell identifier in Interface Builder.

    0 讨论(0)
  • 2021-01-03 19:54

    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);
    
    0 讨论(0)
  • 2021-01-03 19:55

    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.

    0 讨论(0)
  • 2021-01-03 19:55

    Use This ,

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

    I saved few more hours with just this lines.

    0 讨论(0)
  • 2021-01-03 19:56

    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.

    0 讨论(0)
提交回复
热议问题