创建自定义UITableView分隔线

Cla*_*aus 8 uitableview uikit ios uiappearance

我想创建一个像这样的分隔线:

在此输入图像描述

有关如何实现它的任何想法?我尝试获取该行的图像并使用UIAppearance代理对象:

[[UITableView appearanceWhenContainedIn:[MyController class], nil] setSeparatorColor:
[UIColor colorWithPatternImage:[UIImage imageNamed:@"line.png"]]];
[[UITableView appearanceWhenContainedIn:[MyController class], nil] setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
Run Code Online (Sandbox Code Playgroud)

但是,不知何故,只有黑线被渲染.

nul*_*ull 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];
Run Code Online (Sandbox Code Playgroud)

要么

- (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;
}
Run Code Online (Sandbox Code Playgroud)

  • 我之前为我的一个应用程序做过这个技巧,它是一个静态的UITable,我为表格预先设计了背景并删除了分隔符和单元格bg;) (2认同)

Cla*_*aus 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];
Run Code Online (Sandbox Code Playgroud)

看起来不错!感谢你