代码之家  ›  专栏  ›  技术社区  ›  Mailo Světel

在UITableViewCell中定位元素

  •  1
  • Mailo Světel  · 技术社区  · 7 年前

    import UIKit
    
    class LinkCellView: UITableViewCell {
        @IBOutlet weak var cellTitleLabel: UILabel!
        @IBOutlet weak var tagsListView: UIView!
    
        override func awakeFromNib() {
            super.awakeFromNib()
            // Initialization code
        }
    
        override func setSelected(_ selected: Bool, animated: Bool) {
            super.setSelected(selected, animated: animated)
    
            // Configure the view for the selected state
        }
    
    }
    

    Table View Cell in IB

    我来填充 tagsListView cellForRowAt

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "linkCell", for: indexPath) as! LinkCellView
        let object = links[indexPath.row]
    
        cell.cellTitleLabel!.text = object.description
        let tag = UILabel(frame: CGRect(x: 0, y: 0, width: 200, height: 21))
        tag.text = "Frkul"
        cell.tagsListView.addSubview(tag)
    
        return cell
    }
    

    不幸的是,当我在模拟器中运行应用程序时,标签列表几乎看不见。我的期望是看到整个标签列表。

    Resulting cell on iOS

    • Xcode 10.0版
    • iOS 12版

    https://gitlab.com/lipoqil/stackview-in-table-cell

    3 回复  |  直到 7 年前
        1
  •  1
  •   Mailo Světel    7 年前

    好吧,出于某种原因,如果我用 UIStackView UIView ,它的显示几乎如我所愿。

    Table rows in simulator

    它在代码中引入了一个变化

    cell.tagListView.addSubview(tag) cell.tagListView.addArrangedSubview(tag)

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "linkCell", for: indexPath) as! LinkCellView
        cell.tagListView.subviews.forEach { $0.removeFromSuperview() }
        let object = links[indexPath.row]
    
        cell.cellTitleLabel!.text = object.description
        let tag = UILabel(frame: CGRect(x: 0, y: 0, width: 200, height: 21))
        tag.shadowColor = .blue
        tag.text = object.tags
        cell.tagListView.addArrangedSubview(tag)
    
        return cell
    }
    

    我仍然需要解决,如何在那里安装标签,如何使它看起来更像标签,但我相信这已经超出了最初的问题。

        2
  •  0
  •   Mahgolsadat Fathi    7 年前

    如果您有一个标记集合,您需要在tableView单元格中实现一个集合视图;如果您只有一个标记,那么就在xib中实现它?

        3
  •  0
  •   Mailo Světel    7 年前

    这是我的密码。

    func numberOfSections(in tagsCV: UICollectionView) -> Int {
        return 1
    }
    
    func collectionView(_ tagsCV: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return videoObject?.tags?.count ?? 0
    }
    
    func collectionView(_ tagsCV: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize{
        let fontAttributes = [NSAttributedStringKey.font: StylesBook.hashtag.value.textFont]
        let size = (videoObject?.tags?[indexPath.row] ?? "" as String).size(withAttributes: fontAttributes)
        return CGSize(width: size.width + CGFloat(20) , height: 50)
    
    }
    
    func collectionView(_ tagsCV: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = tagsCV.dequeueReusableCell(withReuseIdentifier: "TagCollectionViewCell", for: indexPath) as! TagCollectionViewCell
        cell.tagBtn.setTitle(englishNumToPersian(text: videoObject?.tags?[indexPath.row] ?? "")  , for: .normal)
        cell.tagBtn.addTarget(self, action: #selector(tagTaped(sender:)), for: .touchDown)
        cell.transform = CGAffineTransform(scaleX: -1, y: 1)
        return cell
    }
    

    关键是你需要设置你的收藏视图的滚动方向 horizontal . 这样,无论你有1个或1000个标签,它们都会完美地显示出来,按钮(或标签)的宽度将适合其内容。我建议您使用button,而不是label,并禁用它的用户交互以充当标签。

    推荐文章