微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Swift TableView 单元格

如何解决Swift TableView 单元格

我正在尝试创建 tableView 单元格并自定义布局。但似乎我在我的单元格中添加的任何东西都会卡住与单元格本身具有相同大小的高度和宽度。我怎样才能按照我的喜好设计我的细胞?即,使我的图像具有边距和填充的特定大小。甚至我的文本高度也被拉伸到单元格高度。我添加了边框,以便我的意思可见。我的标签的高度应该只是文本本身的大小,但它仍在拉伸。

我被困在 tableview 单元格上,想要完全以编程方式设计我的单元格布局,而无需故事板。我一整天都在努力自学,然后一直在兜圈子。

import UIKit
    
    class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
        
        let tableView = UITableView()
        
        override func viewDidLoad() {
            super.viewDidLoad()
            
            self.tableView.register(UITableViewCell.self,forCellReuseIdentifier: "cell")
            self.tableView.delegate = self
            self.tableView.dataSource = self
            self.view.addSubview(self.tableView)
        }
        
        override func viewDidLayoutSubviews() {
            super.viewDidLayoutSubviews()
            tableView.frame = view.bounds
        }
        
        func tableView(_ tableView: UITableView,didSelectRowAt indexPath: IndexPath) {
            tableView.deselectRow(at: indexPath,animated: true)
        }
        
        func tableView(_ tableView: UITableView,heightForRowAt indexPath: IndexPath) -> CGFloat {
            return 200
        }
        
        func tableView(_ tableView: UITableView,numberOfRowsInSection section: Int) -> Int {
            return 8
        }
        
        func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCell(withIdentifier: "cell",for: indexPath)
            
            cell.textLabel?.text = "Here is some text"
            cell.textLabel?.layer.cornerRadius = 10
            cell.textLabel?.layer.borderWidth = 5
            cell.textLabel?.layer.borderColor = UIColor.black.cgColor
            
            cell.imageView?.image = UIImage(named: "spiderman")!
            cell.imageView?.layer.cornerRadius = 10
            cell.imageView?.layer.borderWidth = 5
            cell.imageView?.layer.borderColor = UIColor.black.cgColor
            return cell
        }
    }

enter image description here

enter image description here

enter image description here

解决方法

那是因为你使用了 UITableViewCell 的默认 textLabel

如果您打开故事板并使用具有“基本”样式的单元格,您将看到文本始终居中对齐。

我不认为您可以直接调整这两个预定义属性(.textLabel 和 .imageView)的高度或位置。

尽量手动创建imageView和label,不要直接使用cell.textLabel。

示例:

func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell",for: indexPath)
    
    let label = UITextView(frame: CGRect(x: 200,y: 0,width: 500,height: 50))
    label.text = "Here is some text"
    label.layer.cornerRadius = 10
    label.layer.borderWidth = 5
    label.layer.borderColor = UIColor.black.cgColor
    cell.addSubview(label)
    
    let image = UIImage(named: "spiderman")!
    let imageView = UIImageView(frame: CGRect(x: 0,width: 200,height: 200))
    imageView.image = image
    imageView.layer.cornerRadius = 10
    imageView.layer.borderWidth = 5
    imageView.layer.borderColor = UIColor.black.cgColor
    cell.addSubview(imageView);
    return cell
}

enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。