讓分割線左對齊
在iOS8之后,UITableView的分割線距離左邊會有一段距離;我們這里就讓這段距離消失七问。
首先在iOS7里面讓分割線左對齊只要一句代碼就好了:
[self.tableView setSeparatorInset:UIEdgeInsetsZero];
但是在iOS8之后這樣只能縮進(jìn)1/2左右,還是不能完全左對齊。下面我就不多說了直接上代碼吧沟娱!
#pragma <設(shè)置cell分割線>
// 讓分割線左對齊
-(void)viewWillLayoutSubviews{
if ([self.tableView respondsToSelector:@selector(setSeparatorStyle:)]) {
[self.tableView setSeparatorInset:UIEdgeInsetsZero];
}
if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
[self.tableView setLayoutMargins:UIEdgeInsetsZero];
}
}
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
[cell setSeparatorInset:UIEdgeInsetsZero];
}
if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
[cell setLayoutMargins:UIEdgeInsetsZero];
}
}
如何用的純代碼編寫的可能會不能實現(xiàn),那怎么辦巴蠊瘛济似??別急盏缤,看下面:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *ID =@"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
if (!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
}
// cell顏色
cell.backgroundColor = [UIColor colorWithRed:0.194 green:0.8828 blue:1.0 alpha:1.0];
cell.textLabel.text = [NSString stringWithFormat:@"%@==%zd",[self class],indexPath.row];
[self.tableView setLayoutMargins:UIEdgeInsetsZero];
[self.tableView setSeparatorInset:UIEdgeInsetsZero];
[cell setSeparatorInset:UIEdgeInsetsZero];
[cell setLayoutMargins:UIEdgeInsetsZero];
return cell;
}
這樣子的話,分割線就左對齊了砰蠢;
順便分享點小技巧
當(dāng)我們要顯示固定的cell的行數(shù)又想下面的cell的分割線隱藏怎么辦?唉铜?當(dāng)然可以自定義了台舱,這里有個小技巧:直接設(shè)置footView就好了:
// 隱藏所以分割線
//tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
// 隱藏不需要的分割線
self.tableView.tableFooterView = [[UIView alloc]init];
還一點問題就是:
iOS8之后模擬器上面cell的分割線要拖動才能若隱若現(xiàn)看見,不拖動是不能看見的潭流。這個原因是這樣子的竞惋。因為5s,6,6s都到@x2 的屏幕由(2x2)4個像素點構(gòu)成;而plus是(3x3)9個像素點構(gòu)成灰嫉;而在模擬器上只顯示一個像素點拆宛;這么就解釋了為什么若隱若現(xiàn)了。但是在真機上不會有問題的讼撒,只是在模擬器上存在這個問題而已浑厚。