升級到XCode6後,iOS8裡設定tableview的setSeparatorInset:UIEdgeInsetsZero不起作用的解決辦法

來源:互聯網
上載者:User

標籤:

首先我們拖拽一個預設的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不起作用的解決辦法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.