【发布时间】:2011-06-15 20:00:31
【问题描述】:
如何更改 UITableView 中每个单元格末尾显示的分隔线? 我想要一个图像是细分隔线图像。
【问题讨论】:
标签: iphone uitableview
如何更改 UITableView 中每个单元格末尾显示的分隔线? 我想要一个图像是细分隔线图像。
【问题讨论】:
标签: iphone uitableview
将tableview的separatorStyle设置为UITableViewCellSeparatorStyleNone。将分隔符图像作为子视图添加到每个单元格并正确设置框架。
【讨论】:
试试这个
目标 C
[TableView setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
[TableView setSeparatorColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"Divider_line@2x.png"]]];
斯威夫特
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)
首先你可以写代码:
{ [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 子类中添加。
设置要与您的图像搭配的分隔符颜色。
在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];
这绝对是有帮助的。在职的。 但从属性检查器中设置分隔符“无”。 在 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]; 也试过了,除了最右边和最左边之外,它几乎没有显示。
斯威夫特 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
【讨论】:
如果您想更改无分隔符、实线或蚀刻线的默认选项,您有 2 个选项来更改 uitableview 的分隔符样式。
最简单的方法是包含分隔线背景图片 到每个单元格视图。您可以检查您的手机所在的位置 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 alloc] initWithImage:yourBgImg];
使用以下内容检查单元格在该部分中的位置:
NSInteger sectionRows = [tableView numberOfRowsInSection:[indexPathsection]]; NSInteger 行 = [索引路径行];
【讨论】:
你可以试试下面的:
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;
}
【讨论】:
这是另一种将自定义分隔线添加到 UITableView 的方法,方法是为图像创建 CALayer 并将其用作分隔线。
// 为分隔线的图像制作一个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];
【讨论】:
在每个表格视图单元格下添加分隔线的最简单方法可以在情节提要本身中完成。 首先选择tableview,然后在属性检查器中选择分隔线属性为单行。在此之后,选择要自定义的分隔符插图,并将左侧插图从左侧更新为 0。
【讨论】: