Reputation: 1171
So I am adding a UITableView
to a UIViewController
programmatically and I am getting the following result:
I make the table in viewDidLoad
like so:
let size = view.bounds.size
table = UITableView(frame: CGRectMake(0, 65, size.width, size.height-65), style: .Plain)
table.delegate = self
table.dataSource = self
table.separatorInset = UIEdgeInsetsZero
table.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
This is how I setup the cells
:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: "cell")
cell.selectionStyle = .None
cell.imageView?.image = blueCheck
cell.textLabel?.text = "TEXT"
cell.separatorInset = UIEdgeInsetsZero
return cell
}
I have also tried doing this which is a suggested answer to similar questions.
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
cell.separatorInset = UIEdgeInsetsZero
cell.layoutMargins = UIEdgeInsetsZero
}
I have also tried doing stuff like not using reusable cells
and setting the cell
frame but nothing works.
Any help or ideas is welcome. Thanks!
EDIT:
Setting table.separatorInset = UIEdgeInsetsZero
is what is causing the empty cells
to move to the left like in the SS shown above. When that is removed the empty cells
have the large space as well.
I have also attempted using constraints programmatically to see if that made any difference. I got the same result sadly enough. This is what I tried:
let bottomConstraint = NSLayoutConstraint(item: table, attribute: NSLayoutAttribute.BottomMargin, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.BottomMargin, multiplier: 1, constant: 0)
let topConstraint = NSLayoutConstraint(item: table, attribute: NSLayoutAttribute.TopMargin, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.TopMargin, multiplier: 1, constant: 65)
let leftConstraint = NSLayoutConstraint(item: table, attribute: NSLayoutAttribute.LeadingMargin, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.LeadingMargin, multiplier: 1, constant: 0)
let rightConstraint = NSLayoutConstraint(item: table, attribute: NSLayoutAttribute.TrailingMargin, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.TrailingMargin, multiplier: 1, constant: 0)
Upvotes: 2
Views: 414
Reputation: 479
Check the Superview's Frame before adding this Tableview as subview.
Upvotes: 0
Reputation: 1171
So the issue was an iOS 9 thing where sometimes you also need to setcellLayoutMarginsFollowReadableWidth = false
in order to customize insets and margins. I haven't run into this when using the story board + constraints so I am not sure if this only something that comes up when making everything programmatically.
Here is the code I added to make it work:
override func viewWillAppear(animated: Bool) {
if #available(iOS 9.0, *) {
table.cellLayoutMarginsFollowReadableWidth = false
} else {
// Fallback on earlier versions
}
}
override func viewDidLayoutSubviews() {
table.separatorInset = UIEdgeInsetsZero
table.layoutMargins = UIEdgeInsetsZero
}
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
cell.separatorInset = UIEdgeInsetsZero
cell.preservesSuperviewLayoutMargins = false
cell.layoutMargins = UIEdgeInsetsZero
}
Upvotes: 3