我在定制一个UITableView。我想隐藏在最后一个单元格上的分离线…我能这样做吗?
我知道我可以用tableView。separatorStyle = UITableViewCellStyle。没有,但是这会影响tableView的所有单元格。我希望它只影响最后一个单元格。
我在定制一个UITableView。我想隐藏在最后一个单元格上的分离线…我能这样做吗?
我知道我可以用tableView。separatorStyle = UITableViewCellStyle。没有,但是这会影响tableView的所有单元格。我希望它只影响最后一个单元格。
当前回答
试试下面的代码,也许能帮你解决问题
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString* reuseIdentifier = @"Contact Cell";
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
if (nil == cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
if (indexPath.row != 10) {//Specify the cell number
cell.backgroundView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bgWithLine.png"]];
} else {
cell.backgroundView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bgWithOutLine.png"]];
}
}
return cell;
}
其他回答
试试下面的代码,也许能帮你解决问题
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString* reuseIdentifier = @"Contact Cell";
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
if (nil == cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
if (indexPath.row != 10) {//Specify the cell number
cell.backgroundView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bgWithLine.png"]];
} else {
cell.backgroundView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bgWithOutLine.png"]];
}
}
return cell;
}
来跟进Hiren的回答。
在ViewDidLoad和下面一行:
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
或者,如果你正在使用XIB或storyboard,将“separator”更改为“none”:
在CellForRowAtIndexPath中添加这个:
CGFloat separatorInset; // Separator x position
CGFloat separatorHeight;
CGFloat separatorWidth;
CGFloat separatorY;
UIImageView *separator;
UIColor *separatorBGColor;
separatorY = cell.frame.size.height;
separatorHeight = (1.0 / [UIScreen mainScreen].scale); // This assures you to have a 1px line height whatever the screen resolution
separatorWidth = cell.frame.size.width;
separatorInset = 15.0f;
separatorBGColor = [UIColor colorWithRed: 204.0/255.0 green: 204.0/255.0 blue: 204.0/255.0 alpha:1.0];
separator = [[UIImageView alloc] initWithFrame:CGRectMake(separatorInset, separatorY, separatorWidth,separatorHeight)];
separator.backgroundColor = separatorBGColor;
[cell addSubView: separator];
这是一个结果的例子,我显示一个动态单元格的tableview(但只有一个内容)。结果是,只有一个有分隔符,而不是所有的“虚拟”tableview自动添加填充屏幕。
编辑:对于那些不经常阅读注释的人来说,实际上有一种更好的方法,只需几行代码:
override func viewDidLoad() {
super.viewDidLoad()
tableView.tableFooterView = UIView()
}
在willdisplaycell:
cell.separatorInset = UIEdgeInsetsMake(0, cell.bounds.size.width, 0, 0)
更简单、更符合逻辑的做法是:
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
return [[UIView alloc] initWithFrame:CGRectZero];
}
在大多数情况下,您不希望只看到最后一个表视图单元格分隔符。这种方法只删除了最后一个表格视图单元格分隔符,你不需要考虑自动布局问题(即旋转设备)或硬编码值来设置分隔符嵌入。
长筒5 . 13+
当你定义你的表时,只需添加:
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
// Removes separator lines
tableView.separatorStyle = UITableViewCell.SeparatorStyle.none
return UIView()
}
神奇的一行是tableView。separatorStyle = UITableViewCell.SeparatorStyle.none