uitable裡的分割線,uitable分割線
到了iOS8上,發現uitable是越來越不會用了;不說了,先看一下截屏效果:
設計期望的效果是:
1,自訂一個單元格,背景是黃色的;期望鋪滿整個表格單元;
2,單元分割線是貫通;
現實與理想的差別如下:
1,黃顏色單元格沒能橫向鋪滿整個單元;
2,分割線右側沒有拉到頭;
3,我只花了三個單元格;沒有充滿的空間,OS裡也畫上了分割線。
這是我的解決方案:
解決問題1的方式是:
設定table的邊距:[tableView setLayoutMargins:UIEdgeInsetsMake(0, 0, 0, 0)];
這是iOS8上新出的特性,所以這個問題在iOS7上沒有的。
解決問題2和3的方式是:
第一步:設定統一設定表格的分割線留空 [tableView setSeparatorInset:UIEdgeInsetsMake(0, 1000, 0, 0)];
這裡設定分割線橫行留空1000個像素,這個的目的是為了讓所有的分割線畫到視圖之外,這樣子可以讓iOS自動補上的分割線都不可見了。
第二步:針對每個單元格設定留空:
[cell setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];
[cell setLayoutMargins:UIEdgeInsetsMake(0, 0, 0, 0)];
這樣可以讓我們需要的單元格分割線回到視圖中。
#import "ViewController.h"#import "DemoCell.h"@interface ViewController (){ IBOutlet UITableView *tableView;}@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. //以下兩行為關鍵代碼<strong> [tableView setLayoutMargins:UIEdgeInsetsMake(0, 0, 0, 0)]; [tableView setSeparatorInset:UIEdgeInsetsMake(0, 1000, 0, 0)];</strong> [tableView setSeparatorColor:[UIColor redColor]];}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return 3;}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ DemoCell *cell = (DemoCell*)[[[NSBundle mainBundle] loadNibNamed:@"DemoCell" owner:self options:nil] lastObject]; //以下兩行為關鍵代碼<strong> [cell setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)]; [cell setLayoutMargins:UIEdgeInsetsMake(0, 0, 0, 0)];</strong> return cell;}@end