歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> UITableViewCell - UITableView中cell的邊框和背景

UITableViewCell - UITableView中cell的邊框和背景

日期:2017/3/1 10:29:03   编辑:Linux編程
UITableView是iOS開發中最常用的元素,在平常用的iPhone App中大部分都用到了UITableView,所以你應該知道她的強大了。

需求很簡單,就是在一個UITableView裡面實現一個不一樣的UITableViewCell,如下圖裡的“切換賬號”按鈕


正常情況下grouped樣式(UITableViewStyleGrouped)UITableViewCell都是有邊框的,所以如果只是用addSubView添加一個按鈕的話,就會有邊框在外面,不符合有要求,也想過用一個大的圖片,把這個cell給蓋住,但是感覺這方案不夠好,早上找Qzone項目組的同是問了下,他們是用的一個Plain樣式的,那些圓角是用的圖片,感覺還是不夠好,更優美的方案應該是:

  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。可以在

  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 { ... }中進行設置。

Copyright © Linux教程網 All Rights Reserved