我该如何更改在UITableView的每个单元格末尾显示的分隔线?我想要一个细分隔符类型的图像。
我该如何更改在UITableView的每个单元格末尾显示的分隔线?我想要一个细分隔符类型的图像。
将表格视图的separatorStyle
设置为UITableViewCellSeparatorStyleNone
。将分隔符图像添加为每个单元格的子视图,并正确设置其框架。
试试这个
Objective C
[TableView setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
[TableView setSeparatorColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"Divider_line@2x.png"]]];
Swift
tableView.separatorStyle = UITableViewCellSeparatorStyle.SingleLine
tableView.separatorColor = UIColor(patternImage: UIImage(named: "YOUR_IMAGE_NAME")!)
table.separatorStyle = UITableViewCellSeparatorStyle.SingleLine table.separatorColor = UIColor(red: 245/255, green: 245/255, blue: 245/255, alpha: 1.0)
- fs_tigre首先,您可以编写代码:
{ [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;
}
}
tableView:cellForRowAtIndexPath:
中只是向单元格添加子视图这样的解决方案的问题在于,它们会在每次单元格被重用时都添加一个新的子视图,而不会移除旧的子视图。应该仅添加一次,因此您应该在 UITableViewCell
子类中完成此操作。 - Gavin将分隔符的颜色与您的图片进行图案化匹配。
在viewDidLoad
中:
self.tableView.separatorColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"mySeparatorImage"]];
我的项目基于iOS 7
这有助于我
[self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
然后将一个子视图放入单元格作为分隔线!
UIImageView *separator = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"separator.png"]];
[cell.contentView addSubview: separator];
这是我成功实现的一个例子。
记得将表格视图的分隔符样式设置为无。
您可以添加一个UIImageView,例如高度为1点,宽度与单元格框架相同,然后将其原点设置为单元格的左下角。
[self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
- Jasarien这绝对是有帮助的。工作正常。 但要从属性检查器中设置分隔符为 "none"。 在 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];
[cell.contentView bringSubviewToFront:lineView];
,但它大多数时候并没有显示,除了最左边和最右边的一些情况。 - Timothy SwanSwift 3/4
自定义分割线,将以下代码放在自定义单元格中,该单元格是UITableViewCell的子类(或者对于非自定义单元格,在CellForRow或WillDisplay TableViewDelegates中):
let separatorLine = UIView.init(frame: CGRect(x: 8, y: 64, width: cell.frame.width - 16, height: 2))
separatorLine.backgroundColor = .blue
addSubview(separatorLine)
在viewDidLoad方法中:
tableView.separatorStyle = .none