steventnorris steventnorris - 1 month ago 12
Swift Question

UITableViewCell not filling width of table

My table cell is not filling the width of my table. Code below:

Controller with table view:

import UIKit

class MenuController: UIViewController, UITableViewDataSource, UITableViewDelegate {

@IBOutlet var menuTable:UITableView!

var rootNav:UINavigationController!

private var cellIdentifier = "MenuCell"

//This is an array of arrays of dictionaries with String key and Any value
//Top level array is sections
//Second level array is items
//Dictionary holds data for that individual cell
private final var menuItems:[[[String:Any?]]] = [

//Section 1
[
//Shot Chart
["image":Images.BALL,"title":" Chart","stack":ViewStacks.CHART],

//Profile
["image":Images.PROFILE,"title":"Profile","stack":ViewStacks.PROFILE]
],

//Section 2
[
//Logout
["image":Images.LOGOUT,"title":"Logout","stack":nil]
]

]

init(rootNav:UINavigationController, nibName:String?, bundle:Bundle?) {
self.rootNav = rootNav
super.init(nibName: nibName, bundle: bundle)
}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

override func viewDidLoad() {
super.viewDidLoad()

}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

//Table View Datasource
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell:MenuCell? = menuTable.dequeueReusableCell(withIdentifier: cellIdentifier) as! MenuCell?
if cell == nil {
cell = MenuCell(style: UITableViewCellStyle.default, reuseIdentifier: cellIdentifier)
}
let cellProperties = menuItems[indexPath.section][indexPath.row]
cell!.imageView?.image = (cellProperties["image"] as! UIImage)
cell!.titleLbl?.text = (cellProperties["title"] as! String)
return cell!
}

func numberOfSections(in tableView: UITableView) -> Int {
return menuItems.count
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return menuItems[section].count
}

//Table View Delegate
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 50
}

}


UITableViewCell:

import UIKit

class MenuCell: UITableViewCell {
@IBOutlet var iconImg:UIImageView!
@IBOutlet var titleLbl:UILabel!

override func awakeFromNib() {
super.awakeFromNib()
}

override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
super.setSelected(false, animated: true)
}
}


Constraints on cell:
enter image description here

What cells should look like:
enter image description here

The image shows, but no text, no background, nothing. It's as if the autoLayout is doing nothing and the image is just showing in it's default size.

Answer

Its because you have made a xib of UITableViewCell and have not registered that.

It is showing image because you are using cell.imageView which is the default imageView for UITableViewCell and it always satisfy the nil condition. You haven't provided any outlet of your imageView which you have drag and dropped into your UITableViewCell xib.

So change your viewDidLoad

override func viewDidLoad() {

    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    menuTable.register(UINib(nibName: "MenuCell", bundle: nil), forCellReuseIdentifier: "MenuCell")
}

Make sure the identifier you have given of your UITableViewCell subclass i.e. MenuCell is MenuCell

Also change your cellForRow method to

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier) as! MenuCell
    let cellProperties = menuItems[indexPath.section][indexPath.row]

    // you are using imageView which is the default property for UITableViewCell. Create your own imageView outlet. Name it as menuImageView and replace imageView with menuImageView
    cell.imageView?.image = (cellProperties["image"] as! UIImage)

    cell.titleLbl?.text = (cellProperties["title"] as! String)
    return cell
}