为什么UITableViewCell宽度小于UITableView宽度

时间:2016-03-31 11:09:15

标签: swift uitableview

我正在尝试以编程方式创建UITableView。我的单元格宽度有问题,固定为320,而iphone 6的表宽为375,iPhone 6的表宽为414

如何使单元格宽度等于表格的宽度?

查看课程:

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
    }
}

自定义单元格类:

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
    }

}

运行代码时的结果:

单元格宽度为320.0
细胞界限是320.0
tableView宽度为414

screenshot

我正在使用Xcode 7.3,Swift 2.2,iOS 9.3

1 个答案:

答案 0 :(得分:0)

也许您应该明确设置单元格的宽度,它现在具有默认大小:

修改

更改您的手机课程:

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
    }

}

在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
    }

输出:

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