gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 12:42:14 25 4
gpt4 key购买 nike

我有一个嵌套在轮播中的程序化表格 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com