iOS 7下
想要設(shè)置cell的分割線縮進(jìn)為0,在iOS7中只用簡單的設(shè)置cell的separatorInset = UIEdgeInsetZero;
iOS 8下
在iOS8下,上面的方法就不行啦,經(jīng)過查閱資料, 終于在stackoverflow上查到了詳細(xì)的說明,源地址戳這里stackoverflow;
This property isn't available on iOS 7.0 so you need to make sure you check before assigning it!
Additionally, Apple has added a property to your cell that will prevent it from inheriting your Table View's margin settings. When this property is set, your cells are allowed to configure their own margins independently of the table view. Think of it as an override.
This property is called preservesSuperviewLayoutMargins, and setting it to NO will allow the cell's layoutMargin setting to override whatever layoutMargin is set on your TableView. It both saves time (you don't have to modify the Table View's settings), and is more concise. Please refer to Mike Abdullah's answer for a detailed explanation.
什么意思呢,就讓我這個(gè)英語四級(jí)的戰(zhàn)五渣來試著翻譯一下吧.
iOS8中,新加入了一個(gè)屬性:preservesSuperviewLayoutMargins,吶,這個(gè)屬性的加入,可以避免你的cell的外邊繼承自你的tableView,當(dāng)你設(shè)置這個(gè)屬性的時(shí)候,你可以自由的設(shè)置你的cell的外邊距,而不必?fù)?dān)心tableView和cell兩者的相互影響.(翻譯的好渣,你們自己去看英文吧...)
具體怎么代碼實(shí)現(xiàn)呢
//Setup your cell margins:
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
// Remove seperator inset
if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
[cell setSeparatorInset:UIEdgeInsetsZero];
}
// Prevent the cell from inheriting the Table View's margin settings
if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
[cell setPreservesSuperviewLayoutMargins:NO];
}
// Explictly set your cell's layout margins
if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
[cell setLayoutMargins:UIEdgeInsetsZero];
}
}
OVER.