[英]UITableView how to add a custom UIImageView as a separator and set the custom position
I have a UITableView with a number of sections (not rows). 我有一个UITableView与许多部分(而不是行)。 I'd like to add the UIImageView as a separator between each sections at the center (except the last cell).
我想在中心的每个部分之间添加UIImageView作为分隔符(除了最后一个单元格)。 The space between cells is 30
细胞之间的空间是30
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 30;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *headerView = [[UIView alloc] init];
headerView.backgroundColor = [UIColor clearColor];
return headerView;
}
I'd like to set the position like at the screenshot. 我想在屏幕截图中设置位置。
[self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
The cell height is 81. 细胞高度为81。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//Some code
UIImageView *separatorIView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 96, 196, 2)];
[separatorIView setImage:[UIImage imageNamed:@"grayline.png"]];
[cell.contentView addSubview:separatorIView];
return cell;
}
The grayline appears if I set the y coordinate < then cell height. 如果我设置y坐标<然后单元格高度,则会出现grayline。 Thanks in advance.
提前致谢。
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 30;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *headerView = [[UIView alloc] init];
headerView.backgroundColor = [UIColor clearColor];
if (section > 0) {
UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(62, 14, 196, 2)];
[img setImage:[UIImage imageNamed:@"grayline.png"]];
[headerView addSubview:img];
}
return headerView;
}
As you are setting a head view height you can add separator in header. 在设置头视图高度时,可以在标题中添加分隔符。 Table View has a delegate method named "viewForHeaderInSection".
Table View有一个名为“viewForHeaderInSection”的委托方法。
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 5)];
[img setImage:[UIImage imageNamed:@"grayline.png"]];
return img;
}
If you set the separator image in this delegate method you don't need to do anything in cellForRowAtIndexPath. 如果在此委托方法中设置分隔符图像,则无需在cellForRowAtIndexPath中执行任何操作。 Let me know if that helps... :)
如果有帮助,请告诉我... :)
Edit: 编辑:
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 5;
}
` `
自定义分隔符位置是不可能的,因为它们是专门为其框架定义的,除此之外,如果您需要这种效果,那么您必须在标题中添加图像,如自定义空间和颜色与需要视图,只有您可以获得所需视图
You can set Separator color using setSeparator color in which you can pass image 您可以使用setSeparator颜色设置分隔符颜色,您可以在其中传递图像
like, 喜欢,
[self.tableView setSeparatorColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"grayline.png"]]];
just try to make the height of the last header as zero and see if it works. 只是尝试将最后一个标题的高度设置为零,看看它是否有效。
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
if(make_cndition_for_lastsection)
return 0;
else
return 30;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.