简体   繁体   中英

How can I highlight a UITableViewCell

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 CM

只需将突出显示/背景更改/任何代码放入:

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

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.

尝试这个:

[tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
- (void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
      if (//tell if cell is marked) {
            [cell setBackgroundColor:[UIColor colorWithRed:158.0/255.0 green:28.0/255.0 blue:36.0/255.0 alpha:1.0]];
            [[cell textLabel] setTextColor:[UIColor whiteColor]];
            if ([cell detailTextLabel]) {
                  [[cell detailTextLabel] setTextColor:[UIColor whiteColor]];
            }
      }
      else
      {
            [cell setBackgroundColor:[UIColor whiteColor]];
            [[cell textLabel] setTextColor:[UIColor blackColor]];
            if ([cell detailTextLabel]) {
                  [[cell detailTextLabel] setTextColor:[UIColor blackColor]];
            }
      }
}

use this in the uitableviewdelegate and then when you want to highlight the cell mark the cell then call reloadData on the tableview

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

I've got a variation on this that is continuing to stump me. I have a wizard that uses tables to indicate possible selections. Once a selection is made, I'd like to temporarily highlight the selected row, then advance to the next frame in the wizard.

I do the advancement in

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

But I can't make the highlight happen.

Any suggestions?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM