【问题标题】:Swift TableView Cell [closed]Swift TableView 单元[关闭]
【发布时间】:2021-04-21 07:28:03
【问题描述】:

我正在尝试创建 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
        }
    }

【问题讨论】:

  • 为此请移除 func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { return 200 }。从代码。其设置Cell的静态高度。在 Storyboard 中为您的单元格组件提供高度。
  • 我的故事板中没有单元格。我以编程方式完成了这一切。这是唯一的方法吗?我没有碰过故事板。
  • 因此,当您以编程方式设置单元格项目时,请提供它们的高度。
  • 那个也不行,图片和txt和单元格高度一样
  • 即使我想改变我的 txt 或图像的位置,也没有任何移动

标签: swift uitableview tableview cell


【解决方案1】:

那是因为你使用的是 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, y: 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
}

【讨论】:

  • 好的,我知道你在说什么。我尝试了你写的东西,这是向前迈出的一步。您会建议我改用故事板还是完全以编程方式。
  • 可以完全以编程方式完成,如果您看到故事板的来源,他们正在以不同的格式做类似的事情。您仍然需要为这两个视图设置约束,以便它们可以在不同的屏幕尺寸下工作。在情节提要中,您可以拖放并设置这些值。我个人更喜欢在 storyboard 中工作,但是当多个开发人员在同一个 storyboard 中工作并提出单独的 pull request 时,很容易引起 git 冲突。
  • 好的,我会尝试故事板。菜鸟问题。如果我现在想在情节提要中做我的表格视图,我该如何让它与我现在拥有的代码一起工作?还是我需要完全重构我的代码
  • 2 种方式,1 是将 UITableView 作为 View 添加到 Storyboard 中当前的 UIViewController 中,然后将其链接到 tableView 变量。 ~~~~~~ 另一种方法是在类和情节提要中替换您的 UITableView。第二种方式需要进行大量更改,但您可以尝试、尝试并了解它是如何工作的。
  • 最好找一个关于如何创建新tableView的在线教程,因为你需要学习如何链接数据源和委托。
猜你喜欢
  • 2015-04-20
  • 2019-09-08
  • 2014-12-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-18
  • 2016-09-03
相关资源
最近更新 更多