UITableViewCell - UITableView中cell的边框和背景

来源:互联网 发布:服务器性能监控 java 编辑:程序博客网 时间:2024/06/06 09:06

UITableView是iOS开发中最常用的元素,在平常用的iPhone App中大部分都用到了UITableView,所以你应该知道她的强大了。

需求很简单,就是在一个UITableView里面实现一个不一样的UITableViewCell,如下图里的“切换账号”按钮


正常情况下grouped样式(UITableViewStyleGrouped)UITableViewCell都是有边框的,所以如果只是用addSubView添加一个按钮的话,就会有边框在外面,不符合有要求,也想过用一个大的图片,把这个cell给盖住,但是感觉这方案不够好,早上找Qzone项目组的同是问了下,他们是用的一个Plain样式的,那些圆角是用的图片,感觉还是不够好,更优美的方案应该是:

[java] view plaincopy
  1. UIView *tempView = [[UIView alloc] init];  
  2. [cell setBackgroundView:tempView];  
  3. [cell setBackgroundColor:[UIColor clearColor]];   
实很简单,把backgroundView设置为一个空的View,然后就干净了。看了下UITableViewCell的文档,backgroundView在plain-style的TableView里面是nil,在grouped-style的TableView里面并不是空的,所以这里人为置空一下就ok了,这是目前为止我见到的最优美的解决方案。


如果要自定义cell的颜色,应该定义cell的contentview的颜色,并将它的上面的子视图的颜色设置为clear。可以在

[java] view plaincopy
  1. (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {   
  2.    if (indexPath.row % 2)  
  3.    {  
  4.         [cell setBackgroundColor:[UIColor colorWithRed:.8 green:.8 blue:1 alpha:1]];  
  5.    }else {  
  6.     [cell setBackgroundColor:[UIColor clearColor]];  
  7.    }  
  8.    cell.textLabel.backgroundColor = [UIColor clearColor];  
  9.    cell.detailTextLabel.backgroundColor = [UIColor clearColor];  
  10. }  

当然也可以在(UITableViewCell *) tableView: (UITableView *) tableView cellForRowAtIndexPath: (NSIndexPath *) indexPath { ... }中进行设置。

 

给UITableView增加一个好看的背景能为应用程序增色不少,并能促进app的销售,但是随便增加一个背景图片会史你的app更加丑陋。

[java] view plaincopy
  1. //This method produces odd artifacts in the background image:  
  2. ATableViewController *yourTableViewController = [[ATableViewController alloc] initWithStyle:UITableViewStyleGrouped];  
  3. yourTableViewController.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"TableViewBackground.png"]];  
这种方法直接设置tableview的背景色,效果不佳。每个cell中的背景是重复的!


正确的方式:在tableview后面放置一个背景视图,并将tableview设为透明色。

[java] view plaincopy
  1. UIView *backgroundView = [[UIView alloc] initWithFrame: window.frame];  
  2. backgroundView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"TableViewBackground.png"]];  
  3. [window addSubview:backgroundView];  
  4.   
  5. yourTableViewController = [[ATableViewController alloc] initWithStyle:UITableViewStyleGrouped];  
  6. yourTableViewController.view.backgroundColor = [UIColor clearColor];  
  7. [window addSubview:yourTableViewController.view];  


这种方法产生的背景在整个表格Cell中是连续的整张图片。

当然,现在是直接在window下增加了背景层,有点不太灵活,在有导航栏的情况下,可以直接更改导航栏的背景,效果是一样的

[java] view plaincopy
  1. self.navigationController.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"wood1.png"]];  


下面在单个的ViewController里更改背景


在用代码产生的tableViewcontroller中,可以通过loadview方法设置

[java] view plaincopy
  1. @interface SomeController : UIViewController <UITableViewDataSource, UITableViewDelegate> {  
  2.   ...  
  3.   UITableView *tableView;  
  4.   ...  
  5. }  
  6. @property (nonatomic, retain) UITableView *tableView;  
  7. @end  
  8.   
  9.   
  10.   
  11. @implementation SomeController  
  12.   
  13. @synthesize tableView;  
  14.   
  15. ...  
  16.   
  17. - (void)loadView {  
  18.     [super loadView];  
  19.     UIImageView *v = [[[UIImageView alloc] initWithFrame:self.view.bounds] autorelease];  
  20.     [v setImage:[UIImage imageNamed:@"table_background.png"]];  
  21.     [self.view addSubview:v];  
  22.   
  23.   
  24.     self.tableView = [[[UITableView alloc] initWithFrame:self.view.bounds] autorelease];  
  25.     [self.tableView setBackgroundColor:[UIColor clearColor]];  
  26.     [self.view addSubview:self.tableView];  
  27. }  
  28. ...  
  29. @end  


原创粉丝点击