標籤:
首先我們拖拽一個預設的tableview 控制項! 看下xcode5 面板的inspector(檢查器)
我們可以找到一個 Separator Insetss 標籤 預設是 Default
我們選擇一下 發現有個Custom 這時候我們驚奇的發現Left 15 ,這時候我們只要把這個 15 改成 0 , 然後儲存, 你就會發現tableview 的分割線跟以前一樣了。
有些朋友問了如果是代碼寫的tableview 的呢。
下面我們接著分析這個問題,讓我們查詢下 tableview delegate 我們會發現 ios7 增加了一些新屬性,
@property (nonatomic) UIEdgeInsets separatorInset NS_AVAILABLE_IOS(7_0)UI_APPEARANCE_SELECTOR;// allows customization of the frame of cell separators
這個時候你應改發現separatorInset 這個單詞是否有點眼熟, 蘋果公司已經給了注釋,可以自訂視cell 的分割線,
UIEdgeInsets 是個結構體類型,這時候我們發現了我們要的屬性 left
typedef struct UIEdgeInsets {
CGFloat top, left, bottom, right; // specify amount to inset (positive) for each of the edges. values can be negative to ‘outset‘
} UIEdgeInsets;
我們在使用tableview時會發現分割線的左邊會短一些,通常可以使用 setSeparatorInset:UIEdgeInsetsZero 來解決。但是升級到XCode6之後,在iOS8裡發現沒有效果。下面給出解決辦法:
首先在viewDidLoad方法中加上如下代碼:
if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
[self.tableView setSeparatorInset:UIEdgeInsetsZero];
}
if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
[self.tableView setLayoutMargins:UIEdgeInsetsZero];
}
然後在willDisplayCell方法中加入如下代碼:
- (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];
}
}
這樣就可以正常顯示了。
升級到XCode6後,iOS8裡設定tableview的setSeparatorInset:UIEdgeInsetsZero不起作用的解決辦法