Dan TImofte
Dan TImofte

Reputation: 21

Why is UITableViewCell width less than UITableView width

I am trying to create a UITableView programatically. I have a problem with the cell width, it is fixed at 320 while the table width is 375 for iphone 6 or 414 for iphone 6 plus

How can i make the cell width equal to the width of the table ?

view class :

import UIKit

class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {

    override func viewDidLoad() {
        super.viewDidLoad()

        let myTableView = UITableView(frame: self.view.frame)
        myTableView.delegate = self
        myTableView.dataSource = self
        view.addSubview(myTableView)
        // Do any additional setup after loading the view, typically from a nib.
    }

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


    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell:customTableViewCell = customTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "mycell")
        cell.backgroundColor = UIColor.blueColor()
        print("tableView width is \(tableView.frame.size.width)")
        return cell
    }
}

custom cell class :

import UIKit

class customTableViewCell: UITableViewCell {

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        configureView()
    }

    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)!
        configureView()
    }

    func configureView() {

        print("cell width is \(self.frame.size.width)")
        print("cell bounds is \(self.contentView.frame.width)")
        let locationLabel = UILabel()
        locationLabel.frame = CGRectMake(0, 0  , self.frame.size.width,self.frame.size.height )
        locationLabel.text = "custom cell"
        locationLabel.backgroundColor = UIColor.blackColor()

        self.addSubview(locationLabel)
        // add and configure subviews here
    }

}

Result when running the code :

cell width is 320.0
cell bounds is 320.0
tableView width is 414

screenshot

I am using Xcode 7.3 , Swift 2.2 , iOS 9.3

Upvotes: 0

Views: 5931

Answers (1)

schmidt9
schmidt9

Reputation: 4538

maybe you should set cell's width explicitly, it has a default size now:

Edit

Change your cell class:

import UIKit

class customTableViewCell: UITableViewCell {

    var locationLabel: UILabel = UILabel()

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        configureView()
    }

    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)!
        configureView()
    }

    func configureView() {

        print("cell width is \(self.frame.size.width)")
        print("cell bounds is \(self.contentView.frame.width)")
        locationLabel.text = "custom cell"
        locationLabel.textColor = UIColor.whiteColor()
        locationLabel.backgroundColor = UIColor.blackColor()

        self.addSubview(locationLabel)
        // add and configure subviews here
    }

}

Edit in VC:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell:customTableViewCell = customTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "mycell")
        cell.backgroundColor = UIColor.blueColor()
        cell.frame = CGRectMake(0, 0, tableView.frame.size.width, cell.frame.size.height)
        cell.locationLabel.frame = cell.frame
        print("cell width in cellForRowAtIndexPath is \(cell.frame.size.width)")
        print("tableView width is \(tableView.frame.size.width)")
        return cell
    }

Output:

cell width is 320.0
cell bounds is 320.0
cell width in cellForRowAtIndexPath is 414.0
tableView width is 414.0

Upvotes: 2

Related Questions