首页 > 解决方案 > 如何增加 UITableView 中自定义单元格的宽度

问题描述

我已经使用自定义 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(_:))
    }

}

我的视图控制器如下:

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
    }

}

请查看结果: 附上截图

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

标签: iosswiftuitableview

解决方案


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

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

推荐阅读