ios - 单元格宽度对于表格 View 宽度来说太大

标签 ios swift uitableview swift3

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

表格 View :

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 不应该填充所有代码,稍后将被提取。它就在那里,直到我解决宽度问题。

感谢您的帮助。

最佳答案

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

  1. 重写 SplitterCarouselItemTableViewCell 中的 init(frame:) 并在调用 super.init(frame:) 时设置宽度
  2. >
  3. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) 方法中修改 cell.frame

对于 UITableView 单元格使用自动布局会出现问题。您不想弄乱那里的所有 subview 。

关于ios - 单元格宽度对于表格 View 宽度来说太大,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42096158/

相关文章:

ios - 不同的苹果证书

ios - 如果我的数组中有数据,空 TableView 标签会闪烁一秒钟

iOS 如何将 View 添加到屏幕中心并将其粘贴到 UITableViewController 上的屏幕中心?

ios - UITableView numberOfSectionsInTableView 和 numberOfRowsInSection 在初始加载时调用了三次

ios - UITableView 中按月划分的部分

android - Google Play 游戏服务多人 iOS 和 Android

android - 在Android上相当于AVQueuePlayer

ios - 如何为结构的所有常量编写扩展?

iphone - 如何注册其他应用程序的通知?

swift - SwiftUI 中的预览真的需要#if DEBUG 语句才能在发布版本中将其删除吗?