2009-09-03 67 views

回答

80

如果您要做的不僅僅是使用separatorColor更改分隔符的顏色在UITableView的財產,那麼你可以在separatorStyle屬性設置爲UITableViewCellSeparatorStyleNone,然後或者:

例如,如果您的表格當前顯示5行,您可以更新它以顯示9行,並且索引1,3,5,7處的行將是分隔符單元格。

請參閱Subclassing UITableViewCellTable View Programming Guide關於如何創建自定義UITableViewCell s的更多信息。

+27

偉大的大NO使用細胞分離器。這只是醜陋的,但UITableViewCell的子類和重寫layoutSubviews是我的建議。 http://stackoverflow.com/questions/12509747/uitableviewcell-spacing-between-cells/17885077#17885077 – 2014-01-16 09:25:46

+0

@CameronLowellPalmer使用單元格作爲分隔符在某些情況下是完美的。事實上,我們只是想要一個3點寬的透明分離器。放入隱形眼鏡後,間隔細胞非常適合! – mbm29414 2014-07-23 19:22:58

4

我不知道這是否可以通過一些設置「自動」完成。但建議將設置行分隔符爲none,並在您的單元格的邊界實際上繪製您想要的行分隔符。

8

您添加的tableView以下代碼cellForRowAtIndexPath委託創建1px的高度的自定義圖像視圖和200像素寬度everyCell

UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 200, self.view.bounds.size.width, 1)]; 
lineView.backgroundColor = [UIColor blackColor]; 
[cell.contentView addSubview:lineView]; 

注意:我不知道它在性能多麼沉重。

+1

不知道它有多大的差異,但我把它放在tableView的willDisplayCell委託中。 – JohnnyRedTruss 2013-02-21 16:44:59

34

更好的解決方案是使用單元格的當前寬度和高度。事情是這樣的:

UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, cell.contentView.frame.size.height - 1.0, cell.contentView.frame.size.width, 1)]; 

lineView.backgroundColor = [UIColor darkGrayColor]; 
[cell.contentView addSubview:lineView]; 
+2

如果您有一個滾動tableview與更大數量的單元格,這將繼續添加子視圖,因爲單元格循環時用戶滾動tableview。不是一個好的解決方案 – Matjan 2016-03-04 19:11:13

+0

-1這個解決方案,因爲我同意上面的評論。如果您有超過10個左右的單元格(並且您正在重複使用/將它們出列),這是一個不好的解決方案,因爲您在一個單元格上使用的分隔線將在該單元格被重用時出現,並且可能不在您想要的位置。 – 2016-04-18 22:26:50

31

如果您需要爲不同行不同的分隔符的顏色或您希望在該行突出了自來水,然後嘗試這個分隔符仍然可見:

self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; 

// We have to use the borderColor/Width as opposed to just setting the 
// backgroundColor else the view becomes transparent and disappears during 
// the cell's selected/highlighted animation 
UIView *separatorView = [[UIView alloc] initWithFrame:CGRectMake(0, 43, 1024, 1)]; 
separatorView.layer.borderColor = [UIColor redColor].CGColor; 
separatorView.layer.borderWidth = 1.0; 
[cell.contentView addSubview:separatorView]; 

這裏假設你的電池的背景顏色是透明的。


上述解決方案出現了一些廣泛的實驗。下面是一些筆記我發現,我敢肯定,將幫助人們:

在正常的「未選中」狀態

  • 的內容查看(什麼在你的XIB除非你編碼它,否則)繪製通常
  • 的selectedBackgroundView是隱藏
  • 的backgroundView是可見的(所以只要你的內容查看是透明的,你看到的backgroundView或者(如果你還沒有定義的backgroundView你會看到的UITableView本身)的背景顏色

被選擇的細胞,以下同-OUT立即發生的任何動畫:

  • 的內容查看內所有視圖/子視圖有其的backgroundColor清除(或設置爲透明的),標籤等的文字顏色的更改爲其所選顏色
  • selectedBackgroundView變爲可見(此視圖始終爲單元格的全尺寸(如果需要,可以忽略自定義框架,使用子視圖)。還要注意,由於某種原因,子視圖的backgroundColor不會顯示,或許它們像contentView一樣是透明的)。如果沒有定義selectedBackgroundView然後可可將創建/插入藍色(或灰色)梯度背景和你顯示此)
  • 的backgroundView不變

當細胞被取消選定,動畫刪除突出顯示開始:

  • selectedBackgroundView alpha屬性從1.0(完全不透明)動畫到0.0(完全透明)。
  • 的backgroundView再次不變(使動畫看起來像selectedBackgroundView和backgroundView之間的交叉淡入淡出)
  • 只有一次完成了動畫中的內容查看即可在「未選」狀態重繪其子視圖的backgroundColor的再次變得可見(這可能會導致你的動畫看起來可怕所以建議你不要在你的內容查看使用UIView.backgroundColor)
1

在視網膜上的顯示,甚至繪製0.5個單位線將導致兩個像素爲因爲反鋸齒。爲了呈現爲兩個顯示器上的單個像素,四分之一接班了單位:

UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, self.contentView.frame.size.height - (1.0 - 0.25), self.contentView.frame.size.wi 
lineView.backgroundColor = [UIColor colorWithRed:(230.0/255.0f) green:(233/255.0f) blue:(237.0/255.0f) alpha:1.0f]; 
[self.contentView addSubview:lineView]; 
13

這些答案會導致亮點RECT通過分離器被覆蓋您添加到您的細胞(在iOS 6與我至少測試)。您需要設置separatorColor[UIColor clearColor]使細胞仍然由1px的分離,那麼你就可以得出你的分隔符的差距:

- (void)viewDidLoad { 
    self.tableView.separatorColor = [UIColor clearColor]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    // snip 
    CALayer *separator = [CALayer layer]; 
    separator.backgroundColor = [UIColor redColor].CGColor; 
    separator.frame = CGRectMake(0, 43, self.view.frame.size.width, 1); 
    [cell.layer addSublayer:separator]; 
    return cell; 
} 
+0

這會爲當前視圖創建一條線。當屏幕旋轉到橫向時,它會太短。爲此,您需要添加約束。 – 2016-07-10 12:50:43

1

測試在iOS 7(GM):

@implementation MyTableViewController 

- (void)viewDidLayoutSubviews { 
    for (UIView *view in self.view.subviews) { 
     if ([view isKindOfClass:NSClassFromString(@"_UITableViewCellSeparatorView")]) 
      view.backgroundColor = [UIColor redColor]; 
    } 
} 

@end 

注意:看起來UITableView 會將分隔符移動到某些配置中的單元格,這會導致此代碼無法工作,除非您也下載到所有UITableViewCells中。

+1

我相信我可能會遇到類似的問題,因爲我在這裏搜索_UITableViewCellSeparatorView。我有一個適用於iOS 6的自定義分隔符實現,但iOS 7中似乎除了顯示我的默認分隔符外。將分隔符樣式設置爲UITableViewCellSeparatorStyleNone會導致兩者都消失。您是否能夠進一步瞭解您的問題? – markdorison 2013-09-19 22:35:01

+1

這是使用私人API,可能會導致應用程序拒絕,以及它可能在任何未來的iOS版本中損壞。 – Vive 2015-06-20 21:18:11

+0

這不是私人API。但你仍然應該小心,因爲你是正確的,它是私人的視圖層次結構,並可能在任何時候改變。然而,這只是美學,並且是安全的。 – mxcl 2015-06-22 17:39:52

1

下面要點中的單元格是一個UITableViewCell的子類,其中每個單元格可以有自己的分隔符/多種樣式(僅支持.None和.Default)。 它在Swift中編寫並假定使用Autolayout。

https://gist.github.com/evgeniyd/fa36b6f586a5850bca3f

如何使用類:

  1. 集的UITableView對象的分離風格UITableViewCellSeparatorStyle.None

    tableView.separatorStyle = .None 
    
  2. 創建MPSTableViewCell

  3. 某處的一個子類中, awakeFromNib()組蜂窩狀的隔板風格

注:下面的代碼假定小區從廈門國際銀行加載/分鏡

class FASWorkoutCell: FASTableViewCell { 

    required init(coder aDecoder: NSCoder) { 
     super.init(coder: aDecoder) 
    } 

    override func awakeFromNib() { 
     super.awakeFromNib() 

     separatorType = .Default 
    } 

    // ... 

    } 
0

如果您使用自定義的UITableViewCell,你可以簡單地在的UITableViewCell的底部添加的UIView。 xib並將背景色設置爲所需的顏色。

例如,在我XIB在底部添加一個UIView並設置高度1.使用自動佈局,我設置左約束至12,底部約束爲0,右約束爲0,高度爲1

2

如果您在Swift中使用自定義單元格:另一種方法是使用可在該單元格的頂部繪製一條線的函數來擴展UITableViewCell。

import UIKit 

extension UITableViewCell { 
    func addSeparatorLineToTop(){ 
     var lineFrame = CGRectMake(0, 0, bounds.size.width, 1) 
     var line = UIView(frame: lineFrame) 
     line.backgroundColor = UIColor.myGray_300() 
     addSubview(line) 
    } 
} 

然後你就可以在awakeFromNib此行添加到任何自定義單元格,例如

override func awakeFromNib() { 
    super.awakeFromNib() 
    addSeparatorLineToTop() 
} 

該解決方案是好的,因爲它不會弄亂你的故事板,並限制你的額外代碼1號線。

0

斯威夫特版本:

private let kSeparatorTag = 123 
private let kSeparatorHeight: CGFloat = 1.5 

func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) 
{ 
    if cell.viewWithTag(kSeparatorTag) == nil //add separator only once 
    { 
     let separatorView = UIView(frame: CGRectMake(0, cell.frame.height - kSeparatorHeight, cell.frame.width, kSeparatorHeight)) 
     separatorView.tag = kSeparatorId 
     separatorView.backgroundColor = UIColor.redColor() 
     separatorView.autoresizingMask = [.FlexibleWidth, .FlexibleHeight] 

     cell.addSubview(separatorView) 
    } 
}