Reputation: 3510
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
Upvotes: 1
Views: 9219
Reputation: 161
just put the highlighting / background changing / whatever code in:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
Upvotes: 16
Reputation: 21
- (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
Upvotes: 0
Reputation: 2804
Try this:
[tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
Upvotes: 0
Reputation: 155
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?
Upvotes: 0
Reputation: 2351
try using the
[tblView selectRowAtIndexPath:myIndex animated:YES scrollPosition:UITableViewScrollPositionTop];
function.
Upvotes: 0
Reputation: 4333
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
Upvotes: 0
Reputation: 60110
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.
Upvotes: 4