16

This started to happen out of the blue. Any ideas: Code:

CUSTOMCLASSNAME (I have replaced the actual class name as it contains the name of the client.)

Initialising my tableView:

[self.tableView registerClass:[CUSTOMCLASSNAME class] forCellReuseIdentifier:[self reuseIdentifier]];

In cell for row:

Hi, the title is being printed in the console. This is my cellForRow:

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

    AVTCheckListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[self reuseIdentifier] forIndexPath:indexPath];

    [self configureCell:cell atIndexPath:indexPath];

    return cell;
}

- (void)configureCell:(AVTCheckListTableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
    ChecklistGroup *group = [self.checklistFetchedResultsController.fetchedObjects objectAtIndex:indexPath.section];

    ChecklistItem *item = [self getChecklistItemForIndexPath:indexPath];

    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    [[cell cellTextView] setAttributedText:[[item checked] boolValue] ? [[NSAttributedString alloc] initWithString:[item name] attributes:@{ NSStrikethroughStyleAttributeName : @(NSUnderlineStyleSingle) } ] : [[NSAttributedString alloc] initWithString:[item name]]];
    [[cell cellTextView] setUserInteractionEnabled:[[group isUserDefined] boolValue]];
    [[cell cellTextView] setTag:indexPath.row];
    [[cell cellTextView] setDelegate:self];
    [[cell tickImage] setHidden:![[item checked] boolValue]];
}

//Method that returns re-use:

- (NSString *) reuseIdentifier {
    return @"CheckListTableView";
}
j.f.
  • 3,908
  • 2
  • 29
  • 42
CW0007007
  • 5,681
  • 4
  • 26
  • 31
  • What started happening out of the blue? – rdelmar Aug 28 '13 at 15:06
  • Hi the error is the title. No index path for table cell being reused. THe table view then has erratic behaviour when scrolling. Until all cells have been loaded, but as they aren't being reused this is an issue. – CW0007007 Aug 28 '13 at 15:44
  • Its not clear at all what problem you are experiencing. Please add more information to your post that explains your problem. If your app throw an exception include that as well. – Aaron Aug 28 '13 at 15:58
  • You should post your whole cellForRowAtIndexPath method. How do you know the indexPath isn't being reused? – rdelmar Aug 28 '13 at 16:49
  • Hi, the title is being printed in the console. This is my cellForRow: – CW0007007 Aug 29 '13 at 08:15
  • Have edited. Symptoms are; eratic cell heights, scrolling stops randomly. cell titles re-arrange. THis stops once all the cells have been drawn. – CW0007007 Aug 29 '13 at 08:22

13 Answers13

59

Return [cell contentView] instead of cell from tableView:viewForHeaderInSection:

EDIT: This somehow led to crashes on long-press gestures on UI Buttons. To fix that, I gave up and used another section with a single item as a fake section header.

TalkLittle
  • 8,866
  • 6
  • 54
  • 51
  • This solved it for me. In my case it was the viewForFooterInSection method. Thank you! – GivP Jul 16 '14 at 21:46
  • I can confirm that using doing a long press on this section causes the app to crash – Paul N Feb 12 '15 at 21:07
  • 1
    But what I end doing was to add a LongPressGesture to Content View, this blocks the LongPress event to be passed to the Cell that is already deallocated as we are returning the content View, that's what causes the crash and solves the problem. – Paul N Feb 12 '15 at 21:15
  • Code:UILongPressGestureRecognizer *touchDown = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(didTouchDownPress:)]; touchDown.minimumPressDuration = .001; [headerView.contentView addGestureRecognizer:touchDown]; – Paul N Feb 12 '15 at 21:16
  • This allow me to highlight everything on state Began and process the action on state ended – Paul N Feb 12 '15 at 21:16
  • adding a "fake" long-press gesture will block all touches to header. So, you won't be able to add buttons. You also won't be able to scroll if you start within the header. Any way around this? – Andy Mar 02 '15 at 21:05
  • Yeah, this is a pain -- nothing works right. Why doesn't apple let us create custom section headers easily? I am just creating a separate view and using that. – Andy Mar 02 '15 at 21:20
10

I found this warning during my viewForHeaderInSection implementation.

This is my solution in Swift 2.x:

let headerView = self.tableView.dequeueReusableCellWithIdentifier("MyCell") as! MyCell
let containerView = UIView(frame:headerView.frame)
headerView.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]
headerView.myLabel.text = "section title 1"
containerView.addSubview(headerView)
return containerView

This is the Swift 3 version:

let headerView = self.tableView.dequeueReusableCell(withIdentifier: "MyCell") as! MyCell
let containerView = UIView(frame:headerView.frame)
headerView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
headerView.myLabel.text = "section title 1"
containerView.addSubview(headerView)
return containerView
Alessandro Ornano
  • 34,887
  • 11
  • 106
  • 133
  • 1
    This solution worked for me in Objective C, thanks :-) Wanted to add that I did have to setTranslatesAutoresizingMaskIntoContraints to NO on my header cell and give it constraints to my containerView. Also for rotation in my viewWillTransitionToSize:withSizeCoordinator: I had to add a call to have my table view reload it's data and after that I setNeedsLayout on my view. Hope this info helps someone else out too. – CodenameDuchess Dec 02 '15 at 03:05
7

I have just tracked down a cause of this error message.

A UITableViewCell was being used as a normal view. Therefore its' indexPath was not set. In our case one was being returned by viewForHeaderInSection:

