Note that there are some explanatory texts on larger screens.

plurals
  1. POReloading custom UITableViewCell after reordering cells
    text
    copied!<p>I have <code>UITableView</code> which uses custom <code>UITableViewCell</code>s. The cells can have one of three types of background images (set in each cell's <code>.backgroundView.image</code> property): top, middle, or bottom. The top and bottom images are for the first and last cells, and have rounded corners (much like a grouped <code>UITableView</code>'s cells). All other "middle" cells within the <code>UITableView</code> have a rectangular middle background image.</p> <p>My problem is that when reordering the cells in the <code>UITableView</code>'s <strong>Edit</strong> mode the cells do not refresh with a different background image depending on their new location. For example, if I move the first cell into the middle of the table it still retains its original "top" background image (with rounded corners) and the new cell which appears at the top of the table view still has its original "middle" background image, which all looks a bit odd.</p> <p>I can get around this by doing a <code>reloadData</code> on the table view, but this has the problem of not giving a nice graceful animation of the changes. I noticed that when reordering a standard grouped <code>UITableView</code> as soon as a cell is moved / dragged the background image on the relevant cells change (even before the moved cell has been dropped into its new location) which looks very nice. I would like to achieve the same thing.</p> <p>To this end, I have implemented <code>tableView:targetIndexPathForMoveFromRowAtIndexPath:toProposedIndexPath</code> which is called every time a row is dragged around in the table view. Within this I have tried various methods of setting the <code>backgroundView.image</code> including just directly setting the image and then also explicitly telling it to redraw the cell and/or image using <code>setNeedsLayout</code> and <code>setNeedsDisplay</code> but nothing seems to make the cell redraw. I have <code>NSLog</code>'ged the results of these changes and they appear to be committing to the cell, as the correct values appear in the <code>NSLog</code>, but the cell just does not seem to be updating on screen.</p> <p>If anyone could point out what I am doing wrong, or suggest a better way of achieving a working outcome, I would be very much appreciative. Thanks!</p> <p>EDIT: the following code was pulled from the bounty and formatted so it can be more easily digested:</p> <pre><code>UIImage *rowBackground; UIImage *selectionBackground; NSInteger sectionRows = [_tableView numberOfRowsInSection:[indexPath section]]; NSInteger row = [indexPath row]; if (row == 0 &amp;&amp; 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]; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload