tags:

views:

1707

answers:

4

I'd like very much to temporarily highlight a UITableViewCell to draw attention to the fact that the containing data has changed.

there is a UITableViewCell method: -setHighlighted:(BOOL) animated:(BOOL) but I can't make it do anything?

Here is the relevant part of the view controller:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  UITableViewCell *cell;
  switch (indexPath.section) {
    case 0: {
     if (indexPath.row == 0) {
    cell = [[UITableViewCell alloc ] initWithStyle:UITableViewCellStyleDefault 
                                   reuseIdentifier:@"currentLoc"];
    cell.accessoryType = UITableViewCellAccessoryNone;
      cell.textLabel.text = @"This is the special cell";
    }
    [cell setHighlighted:YES animated:YES];
    [cell setHighlighted:NO animated:YES];

    //[cell setNeedsDisplay];

  } else {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                   reuseIdentifier:@"setLocAutomatically"];
    cell.accessoryType = UITableViewCellAccessoryNone;
    cell.textLabel.text = @"Foo";

  }
} break;
case 1: {
  cell = [[UITableViewCell alloc ] initWithStyle:UITableViewCellStyleDefault
                                 reuseIdentifier:@"selectCity"];
  cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  if (indexPath.row == 0) {
    cell.textLabel.text = @"Select a Bar";
  } else {
    cell.textLabel.text = @"Select a Baz";
  }
} break;
  }
  [cell autorelease];
  return cell;
}

See the [cell setHighlight...] above for my attempt.

Much to my frustration, the cell doesn't highlight and I haven't figured out any way to make it work.

Thank you,

Carl C-M

+3  A: 

Because you're doing all this in tableView:cellForRowAtIndexPath:, nothing happens to the cell on-screen until after you return the cell. Therefore, setting the cell to be highlighted does nothing visible to the user.

What you want to do is figure out a way to set the cell as highlighted after you return it from the method. Perhaps looking into NSObject's performSelector:withObject:afterDelay: method might do you some good - you could set the cell as highlighted with a delay, so it gets returned, displayed, then highlighted.

Tim
A: 

Have you thought of altering the UILabel for the cell and simply changing the appearance rather than trying to alter the cell itself. Possible a different text color or bold?

That would allow you to keep the changes within tableView:cellForRowAtIndexPath:

This also ensures the the default appearance of highlighting isn't mixed with your additional message of changed information. Highlighting can mean a lot of things, but text formatting can be used to indicate your specific concept.

UILabel * textLabel = [yourCell textLabel];
// From here you can alter the text

http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UILabel_Class/Reference/UILabel.html

Paulo
A: 

try using the [tblView selectRowAtIndexPath:myIndex animated:YES scrollPosition:UITableViewScrollPositionTop]; function.

Nareshkumar
A: 

just put the highlighting / background changing / whatever code in:

  • (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
Eddie