天天看點

UITableViewCell的選中時的顔色設定

[cpp]  view plain copy
  1. 1.系統預設的顔色設定  

[cpp]  view plain copy

  1. //無色  
  2. cell.selectionStyle = UITableViewCellSelectionStyleNone;  
  3. //藍色  
  4. cell.selectionStyle = UITableViewCellSelectionStyleBlue;  
  5. //灰色  
  6. cell.selectionStyle = UITableViewCellSelectionStyleGray;  

轉至http://blog.csdn.net/a6472953/article/details/7532212

2.自定義顔色和背景設定

 改變UITableViewCell選中時背景色:

UIColor *color = [[UIColoralloc]initWithRed:0.0green:0.0blue:0.0alpha:1];//通過RGB來定義自己的顔色

[html]  view plain copy

  1. cell.selectedBackgroundView = [[[UIView alloc] initWithFrame:cell.frame] autorelease];  
  2. cell.selectedBackgroundView.backgroundColor = [UIColor xxxxxx];  

3自定義UITableViewCell選中時背景

[html]  view plain copy

  1. cell.selectedBackgroundView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"cellart.png"]] autorelease];   
  2. 還有字型顔色   
  3. cell.textLabel.highlightedTextColor = [UIColor xxxcolor];  [cell.textLabel setTextColor:color];//設定cell的字型的顔色  

4.設定tableViewCell間的分割線的顔色

[theTableView setSeparatorColor:[UIColor xxxx ]];

5、設定cell中字型的顔色

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  if(0 == indexPath.row)
  {
    cell.textLabel.textColor = ...;
    cell.textLabel.highlightedTextColor = ...;
  }
  ...
}      

6.pop傳回table時,cell自動取消選中狀态

首先我有一個UITableViewController,其中每個UITableViewCell點選後都會push另一個ViewController,每次點選Cell的時候,Cell都會被選中,當從push的ViewController傳回的時候選中的Cell便會自動取消選中。後來由于某些原因我把這個UITableViewController改成了UIViewController,之後就産生了一個問題:每次傳回到TableView的時候,之前選中的Cell不能自動取消選中,經過查找得知: UITableViewController有一個clearsSelectionOnViewWillAppear的property, 而當把UITableViewController修改成UIViewController後,這個屬性自然就不存在了,是以我們必須手動添加取消選中的功能,方法很簡單,在viewWillAppear方法中加入: [self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow] animated:YES];    

7.點選後,過段時間cell自動取消選中

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{      …………     //消除cell選擇痕迹     [self performSelector:@selector(deselect) withObject:nil afterDelay:0.5f]; } - (void)deselect {     [self.tableview deselectRowAtIndexPath:[self.tableview indexPathForSelectedRow] animated:YES]; }