Custom UITableViewCell, UITableView and allows you to use MultipleSelectionDuringEditing

I'm having a problem using the new iOS 5 feature to select multiple cells in edit mode. The structure of the application is as follows:

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

where the UIViewController is both the delegate and the data source for the UITableView (instead, I use the UIViewController instead of the UITableViewController and I cannot change it). Cells are loaded into a UITableView as 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 was created from an xib file. In particular, I created a new xib file in which the supervisor consists of a UITableViewCell element. To provide customization, I set the type for this element as CustomUITableViewCell , where CustomUITableViewCell extends UITableViewCell .

 @interface CustomTableViewCell : UITableViewCell // do stuff here @end 

The code works well. The table displays custom cells. Now, while the application is running, I set allowsMultipleSelectionDuringEditing to YES , and I enter edit mode for the UITableView . Seems to work. In fact, an empty circle appears next to each cell. The problem is that when you select a row, an empty circle does not change its state. Theoretically, the circle should change from empty to a red checkmark and vice versa. The circle seems to stay above the contentView for the cell.

I have done many experiments. I also checked that the following method.

 - (NSArray *)indexPathsForSelectedRows 

and it is updated during the selection in edit mode. It displays the selected cells.

What is not so clear to me is that when I try to work without a special cell, only with UITableViewCell , the circle updates its state correctly.

Do you have any suggestions? Thank you in advance.

+4
source share
1 answer

For those interested, I found the right solution to fix the previous problem. The problem is that when you select the UITableViewCellSelectionStyleNone style, the red flags in edit mode are not displayed correctly. The solution is to create custom UITableViewCell and ovverride some methods. I use awakeFromNib because my cell was created via xib. To achieve a solution, I followed these two stackoverflow topics:

Here is 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 this helps.

+4
source

Source: https://habr.com/ru/post/1394251/


All Articles