No index path for table cell being reused
Asked Answered
M

13

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";
}
Miscarry answered 28/8, 2013 at 14:22 Comment(6)
What started happening out of the blue?Predominate
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.Miscarry
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.Besot
You should post your whole cellForRowAtIndexPath method. How do you know the indexPath isn't being reused?Predominate
Hi, the title is being printed in the console. This is my cellForRow:Miscarry
Have edited. Symptoms are; eratic cell heights, scrolling stops randomly. cell titles re-arrange. THis stops once all the cells have been drawn.Miscarry
M
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 :-)

Miscarry answered 3/9, 2013 at 9:53 Comment(2)
Could anyone explain why this causes the issue ?Sip
I suspect it's to do with the dequeueing of table view cells. I can't be sure though.Miscarry
O
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.

Outing answered 23/1, 2014 at 20:57 Comment(7)
This solved it for me. In my case it was the viewForFooterInSection method. Thank you!Ceraceous
I can confirm that using doing a long press on this section causes the app to crashTowandatoward
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.Towandatoward
Code:UILongPressGestureRecognizer *touchDown = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(didTouchDownPress:)]; touchDown.minimumPressDuration = .001; [headerView.contentView addGestureRecognizer:touchDown];Towandatoward
This allow me to highlight everything on state Began and process the action on state endedTowandatoward
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?Withrow
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.Withrow
C
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
Crossroad answered 2/11, 2015 at 11:13 Comment(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.Sirius
B
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

Budd answered 26/9, 2013 at 16:1 Comment(2)
So what did you do to fix it?Hormone
I stopped using a UITableViewCell as a normal view - ie where I needed to create a normal view I used UIView instead of UITableViewCell.Budd
P
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.

Prot answered 8/8, 2014 at 18:46 Comment(0)
M
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 :-)

Miscarry answered 3/9, 2013 at 9:53 Comment(2)
Could anyone explain why this causes the issue ?Sip
I suspect it's to do with the dequeueing of table view cells. I can't be sure though.Miscarry
W
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.

Windtight answered 9/4, 2014 at 11:17 Comment(0)
L
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;
}
Loquat answered 11/5, 2015 at 4:47 Comment(0)
H
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.

Hylozoism answered 15/12, 2014 at 1:54 Comment(0)
D
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.

Ducktail answered 10/7, 2015 at 0:12 Comment(0)
M
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
    }
Mechanician answered 2/1, 2016 at 6:58 Comment(0)
A
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.

Adriene answered 8/5, 2016 at 19:28 Comment(0)
I
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!

Impale answered 30/7, 2017 at 20:12 Comment(0)
M
-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?

Maighdiln answered 28/8, 2013 at 14:49 Comment(1)
INternal bug doesn't seem to be a good answer... Doesn't do it with other tableviews.Miscarry

© 2022 - 2024 — McMap. All rights reserved.