I have some strange issue with UITableView
only in iOS 7.
UITableViewCellSeparator
disappears above the first row and below the last row. S
Did you try adding a UIView of height 1 in the header and the footer of the table with light gray background color? Basically it'll mock the first and last separators.
To fix the above issue, add empty footer in viewDidLoad.
UIView *emptyView_ = [[UIView alloc] initWithFrame:CGRectZero];
emptyView_.backgroundColor = [UIColor clearColor];
[tableView setTableFooterView:emptyView_];
Please don't use the above lines in viewForFooterInSection delegate method. Means Dont implement viewForFooterInSection method.
As this is still an issue with IOS 8, I will add my solution in Swift. Set the tableview separator line to none. Then add this code to the cellForRowAtIndexPath delegate method. It will add a nice separator. The if statement allows to decide which cells should have a separator.
var separator:UIView!
if let s = cell.viewWithTag(1000)
{
separator = s
}
else
{
separator = UIView()
separator.tag = 1000
separator.setTranslatesAutoresizingMaskIntoConstraints(false)
cell.addSubview(separator)
// Swiper constraints
var leadingConstraint = NSLayoutConstraint(item: separator, attribute: .Leading, relatedBy: .Equal, toItem: cell, attribute: .Leading, multiplier: 1, constant: 15)
var heightConstraint = NSLayoutConstraint(item: separator, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 0.5)
var bottomConstraint = NSLayoutConstraint(item: cell, attribute: .Bottom, relatedBy: .Equal, toItem: separator, attribute: .Bottom, multiplier: 1, constant:0)
var trailingConstraint = NSLayoutConstraint(item: cell, attribute: .Trailing, relatedBy: .Equal, toItem: separator, attribute: .Trailing, multiplier: 1, constant: 15)
cell.addConstraints([bottomConstraint, leadingConstraint, heightConstraint, trailingConstraint])
}
if indexPath.row == 3
{
separator.backgroundColor = UIColor.clearColor()
}
else
{
separator.backgroundColor = UIColor.blackColor()
}
Seems like this issue manifests under so many circumstances.
For me it had something to do with cell selection. I have no idea why and didn't have time to dig into it too deep, but I can say it started occurring when I set the cell's selectionStyle
to none. ie:
//This line brought up the issue for me
cell.selectionStyle = UITableViewCellSelectionStyleNone;
I tried using some of the delegate methods above that toggle on and off the separatorStyle
property of the tableView
but they didn't seem to do anything to fix my issue.
The whole reason why I needed that was because I didn't even need cell selection.
So, I did find something that worked for me. I just disabled selection on the UITableView
:
tableView.allowsSelection = NO;
Hope this helps someone, if you don't need to have cell selection.
I dumped the subview hierarchy of affected cells and found that the _UITableViewCellSeparatorView
was set to hidden. No wonder it's not shown!
I overrode layoutSubviews
in my UITableViewCell
subclass and now the separators are displayed reliably:
Objective-C:
- (void)layoutSubviews {
[super layoutSubviews];
for (UIView *subview in self.contentView.superview.subviews) {
if ([NSStringFromClass(subview.class) hasSuffix:@"SeparatorView"]) {
subview.hidden = NO;
}
}
}
Swift:
override func layoutSubviews() {
super.layoutSubviews()
guard let superview = contentView.superview else {
return
}
for subview in superview.subviews {
if String(subview.dynamicType).hasSuffix("SeparatorView") {
subview.hidden = false
}
}
}
The other solutions proposed here didn't work consistently for me or seem clunky (adding custom 1 px footer views).
Here's my solution to this problem. After you insert your cell(s), reload the section. If reloading the entire section is too intensive for you, just reload the indexPaths above and below.
[CATransaction begin];
[CATransaction setCompletionBlock:^{
// Fix for issue where seperators disappear
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationNone];
}];
[self.tableView insertRowsAtIndexPaths:@[ indexPath ] withRowAnimation:UITableViewRowAnimationFade];
[CATransaction commit];