2011-01-26 117 views

回答

4

您可以添加一個UIImageView,例如,單元格的高度和寬度一樣寬,然後將其原點設置爲單元格的左下角。

+6

不要忘記關閉默認的分隔樣式表視圖:`[自我。tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];` – Jasarien 2011-01-26 12:41:04

+0

你可以給我粗略的代碼,所以我可以開始,我如何設置它的起源單元格的左下角? – 2011-01-26 12:43:44

+1

我已經得到了解決方案,謝謝大家。 – 2011-01-26 13:25:32

84

將tableview的separatorStyle設置爲UITableViewCellSeparatorStyleNone。將您的分隔符圖像作爲子視圖添加到每個單元格並正確設置框架。

+1

你會plz給我一個例子。 – 2011-01-26 12:46:18

+0

[separatorImageView setFrame:CGRectMake(0,0,cell.frame.size.width,separatorImageView.image.size.height)]; – Felix 2011-01-26 12:54:43

+0

[cell.imageView setFrame:CGRectMake(0,0,cell.frame.size.width,cell.frame.size.height)];沒有能夠做到這一點:( – cV2 2011-03-16 14:40:33

0

如果要更改默認選項(無分隔符,實線或蝕刻線),您有2個選項可更改uitableview的分隔符樣式。

  1. 最簡單的由在包括分隔線背景圖像 給每個小區視圖。你可以檢查你的單元格位於 tableview的哪個位置,以便應用正確的背景圖像,該背景圖像將爲您提供 單元格頂部的分隔線或 單元格的底部。

    設置分隔符的風格沒有你 的tableview的viewDidLoad中:

    [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone]。

    設置你的背景圖像中(的UITableViewCell *)的tableView:(UITableView的*)的tableView 的cellForRowAtIndexPath:(NSIndexPath *)indexPath功能

    UIImage* yourBgImg = [[UIImage imageNamed:@"bgImage.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(5, 5, 5, 5)]; 
    

    cell.backgroundView = [[UIImageView的頁頭] initWithImage:yourBgImg];

    檢查你的細胞的結合下面的部分中的位置:

    NSInteger的sectionRows = [的tableView numberOfRowsInSection:[indexPathsection]]; NSInteger row = [indexPath row];

  2. 將分隔線添加爲單元格。我發現一個帖子最近這 這裏: http://www.dimzzy.com/blog/2012/01/separator-cells-for-uitableview/#disqus_thread
4

試試這個:

UIImageView *separator = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"separator.png"]]; 
[cell.contentView addSubview: separator]; 

這就是我如何得到它工作得很好的例子。

請記住將表視圖的分隔符樣式設置爲無。

29

首先,你可以寫代碼:

{ [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];} 

{ #define cellHeight 80 // You can change according to your req.<br> 
    #define cellWidth 320 // You can change according to your req.<br> 

    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
     UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"seprater_line.png"]]; 
     imgView.frame = CGRectMake(0, cellHeight, cellWidth, 1); 
     [customCell.contentView addSubview:imgView]; 
     return customCell; 

    } 
} 
51

試試這個

目標C

[TableView setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine]; 

    [TableView setSeparatorColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"[email protected]"]]]; 

斯威夫特

tableView.separatorStyle = UITableViewCellSeparatorStyle.SingleLine 
    tableView.separatorColor = UIColor(patternImage: UIImage(named: "YOUR_IMAGE_NAME")!) 
7

設置分隔符的顏色與你的圖像進行構圖。

viewDidLoad

self.tableView.separatorColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"mySeparatorImage"]]; 
0

這裏是通過使CALayer用於圖像,並使用該添加自定義分隔線到UITableView的另一種方法爲分隔線。

//作出的CALayer爲分隔線的圖像

CALayer *separator = [CALayer layer]; 
separator.contents = (id)[UIImage imageNamed:@"myImage.png"].CGImage; 
separator.frame = CGRectMake(0, 54, self.view.frame.size.width, 2); 
[cell.layer addSublayer:separator]; 
5

我的項目是基於iOS上的7 這有助於我

[self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];

然後把一個子視圖到小區分隔器!

0

你可以試試下面:

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"separator.png"]]; 
    imageView.frame = CGRectMake(0, 100, 320, 1); 
    [customCell.contentView addSubview:imageView]; 

    return customCell; 
} 
2

這是絕對有幫助。加工。 ,但從屬性檢查器中設置分隔符「無」。在的cellForRowAtIndexPath 寫下面的代碼方法

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

    lineView.backgroundColor = [UIColor blackColor]; 
    [cell.contentView addSubview:lineView]; 
1

斯威夫特3/4

自定義的分隔線,把這個代碼在這的UITableViewCell的子類中的自定義單元格(或CellForRow或WillDisplay TableViewDelegates爲非定製單元格):

let separatorLine = UIImageView.init(frame: CGRect(x: 8, y: 64, width: cell.frame.width - 16, height: 2)) 
separatorLine.backgroundColor = .blue 
addSubview(separatorLine) 
1

在每個tableview單元格下添加分隔線的最簡單方法可以在t他故事板本身。 首先選擇tableview,然後在屬性檢查器中選擇分隔線屬性爲單行。之後,選擇要自定義的分隔符插入,並將左側插入從左側更新爲0。

Example