2013-06-28 61 views
7

我想創建分隔線像這樣的:創建自定義的UITableView分隔線

enter image description here

有關如何實現它的主意? 我試圖讓行的圖像,並使用UIAppearance代理對象:

[[UITableView appearanceWhenContainedIn:[MyController class], nil] setSeparatorColor: 
[UIColor colorWithPatternImage:[UIImage imageNamed:@"line.png"]]]; 
[[UITableView appearanceWhenContainedIn:[MyController class], nil] setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine]; 

但是,不知何故,只有黑線獲取呈現。

回答

9

你可以試試下面:

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; 
} 
+1

我相信你的最後一行是:[cell.contentView addSubview:分隔符]。 無論如何它不工作,因爲我需要一個分離線兩種顏色 – Claus

+0

如何添加圖像,而不是? – null

+0

我認爲第二種方法效果更好,但我必須設置 CGRectMake(0,1,320,1) – Claus

9

@Tarek 我用你的對象的兩個實例創建雙線

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

看起來不錯!榮譽對您

+0

我的榮幸男人:) – null

+1

thnks男人的工作ñ幫助我 – ashokdy

1

斯威夫特3

viewDidLoad中:

//remove default separator line 
tableView.separatorStyle = .none 

的tableView細胞:

class MyCustomCell: UITableViewCell { 

    override func awakeFromNib() { 
     super.awakeFromNib() 

     let separator = UIView(frame: CGRect(x: 8, y: bounds.size.height - 0.5, width: bounds.size.width - 22, height: 1)) 
     separator.backgroundColor = UIColor.red 
     contentView.addSubview(separator) 
    } 
}