【问题标题】:how to add header view to each of custom tableViewCells in swift如何在swift中将标题视图添加到每个自定义tableViewCells
【发布时间】:2021-02-26 17:12:44
【问题描述】:

我有两个自定义 TableViewCell。所以第一个 TableViewCell 就像一个最近的列表,它可以变得更长。但第二个单元格始终停留在底部。所以我需要添加持有 secondTableViewCell 的 UILabel。 the result that i need.

import UIKit

class bagPage: UIViewController, UITableViewDelegate, UITableViewDataSource {
    var itemsName: [String] = []
    var itemsPhoto: [UIImage] = []
   // these arrays will defined in other view controller
    
    @IBOutlet weak var tableView: UITableView!
        override func viewDidLoad() {
        super.viewDidLoad()
        
        tableView.dataSource = self
        tableView.delegate = self
       
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        
        if indexPath.row < itemsName.count{
            return 165
        }else{
            return 50
        }
        
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return  6 + itemsName.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        
        if indexPath.row < itemsName.count{
            
            let cell = tableView.dequeueReusableCell(withIdentifier: "bagTableViewCell") as? bagTableViewCell
            cell?.itemName.text = itemsName.last
            cell?.itemPhoto.image = itemsPhoto.last
            return cell!
        }
        if indexPath.row == itemsName.count {
            let cellTwo = tableView.dequeueReusableCell(withIdentifier: "extraBagPageTableView") as? extraBagPageTableView
            // here i'm hiding views in first row
            cellTwo?.textLabel?.text = "These are products that u can add to your cell"

            return cellTwo!
            
        }else{
            let cellTwo = tableView.dequeueReusableCell(withIdentifier: "extraBagPageTableView") as? extraBagPageTableView
            return cellTwo!
        }
        
    }
    
}

【问题讨论】:

  • 我想我的其他解决方案不是您想要的,请允许我再次尝试并提供帮助。我认为我的困惑是您所说的包含第二个 tableview 单元格的标签的方式。你的意思是什么,因为你的图片没有让问题更清楚?试着解释你所说的保持是什么意思。你想让第二个单元格中的标签做某事吗?
  • 看,我将两个 UITableViewCells 拖到了我的 tableView 中。第一个可以通过用户的操作变长。但第二个,总是停留在底部,我需要一个 UILabel 来描述第二个单元格的内容。我添加了我的故事板的照片
  • 否则它们在第一个单元格中可以是无限行,但第二个单元格中只有五个,并且标签应该像第二个单元格的第一行。我希望你能理解我。
  • 我将两个 UITableViewCells 拖到了我的 tableView 中。 OK - 第一个可以通过用户的操作变得更长。好的 - 但第二个,总是留在底部好的 - 我需要一个 UILabel 来描述第二个单元格的内容。不行 你需要解释一下这个标签是什么。图片不解释。
  • 否则它们在第一个单元格中可以是无限行 好的,我知道根据用户交互,单元格可以变长 - 第二个单元格您还没有清楚地解释它的不同之处。我真的很想帮助你,但我很难理解第二个单元格中的标签是做什么的

标签: ios swift uitableview uitableviewsectionheader


【解决方案1】:

这是提供解决方案的另一种尝试。这是你要找的吗?

这是项目的链接 https://drive.google.com/file/d/1XlSF4fGiNzOqQHauiNo7TuFhKUTrFbsJ/view?usp=sharing

如果您需要更多帮助,请告诉我

【讨论】:

  • 如果“产品编号...”和“在此处添加更多产品”是两个不同的单元格,是的!
  • 它们是不同的细胞是的
  • 是的,看来这就是我想要实现的目标
  • 我添加了完整项目的链接
  • 我向项目发送了请求
【解决方案2】:

晚上好,所以我按照您的图片制作了代码,并按照您的 1 个表格视图部分、2 个不同单元格的约定。我尝试使用您的命名约定。注意我必须交换高度值,你把它们弄错了。

   class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    
        @IBOutlet weak var tableView: UITableView!
        var itemsName: [String] = ["Helmet", "Gloves", "Bindings", "Goggles", "Kneepads", "Boots", "Snowboard"]
        
        override func viewDidLoad() {
            super.viewDidLoad()
        }
        
        func numberOfSections(in tableView: UITableView) -> Int {
            return 1
        }
    
        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return 1 + itemsName.count
        }
        
        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            
            if indexPath.row < itemsName.count{
                let bagTableViewCell = tableView.dequeueReusableCell(withIdentifier: "BagTableViewCell") as? BagTableViewCell
                bagTableViewCell?.productsLabel.text = itemsName[indexPath.row]
                return bagTableViewCell!
            } else {
                let extraBagPageTableView = tableView.dequeueReusableCell(withIdentifier: "ExtraBagPageTableView") as? ExtraBagPageTableView
                return extraBagPageTableView!
            }
        }
        
        func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
            if indexPath.row < itemsName.count {
                return 50
            } else {
                return 165
            }
        }
    }

还有表格视图单元格。请注意,第二个单元格的名称不好。

class BagTableViewCell: UITableViewCell {
    @IBOutlet weak var additionalProductsLabel: UILabel!
    @IBOutlet weak var productsLabel: UILabel!
    @IBOutlet weak var productImage: UIImageView!
}

class ExtraBagPageTableView: UITableViewCell {
    
}

它看起来像这样

项目可以在这里找到。 (这次我正确设置了权限:))

TableCellWithLabel Project

【讨论】:

  • 你好。看起来就像你将 uilabel 添加到第二个单元格。但不要伪造我在第二个单元格中有 5 行
  • 看起来我找到了一个解决方案,但滚动时它是一个包。我在答案中定义了当前代码。你可以尝试在你的代码中实现它吗?
  • 我在第二个单元格中添加了一行,并隐藏了所有视图,而不是在其中添加了文本。但如果用户将 6-7 个项目添加到包中,则文本标签会更改位置。尝试向两个数组添加 7-8 个项目
  • 这就是为什么第一个解决方案更适合您的原因,使用节标题意味着无论第一节或第二节中有多少单元格,都只会有一个额外的标签。我建议我们切换回第一个解决方案,我将帮助您调整代码以使用它。这可能是大约 10 行额外的代码,但总体上是一个更好的解决方案。我将使用第一个解决方案更新项目,也许我们可以讨论不和谐或在 SO 聊天中完成。不过会晚一点。
  • 好的。我没有使用不和谐。应该发送链接或类似的东西?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-05
相关资源
最近更新 更多