单元格宽度对于tableview宽度来说太大了

时间:2017-02-07 17:25:06

标签: ios swift uitableview swift3

我有一个嵌套在caraousel中的程序化tableview,但是当它显示时,程序单元格总是设置得比其中的tableview更宽。如果我打印轮播项目宽度,tableview宽度和单元格宽度我得到:281,281 ,320(iphone5屏幕宽度)。我尝试在单元格中添加约束但是一直没有错误,我相信因为单元格还没有被制作出来。所以我不确定在哪里放置约束以便我停止获取这些错误,我只需要单元格内容视图宽度来匹配tableview宽度,但是如何在我的自定义单元格中获取tableview宽度以编程方式制作?继承我目前的代码没有任何限制:

的TableView:

import UIKit

class SplitterCarouselItemTableView: UITableView {

    var splitter: BillSplitter?

    required init(frame: CGRect, style: UITableViewStyle, splitter: BillSplitter) {
        super.init(frame: frame, style: style)
        self.splitter = splitter
        setupView()
    }

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

    func setupView() {

        if Platform.isPhone {
            let tableViewBackground = UIImageView(image: UIImage(data: splitter?.image as! Data, scale:1.0))
            self.backgroundView = tableViewBackground
            tableViewBackground.contentMode = .scaleAspectFit
            tableViewBackground.frame = self.frame
        }

        self.backgroundColor = .clear
        self.separatorStyle = .none
    }
}

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

    tableView.register(SplitterCarouselItemTableViewCell.classForCoder(), forCellReuseIdentifier: "splitterCarouselItemTableViewCell")

    let cell: SplitterCarouselItemTableViewCell = tableView.dequeueReusableCell(withIdentifier: "splitterCarouselItemTableViewCell") as! SplitterCarouselItemTableViewCell
    var item = ((allBillSplitters[tableView.tag].items)?.allObjects as! [Item])[indexPath.row]
    if allBillSplitters[tableView.tag].isMainBillSplitter {
        getMainBillSplitterItems(splitter: allBillSplitters[tableView.tag])
        item = mainBillSplitterItems[indexPath.row]
    }
    let count = item.billSplitters?.count

    if count! > 1 {
        cell.name!.text = "\(item.name!)\nsplit \(count!) ways"
        cell.price!.text = "£\(Double(item.price)/Double(count!))"

    } else {
        cell.name!.text = item.name!
        cell.price!.text = "£\(item.price)"
    }

    return cell
}

细胞:

import UIKit

class SplitterCarouselItemTableViewCell: UITableViewCell {

    var name: UILabel!
    var price: UILabel!
    var view: UIView!

    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:)")
    }

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: "splitterCarouselItemTableViewCell")

        self.backgroundColor = .clear

        let width = Int(contentView.bounds.width)
        let height = Int(contentView.bounds.height)

        view = UIView(frame: CGRect(x: 0, y: 2, width: width, height: height - 4 ))
        view.backgroundColor = UIColor.white.withAlphaComponent(0.5)

        let viewHeight = Int(view.bounds.height)
        let viewWidth = Int(view.bounds.width)

        price = UILabel(frame: CGRect(x: viewWidth - 80, y: 0, width: 75, height: viewHeight))
        price.font = UIFont.systemFont(ofSize: 15.0)
        price.textAlignment = .right

        name = UILabel(frame: CGRect(x: 5, y: 0, width: width, height: viewHeight))
        name.font = UIFont.systemFont(ofSize: 15.0)
        name.numberOfLines = 0

        view.addSubview(name)
        view.addSubview(price)

        contentView.addSubview(view)
    }
}

我知道init不应该填充所有代码,稍后会提取。它就在那里,直到我解决宽度问题。

感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

挑战在于tableView.dequeueReusableCell(withIdentifier:)方法间接调用init(frame:)课程中的UITableViewCell。有几种方法可以解决这个问题:

  1. 覆盖init(frame:)中的SplitterCarouselItemTableViewCell并在您拨打super.init(frame:)时设置您的宽度
  2. 修改cell.frame方法
  3. 中的func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath)

    使用Autolayout对UITableView单元有问题。你不想搞乱那里的所有子视图。

答案 1 :(得分:0)

试试添加这个

    cell.frame = CGRect(x: CGFloat(Int(cell.frame.minX)),
                        y: CGFloat(Int(cell.frame.minY)),
                        width: tableView.frame.width,
                        height: cell.frame.height)

给你的

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

这为我解决了问题。

我的 UITableViewController 和 UITableViewCell 以编程方式完成

相关问题