我在定制一个UITableView。我想隐藏在最后一个单元格上的分离线…我能这样做吗?
我知道我可以用tableView。separatorStyle = UITableViewCellStyle。没有,但是这会影响tableView的所有单元格。我希望它只影响最后一个单元格。
我在定制一个UITableView。我想隐藏在最后一个单元格上的分离线…我能这样做吗?
我知道我可以用tableView。separatorStyle = UITableViewCellStyle。没有,但是这会影响tableView的所有单元格。我希望它只影响最后一个单元格。
当前回答
如果接受的答案不成立,你可以试试这个:
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
return 0.01f; }
太棒了;)
其他回答
迅速:
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
...
// remove separator for last cell
cell.separatorInset = indexPath.row < numberOfRowsInSection-1
? tableView.separatorInset
: UIEdgeInsets(top: 0, left: tableView.bounds.size.width, bottom: 0, right: 0)
return cell
}
objective - c:
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
...
// remove separator for last cell
cell.separatorInset = (indexPath.row < numberOfRowsInSection-1)
? tableView.separatorInset
: UIEdgeInsetsMake(0.f, tableView.bounds.size.width, 0.f, 0.f);
return cell;
}
在UITableViewDataSource的cellForRowAtIndexPath方法中
迅速:
if indexPath.row == {your row number} {
cell.separatorInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: .greatestFiniteMagnitude)
}
or :
cell.separatorInset = UIEdgeInsetsMake(0, 0, 0, UIScreen.main.bounds.width)
对于默认保证金:
cell.separatorInset = UIEdgeInsetsMake(0, tCell.layoutMargins.left, 0, 0)
显示端到端的分隔符
cell.separatorInset = .zero
objective - c:
if (indexPath.row == {your row number}) {
cell.separatorInset = UIEdgeInsetsMake(0.0f, 0.0f, 0.0f, CGFLOAT_MAX);
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
}
使用这个子类,set separatorInset在iOS 9.2.1中不起作用,内容会被压缩。
@interface NSPZeroMarginCell : UITableViewCell
@property (nonatomic, assign) BOOL separatorHidden;
@end
@implementation NSPZeroMarginCell
- (void) layoutSubviews {
[super layoutSubviews];
for (UIView *view in self.subviews) {
if (![view isKindOfClass:[UIControl class]]) {
if (CGRectGetHeight(view.frame) < 3) {
view.hidden = self.separatorHidden;
}
}
}
}
@end
https://gist.github.com/liruqi/9a5add4669e8d9cd3ee9
我能找到的唯一解决办法就是下一个
extension UITableViewCell {
func separator(hide: Bool) {
separatorInset.left = hide ? self.bounds.width * 1.5 : 16 // or whatever you want to be the left inset
}
}
我不知道为什么,但是self.bounds.width没有像预期的那样工作,所以我乘以1.5。
如果你想隐藏一个单元格,你只需要这样做:
cell.separator(hide: true)
对于其余的单元格,只发送参数为false
cell.separator(hide: false)