Reputation: 23
I have a custom separator that I am implementing in my TableViewController
and want to have it hidden for the last row in each section due to it offsetting the display of my custom section header.
I am using a prototype cell in my storyboard which is of type TableViewController
and the cell is implementing the SessionTableViewCell
identifier.
I also have a custom SessionTableViewCell
class but this is empty at the moment.
The problem I am experiencing is that the current code works when the TableView
loads but when scrolling to another section and back the separator is then again applied to the last cell.
I believe the problem lies in the reuse of the cell in this method but I have also tried to create the separator in my SessionTableViewCell
subclass and then referring to it in my TableViewController
but the same behaviour still exists.
SessionTableViewController.swift
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "SessionTableViewCell") as! SessionTableViewCell
cell.sessionStartLabel.text = sessionData[indexPath.row].start
cell.sessionEndLabel.text = sessionData[indexPath.row].end
cell.sessionScoreLabel.text = sessionData[indexPath.row].score
let screensize = UIScreen.main.bounds
let separatorHeight = CGFloat(7.0)
let customSeparator = UIView.init(frame: CGRect(x: 0, y: cell.bounds.size.height - separatorHeight, width: screensize.width, height: separatorHeight))
customSeparator.backgroundColor = Config.tableViewBackgroundColor
cell.addSubview(customSeparator)
let rowsInSection = tableView.numberOfRows(inSection: indexPath.section)
let lastRowInSection = rowsInSection - 1
if indexPath.row != lastRowInSection {
customSeparator.isHidden = false
} else {
customSeparator.isHidden = true
}
return cell
}
Upvotes: 2
Views: 133
Reputation: 2652
Separator is getting added again and again.
When you are trying to hide the separator its hiding the recently added separator view.
Create your separator in awakeFromNib()
of SessionTableViewCell
and add the property customSeparator
as the instance var of SessionTableViewCell
.
It should work fine.
Upvotes: 1
Reputation: 277
Remove custom separator before add
let customeView = cell.contentView.viewWithTag(200) as! UIView
if customeView != nil
{
customeView.removeFromSuperview()
}
let screensize = UIScreen.main.bounds
let separatorHeight = CGFloat(7.0)
let customSeparator = UIView.init(frame: CGRect(x: 0, y: cell.bounds.size.height - separatorHeight, width: screensize.width, height: separatorHeight))
customSeparator.backgroundColor = Config.tableViewBackgroundColor
customSeparator.tag = 200
cell.addSubview(customSeparator)
Upvotes: 0
Reputation: 336
I think I get your problem, and would suggest an alternative:
You could set the section footer to an empty frame.
CGRectZero
I've implemented a similar scenario with table footers. I'm sorry I'm new to swift and will try and post some swift code later. In the mean time you could try something similar to:
self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
Upvotes: 0