2013-06-28 13 views
7

Chciałbym utworzyć linię separatora jak ten:tworzyć niestandardowe UITableView separator linii

enter image description here

Każdy pomysł o tym, jak je zaimplementować? Próbowałem się obraz linii i korzystania UIAppearance obiektów Proxy:

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

ale jakoś tylko czarna linia zostanie wygenerowana.

Odpowiedz

9

można spróbować poniżej:

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]; 

lub

- (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

wierzę Twoja ostatnia linia jest: [cell.contentView addSubview: Separator]; mimo to nie działa, ponieważ potrzebuję linii separatora z dwoma kolorami – Claus

+0

jak dodać obraz zamiast tego? – null

+0

Myślę, że drugie podejście działa lepiej, ale musiałem ustawić CGRectMake (0, 1, 320, 1) – Claus

9

@Tarek użyłem dwóch instancji swoich obiektów do tworzenia podwójnej linii

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]; 

Wygląda dobrze! Uznanie dla ciebie

+0

Mój człowiek przyjemności :) – null

+1

thnks człowiek jego praca n pomógł mi – ashokdy

1

Swift 3

viewDidLoad:

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

tableView komórka:

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) 
    } 
} 
Powiązane problemy