gpt4 book ai didi

ios - 如何增加 UITableView 中自定义单元格的宽度

转载 作者:行者123 更新时间:2023-12-01 18:35:53 24 4
gpt4 key购买 nike

我已经使用自定义 UITableViewCell 创建了 UITableView。但是我得到的问题是单元格的宽度不是帧宽度,尽管我已经在 CGReact 中分配了。请查看我的代码:

CustomTableViewCell 类:

import UIKit

class CustomTableViewCell: UITableViewCell {

lazy var backView : UIView = {
let view = UIView(frame: CGRect(x: 10, y: 6, width: self.frame.width, height: 76))
view.backgroundColor = .red
view.layer.applySketchShadow()
return view
}()

override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}

lazy var iconTime : UIImageView = {
var object = UIImageView(frame: CGRect(x: 10, y: 54, width: 12, height: 12))
object.image = #imageLiteral(resourceName: "clock")
return object
}()

lazy var notification : UILabel = {
var object = UILabel(frame: CGRect(x: 10, y: 7, width: backView.frame.width, height: 40))
object.adjustsFontSizeToFitWidth = true
object.minimumScaleFactor = 0.5
object.font = object.font.withSize(28.0)
object.numberOfLines = 3
return object
}()

lazy var notificationTime : UILabel = {
var object = UILabel(frame: CGRect(x: 30, y: 40, width: backView.frame.width, height: 40))
object.adjustsFontSizeToFitWidth = true
object.minimumScaleFactor = 0.5
object.font = object.font.withSize(12.0)
return object
}()

override func layoutSubviews() {
contentView.backgroundColor = UIColor.clear
backgroundColor = UIColor.clear
backView.layer.cornerRadius = 5
backView.clipsToBounds = true
}

override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)

addSubview(backView)
[notification, notificationTime, iconTime].forEach(backView.addSubview(_:))
}

}

我的 View Controller 如下:
import UIKit

class UserModal {
var tableView = UITableView()
var notification: String?
var notificationTime : String?

init(notification: String, notificationTime: String) {
self.notification = notification
self.notificationTime = notificationTime
}
}

class newNotificationController : UIViewController {
var tableView = UITableView()
var userMod = [UserModal]()

override func viewDidLoad() {
super.viewDidLoad()
setTableView()

userMod.append(UserModal(notification: "Data ", notificationTime: "Time"))
userMod.append(UserModal(notification: "This is some Notification which needs to be populated in the Grid view for testing but lets see what is happening here!! ", notificationTime: "12-12-1212 12:12:12"))
userMod.append(UserModal(notification: "Data ", notificationTime: "Time"))
}

func setTableView() {
tableView.frame = self.view.frame
tableView.backgroundColor = UIColor.clear
tableView.delegate = self
tableView.dataSource = self
tableView.separatorColor = UIColor.clear
self.view.addSubview(tableView)
tableView.register(CustomTableViewCell.self, forCellReuseIdentifier: "cell")
}

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
navigationController?.setNavigationBarHidden(true, animated: animated)
}

override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
navigationController?.setNavigationBarHidden(false, animated: animated)
}

}

extension newNotificationController: UITableViewDelegate , UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return userMod.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as? CustomTableViewCell else { fatalError("Unable to populate Notification History")}
cell.notification.text = userMod[indexPath.row].notification
cell.notificationTime.text = userMod[indexPath.row].notificationTime
return cell
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 85
}

}

请查看结果:
Screenshot attached

我不明白为什么我的单元格的宽度是框架的宽度。任何帮助将不胜感激。谢谢!!

最佳答案

问题在于这段代码中的框架宽度,不知何故自我的宽度不是设备的宽度,所以正因为如此,你正面临这个问题。

lazy var backView : UIView = {
let view = UIView(frame: CGRect(x: 10, y: 6, width: self.frame.width, height: 76))
view.backgroundColor = .red
view.layer.applySketchShadow()
return view
}()

要解决此问题,您可以像这样设置框架
let view = UIView(frame: CGRect(x: 10, y: 6, width: UIScreen.main.bounds.size.width - 10, height: 76))

关于ios - 如何增加 UITableView 中自定义单元格的宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59320204/

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