I have a UITableView that uses custom UITableViewCell s. Cells can have one of three types of background images (set in each property of the .backgroundView.image cell): top, middle, or bottom. The top and bottom images are for the first and last cells and have rounded corners (like grouped UITableView cells). All other “middle” cells inside the UITableView have a rectangular middle background image.
My problem is that when reordering cells in UITableView mode, the Edit cells do not refresh with a different background image depending on their new location. For example, if I move the first cell to the middle of the table, it still retains its original “top” background image (with rounded corners), and the new cell that appears at the top of the table still has its original “middle”, a background image that looks a little weird.
I can get around this by doing reloadData in the table view, but this is not the problem of giving a nice graceful animation of the changes. I noticed that when reordering a standard grouped UITableView , as soon as the cell is moved / dragged, the background image on the corresponding cells changes (even before the moved cell has been reset to a new location), which looks very nice. I would like to achieve the same.
To this end, I implemented tableView:targetIndexPathForMoveFromRowAtIndexPath:toProposedIndexPath , which is called every time the row moves in the form of a table. As part of this, I tried various methods for setting backgroundView.image , including directly setting the image, and then also explicitly telling it to redraw the cell and / or image using setNeedsLayout and setNeedsDisplay , but it doesn't seem to redraw the cell. I have NSLog 'the results of these changes, and they seem to fix the cell since the correct values appear in the NSLog , but the cell just does not update on the screen.
If someone can indicate what I am doing wrong, or suggest a better way to achieve a working result, I would be very grateful. Thanks!
EDIT: The following code has been pulled out of generosity and formatted so that it can be easily learned:
UIImage *rowBackground; UIImage *selectionBackground; NSInteger sectionRows = [_tableView numberOfRowsInSection:[indexPath section]]; NSInteger row = [indexPath row]; if (row == 0 && row == sectionRows - 1) { rowBackground = [UIImage imageNamed:@"field_title_bkg.png"]; selectionBackground = [UIImage imageNamed:@"field_title_bkg.png"]; } else if (row == 0) { rowBackground = [UIImage imageNamed:@"table_row_top_bkg.png"]; selectionBackground = [UIImage imageNamed:@"table_row_top_bkg.png"]; } else if (row == sectionRows - 1) { rowBackground = [UIImage imageNamed:@"table_bottom_row_bkg.png"]; selectionBackground = [UIImage imageNamed:@"table_bottom_row_bkg.png"]; } else { rowBackground = [UIImage imageNamed:@"table_row_bkg.png"]; selectionBackground = [UIImage imageNamed:@"table_row_bkg.png"]; } UIImageView *rowBackGroundImageView = [[UIImageView alloc] initWithImage:rowBackground]; UIImageView *rowBackGroundSelectionImageView = [[UIImageView alloc] initWithImage:selectionBackground]; if (row != sectionRows - 1) { UIImageView *sep = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"cell_separator.png"]]; [sep setFrame:CGRectMake(20, 56, 280, 1)]; [rowBackGroundImageView addSubview:sep]; } [cell setBackgroundView:rowBackGroundImageView]; [rowBackGroundImageView release];
iphone cocoa-touch uikit uitableview
Skoota
source share