2009-06-12 138 views
1

我非常想暂时突出显示UITableViewCell,以引起人们注意包含数据已更改的事实。有一个UITableViewCell方法:-setHighlighted:(布尔)动画:(布尔),但我不能让它做任何事情?如何突出显示UITableViewCell

这里是视图控制器的相关部分:

- (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; 
} 

参见[电池setHighlight ...]以上为我的尝试。

很让我沮丧的是,单元格没有突出显示,我还没有想出任何方法使其工作。

谢谢

卡尔Ç-M

回答

4

因为你做的这一切tableView:cellForRowAtIndexPath:,什么也没发生到屏幕上的细胞,直到后返回小区。因此,设置要突出显示的单元对用户来说没有任何可见的。

你想要做的是找出一种方法来设置单元格从方法返回后突出显示。也许看着NSObject's performSelector:withObject:afterDelay:方法可能会对你有所帮助 - 你可以将单元格设置为突出显示,以便延迟显示,以便返回,显示,然后突出显示。

0

你有没有想过改变单元格的UILabel,只是改变外观而不是试图改变单元格本身。可能是不同的文字颜色或粗体?

这将让你保持内tableView:cellForRowAtIndexPath:

的变化这也保证了高亮的默认外观是不是与你的改变信息的附加信息混合。突出显示可能意味着很多事情,但文本格式可用于指示您的特定概念。

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

0

尝试使用 [tblView selectRowAtIndexPath:myIndex animated:YES scrollPosition:UITableViewScrollPositionTop]; 功能。

16

只是把高亮/背景改变/不管在代码:

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

这个工作对我来说,应该是接受的答案是,imo。 – 2011-01-16 05:22:14

+0

也适用于我 – z22 2014-07-16 09:08:44

0

我有这个正在继续难倒我的变化。我有一个使用表格来指示可能的选择的向导。一旦做出选择,我想暂时突出显示选定的行,然后前进到向导中的下一帧。

我做

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

进步,但我不能让亮点出现。

有什么建议吗?

0

试试这个:在的UITableViewDelegate

[tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone]; 
0
- (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]]; 
      } 
     } 
} 

使用这个,然后当你想要突出的细胞标记,则电池调用reloadData上实现代码如下