No index path for table cell being reused

前端 未结 13 1499
执念已碎
执念已碎 2021-02-04 05:18

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

Initialis

相关标签:
13条回答
  • 2021-02-04 06:02

    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!

    0 讨论(0)
  • 2021-02-04 06:02

    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?

    0 讨论(0)
  • 2021-02-04 06:10

    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
        }
    
    0 讨论(0)
  • 2021-02-04 06:14

    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.

    0 讨论(0)
  • 2021-02-04 06:15

    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.

    0 讨论(0)
  • 2021-02-04 06:15

    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.

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