Question

I have a problem using iOS 5 new functionality to select multiple cells during editing mode. The application structure is the following:

-> UIViewController
---> UITableView
----> CustomUITableViewCell

where UIViewController is both the delegate and the data source for the UITableView (I'm using an UIViewController instead of UITableViewController for requirement reasons and I cannot change it). Cells are loaded into the UITableView like the following code.

- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CustomTableViewCell *cell = (CustomTableViewCell*)[tv dequeueReusableCellWithIdentifier:kCellTableIdentifier];
    if (cell == nil)
    {
        [[NSBundle mainBundle] loadNibNamed:@"CustomTableViewCellXib" owner:self options:nil];     
        cell = self.customTableViewCellOutlet;    
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }

    // configure the cell with data
    [self configureCell:cell atIndexPath:indexPath];    

    return cell;
}

The cell interface has been created from a xib file. In particular, I've created a new xib file where the superview consists of a UITableViewCell element. To provide customization, I set the type for that element as CustomUITableViewCell, where CustomUITableViewCell extends UITableViewCell.

@interface CustomTableViewCell : UITableViewCell

// do stuff here

@end

The code works well. In the table custom cells are displayed. Now, during application execution I set allowsMultipleSelectionDuringEditing to YES and I enter the edit mode for the UITableView. It seems working. In fact, alongside of each cell an empty circle appears. The problem is that when I select a row the empty circle doesn't change its state. In theory, the circle has to change from empty to red checkmark and viceversa. It seems that circle remains above the contentView for the cell.

I've made a lot of experiments. I've also checked also that following method.

- (NSArray *)indexPathsForSelectedRows

and it gets updated during selection in editing mode. It displays the right selected cells.

The thing that it's not so clear to me is that when I try to work without a custom cell, only with UITableViewCell, the circle updates its state correctly.

Do you have any suggestion? Thank you in advance.

Was it helpful?

Solution

For those interested in, I've found a valid solution to fix the previous problem. The problem is that when selection style is UITableViewCellSelectionStyleNone red checkmarks in editing mode aren't displayed correctly. The solution is to create a custom UITableViewCell and ovverride some methods. I'm using awakeFromNib because my cell has been created through xib. To reach the solution I've followed these two stackoverflow topics:

  1. multi-select-table-view-cell-and-no-selection-style
  2. uitableviewcell-how-to-prevent-blue-selection-background-w-o-borking-isselected

Here the code:

- (void)awakeFromNib
{
    [super awakeFromNib];

    self.backgroundView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"row_normal"]] autorelease];
    self.selectedBackgroundView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"row_selected"]] autorelease];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    if(selected && !self.isEditing)
    {

        return;
    }        

    [super setSelected:selected animated:animated];
}

- (void)setHighlighted: (BOOL)highlighted animated: (BOOL)animated
{
    // don't highlight
}

- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];

}

Hope it helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top