Hope this helps.

Chris

Mutawe
  • 6,464
  • 3
  • 47
  • 90
chrisoneiota
  • 434
  • 4
  • 5
2

In my case, this error was presented only on iPhone (not iPad) because I had placed [self.tableView beginUpdates]; and later [self.tableView endUpdates]; inside [UIView transitionWithView:duration:options:animations:compeletion];. To fix, I just removed begin/end updates, as I preferred a fading animation instead of fading plus animating cell height changes.

Jordan H
  • 52,571
  • 37
  • 201
  • 351
1

I worked this out after a few days. In my custom cell I had a textView, when I was adding it to the contentView I was doing this:

[self.cellTextView setClearsOnInsertion:YES];

This was the cause of the issue; incase anyone else has a similar problem.

Happy coding :-)

CW0007007
  • 5,681
  • 4
  • 26
  • 31
1

Well, I just used the better part of a day trying to figure this out, so hopefully this alternative explanation saves somebody else some time.

I had a tableview which was giving this message sometimes, when loading. It turned out to be caused by KVO notifications for Core Data objects firing while the view was loading. (On observing a change, my controller tried called reloadData on the tableview in question. Fixed by not observing the objects until the view had finished loading (previously I started observing the object once it was assigned via the accessor)

TLDR: Check to see if you might be trying to reload your data from something other than the main thread.

EVEN BETTER:

This is a problem that you won't run into if you (as I've discovered you really should) use child managed object contexts and only merge changes back into your main MOC on the main thread at sensible times.

Jonathan Zhan
  • 1,883
  • 1
  • 14
  • 17
1

If you are using the cell for the header view section header, you have to put it into the container and then return the container view to prevent "no index path for table cell being reused". The sample code as below.

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    CustomerCellHeader *headerViewCell = [tableView dequeueReusableCellWithIdentifier:kCustomerCellHeaderIdentifier];
    headerView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    [headerView addSubview:headerViewCell];
    return headerView;
}
pkamb
  • 33,281
  • 23
  • 160
  • 191
Kit
  • 2,370
  • 13
  • 11
0

I had issues with returning a UITableViewCell in tableView:viewForHeaderInSection, but returning [cell contentView] was causing any buttons within the cell to crash, and setting a view wrapper around the cell seemed wasteful. Instead, I changed my UITableViewCell class to a UITableViewHeaderFooterView and it worked like a charm!

If you get this issue:

Setting the background color on UITableViewHeaderFooterView has been deprecated. Please use contentView.backgroundColor instead.

Just remember to change the background color on your nib to default.

Ben Chen
  • 71
  • 1
  • 3
0

I had the same issue. This message appears when I scroll and cells are reused.

A lot answers talked about viewForHeaderInSection but I didn't use them. In case someone has the similar problem, I hereby show my solution.

I added observer to the textfields inside each cell. I wonder whether this is the observer that caused this issue as I didn't remove the observers when a cell is recycled.

So I removed the observer when a cell is deinited.

It seems the problem is solved. I cannot guarantee.

Clarke Xun Gong
  • 113
  • 2
  • 5
0

It helped me in swift 2.1

  func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView {
            let headerCell:SBCollapsableTableViewHeader = self.friendsInfoTableView.dequeueReusableCellWithIdentifier("HeaderCell") as! SBCollapsableTableViewHeader
            let containerView = UIView(frame:headerCell.frame)
            headerCell.delegate = self
            headerCell.titleLabel.font = UIFont.boldSystemFontOfSize(15)
            headerCell.titleLabel.text = "\(self.ObjectArray[section].sectioName)"
            headerCell.collapsedIndicatorLabel.text = collapsed ? "+" : "-"
            headerCell.tag = section
            headerCell.backgroundColor =  UIColor(red:72/255,green:141/255,blue:200/255,alpha:0.9)
            containerView.addSubview(headerCell)
            return containerView
    }
Jitendra
  • 842
  • 1
  • 9
  • 25
0

I received the same error, but for a different reason.

I had a custom UITableViewCell that set one of the UITextField's to becomeFirstResponder. The error occurred when more than one instance of this custom cell was being returned.

I simply set only the first instance of the custom cell to becomeFirstResponder and it resolved the issue.

GrindItOut
  • 181
  • 3
0

In my case (pretty stupid one), I used the if let syntax inside cellForRowAt like that:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if let cell = tableView.dequeueReusableCell(withIdentifier: "carCell", for:
    indexPath) as? carTableViewCell {
        // Did some stuff here
        return CarTableViewCell()
    else {
        return CarTableViewCell()    
    }
}

As you may see, I returned i generic carTableViewCell(), and that's what gave me that despicable error... I hope it will ever help someone haha (:

Happy Coding!

Gal Shahar
  • 2,695
  • 1
  • 21
  • 29
-1

can you try the following (provided you do not have any specific configuration for a cell at that indexpath) :-

CustomClassName *cell=[tableView dequeueReusableCellWithIdentifier:[self reuseIdentifier]];
if(cell==nil)
{
cell=[[CustomClassName alloc] initWithStyle:whatever_style reuseIdentifer:[self reuseIdentifier]];
}

Please also post what error you are getting if the above does not work.

Also have a look at the following link:-

What is the meaning of the "no index path for table cell being reused" message in iOS 6/7?

Community
  • 1
  • 1
Max
  • 4,067
  • 1
  • 18
  • 29