Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 10-25-2009, 01:25 PM   #1 (permalink)
Registered Member
 
Join Date: Jun 2009
Posts: 25
eatenbyrats is on a distinguished road
Default Force UITableView re-render after row re-order

I'm trying to implement a TableView that has alternating backgrounds on rows to help readability, but I'm having difficulty getting rendering subsequent to re-ordering working.

Re-drawing after deletion works without any problems, but I get unpredictable results when I call tableView:reloadRowsAtIndexPaths:withRowAnimation: in the implementation of tableView:moveRowAtIndexPath:toIndexPath:

Code:
// Create table cells with alternating row appearance
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	
  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellID];
  
  if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellID] autorelease];
		
    extraneous detail removed...
  } 
  
  BOOL oddRow = (indexPath.row % 2 == 0);
  NSString *backgroundImagePath = [[NSBundle mainBundle] pathForResource:oddRow ? @"oddbkd" : @"evenbkd" ofType:@"png"];
  UIImage *backgroundImage = [[UIImage imageWithContentsOfFile:backgroundImagePath] stretchableImageWithLeftCapWidth:0.0 topCapHeight:1.0];
  cell.backgroundView = [[[UIImageView alloc] initWithImage:backgroundImage] autorelease];
  cell.backgroundView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  cell.backgroundView.frame = cell.bounds;    
	
  return cell;
}

// Delete rows
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
  
  if (editingStyle == UITableViewCellEditingStyleDelete) {		
    [testData removeObjectAtIndex:indexPath.row];
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
  } 
  
  // Reload visible cells to force a redraw
  [tableView reloadRowsAtIndexPaths:[tableView indexPathsForVisibleRows] withRowAnimation:UITableViewRowAnimationNone];
}


// Rearrange rows
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
	
  id tempObject = [[testData objectAtIndex:sourceIndexPath.row] retain];
	
  [testData removeObjectAtIndex:sourceIndexPath.row];
  [testData insertObject:tempObject atIndex:destinationIndexPath.row];
  
  [tempObject release];

  // Blank cells, stack dumps, exceptions from nested setEdit:withAnimation: calls...
  [tableView reloadRowsAtIndexPaths:[tableView indexPathsForVisibleRows] withRowAnimation:UITableViewRowAnimationNone];
}
Any suggestions appreciated- I've tried telling the UITableViewCell.view itself to redraw and reloading the whole table (not exactly desirable) without success.
eatenbyrats is offline   Reply With Quote
Old 11-06-2009, 03:14 AM   #2 (permalink)
Beast Mode
 
Join Date: Dec 2008
Age: 21
Posts: 1,971
Bertrand21 is on a distinguished road
Default

was that really necessary?
__________________
Haters gonna Hate
Likers gonna Like
Bertrand21 is offline   Reply With Quote
Old 06-04-2010, 06:32 PM   #3 (permalink)
Registered Member
 
Join Date: Jun 2010
Posts: 1
lgxxl is on a distinguished road
Lightbulb SOLVED: Cause is an Animation Overlap

The code you describe has the same error inside as mine had. The problematic thing is, that this piece of code

Quote:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
launches its own animation though you use UITableViewRowAnimationNone it still kicks of an animation. So, any deletion or insertion sports an animation (which is fine) but you have to wait until they are finished before touching the cells again with some reload. I worked around this by kicking of a timer which delays my message.

That solved it for me!

regards,
Helge

Quote:
Originally Posted by eatenbyrats View Post
I'm trying to implement a TableView that has alternating backgrounds on rows to help readability, but I'm having difficulty getting rendering subsequent to re-ordering working.

Re-drawing after deletion works without any problems, but I get unpredictable results when I call tableView:reloadRowsAtIndexPaths:withRowAnimation: in the implementation of tableView:moveRowAtIndexPath:toIndexPath:

Code:
// Create table cells with alternating row appearance
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	
  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellID];
  
  if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellID] autorelease];
		
    extraneous detail removed...
  } 
  
  BOOL oddRow = (indexPath.row % 2 == 0);
  NSString *backgroundImagePath = [[NSBundle mainBundle] pathForResource:oddRow ? @"oddbkd" : @"evenbkd" ofType:@"png"];
  UIImage *backgroundImage = [[UIImage imageWithContentsOfFile:backgroundImagePath] stretchableImageWithLeftCapWidth:0.0 topCapHeight:1.0];
  cell.backgroundView = [[[UIImageView alloc] initWithImage:backgroundImage] autorelease];
  cell.backgroundView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  cell.backgroundView.frame = cell.bounds;    
	
  return cell;
}

// Delete rows
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
  
  if (editingStyle == UITableViewCellEditingStyleDelete) {		
    [testData removeObjectAtIndex:indexPath.row];
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
  } 
  
  // Reload visible cells to force a redraw
  [tableView reloadRowsAtIndexPaths:[tableView indexPathsForVisibleRows] withRowAnimation:UITableViewRowAnimationNone];
}


// Rearrange rows
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
	
  id tempObject = [[testData objectAtIndex:sourceIndexPath.row] retain];
	
  [testData removeObjectAtIndex:sourceIndexPath.row];
  [testData insertObject:tempObject atIndex:destinationIndexPath.row];
  
  [tempObject release];

  // Blank cells, stack dumps, exceptions from nested setEdit:withAnimation: calls...
  [tableView reloadRowsAtIndexPaths:[tableView indexPathsForVisibleRows] withRowAnimation:UITableViewRowAnimationNone];
}
Any suggestions appreciated- I've tried telling the UITableViewCell.view itself to redraw and reloading the whole table (not exactly desirable) without success.
lgxxl is offline   Reply With Quote
Reply

Bookmarks

Tags
uitableview, uitableviewcell

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 344
10 members and 334 guests
alexP, ClerurcifeDer, givensur, glenn_sayers, guusleijsten, ipodphone, JmayLive, Punkjumper, whitey99, yys
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,649
Threads: 94,114
Posts: 402,883
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Anwerbl
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 09:39 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0