Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No index path for table cell being reused

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";
}
like image 219
CW0007007 Avatar asked Aug 28 '13 14:08

CW0007007


2 Answers

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.

like image 172
TalkLittle Avatar answered Nov 15 '22 18:11

TalkLittle


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
like image 45
Alessandro Ornano Avatar answered Nov 15 '22 18:11

Alessandro Ornano