[英]How to set the full width of separator in UITableView
我有一個UITableView
,其中分隔符沒有全寬。 它在左側前 10 個像素處結束。 我在viewDidLoad()
玩弄這段代碼。
self.tableView.layoutMargins = UIEdgeInsetsZero;
同樣在故事板中,您可以選擇自定義或默認選擇器。 現在填充的所有單元格都沒有全寬選擇器,但空的單元格具有全寬。
我怎樣才能解決這個問題?
這在使用 Xcode 6.4 和 Swift 1.2 的 iOS 8.4 - 9.0 設備上對我有用:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = UITableViewCell()
cell.preservesSuperviewLayoutMargins = false
cell.separatorInset = UIEdgeInsetsZero
cell.layoutMargins = UIEdgeInsetsZero
return cell
}
斯威夫特 5 更新:
cell.preservesSuperviewLayoutMargins = false
cell.separatorInset = UIEdgeInsets.zero
cell.layoutMargins = UIEdgeInsets.zero
我從這篇文章中得到了答案: iOS 8 UITableView separator inset 0 not working
只需在您的UITableViewController
上添加此代碼
-(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];
}
}
-(void)viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];
if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
[self.tableView setSeparatorInset:UIEdgeInsetsZero];
}
if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
[self.tableView setLayoutMargins:UIEdgeInsetsZero];
}
}
對於 Swift 3 :
override func viewDidLoad() {
super.viewDidLoad()
tableView.separatorInset = .zero
tableView.layoutMargins = .zero
}
我從UITableViewController
繼承,並且除了willDisplayCell
的兩個 insets 設置willDisplayCell
還需要將preservesSuperviewLayoutMargins
設置為 false。 在 Swift 中,它看起來像這樣:
override func tableView(_tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
if cell.respondsToSelector("setSeparatorInset:") {
cell.separatorInset = UIEdgeInsetsZero
}
if cell.respondsToSelector("setLayoutMargins:") {
cell.layoutMargins = UIEdgeInsetsZero
}
if cell.respondsToSelector("setPreservesSuperviewLayoutMargins:") {
cell.preservesSuperviewLayoutMargins = false
}
}
適用於 iOS 9+ 中的 Swift
如果使用自定義UITableViewCell
:
override var layoutMargins: UIEdgeInsets {
get { return UIEdgeInsetsZero }
set(newVal) {}
}
然后對於viewDidLoad
的UITableView
:
self.tableView?.separatorInset = UIEdgeInsetsZero;
self.tableView?.layoutMargins = UIEdgeInsetsZero;
在cellForRowAtIndexPath
方法中使用它,配置單元格的分隔符規格,
它適用於 iOS9.0+
cell.separatorInset = UIEdgeInsetsZero;
cell.layoutMargins = UIEdgeInsetsZero;
cell.preservesSuperviewLayoutMargins = NO;
對於 iPad 有問題的人——這會讓你處於與 iPhone 相同的狀態。 然后,您可以根據需要調整separatorInset
。
tableView.cellLayoutMarginsFollowReadableWidth = false
已針對 iOS 9.3 和 Swift 2.2 進行測試。 確保將代碼放在willDisplayCell
,該代碼在顯示單元格之前被調用,而不是放在僅創建單元格的cellForRowAtIndexPath
中。
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
cell.separatorInset = UIEdgeInsetsZero
cell.layoutMargins = UIEdgeInsetsZero
}
為UITableViewController
的函數添加override
,如下所示:
override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
快速 5,Xcode 11 更新
把它放在 viewDidLoad() 里面
yourTableView.separatorInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
對於邊到邊分隔符,只需將 left 和 right 的值設置為 0。
順便將“yourTableView”更改為您的tableView Outlet 名稱。
以上在 Swift 2.2 和 Xcode 7.3.1 中都不適合我
結果證明這是最簡單的解決方案。 不需要代碼。 只需在UITableView
檢查器中更改TableViewCell
Layout Margins 值:
對於 Swift 3 :
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
if cell.responds(to: #selector(setter: UITableViewCell.separatorInset)) {
cell.separatorInset = UIEdgeInsets.zero
}
if cell.responds(to: #selector(setter: UITableViewCell.layoutMargins)) {
cell.layoutMargins = UIEdgeInsets.zero
}
if cell.responds(to: #selector(setter: UITableViewCell.preservesSuperviewLayoutMargins)) {
cell.preservesSuperviewLayoutMargins = false
}
}
在viewDidLoad
(測試 iOS11 - swift 4.1)
嘗試
tableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0)
這些解決方案都不適用於 iPad,但我提出了一個涵蓋兩種設備的解決方案:
使用可重復使用的細胞:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
...[other code]...
[cell setLayoutMargins:UIEdgeInsetsZero];
[cell setSeparatorInset:UIEdgeInsetsZero];
return cell;
}
對於不可重復使用的單元格:
- (void)removeSeparatorInset:(UITableView*)tableView{
NSArray *cells = [tableView visibleCells];
for (UITableViewCell *cell in cells){
[cell setLayoutMargins:UIEdgeInsetsZero];
[cell setSeparatorInset:UIEdgeInsetsZero];
}
}
-(void) viewDidLayoutSubviews{
[super viewDidLayoutSubviews];
[self removeSeparatorInset:self.tableView];
}
只是為了擴展這種方法:
@property(nonatomic) UIEdgeInsets separatorInset;
@property(nonatomic) UIEdgeInsets layoutMargins;
UITableView
和UITableViewCell
可以使用這兩個屬性。 后者實際上是UIView
一個屬性,它是UITableView
和UITableViewCell
的父類。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.