简体   繁体   English

如何在表格视图单元格中以编程方式添加视图,Swift

[英]How to add views programmaticaly in tableview cell, Swift

Please help.请帮忙。 I want to create table view cell content absolutely programmatically.我想绝对以编程方式创建表格视图单元格内容。 But it's not wroking ( Where I'm wrong? My code:但它并没有起作用(我错在哪里?我的代码:

import Foundation
import UIKit

class FiltersMenuCell: UITableViewCell {
    var customFilters = [SearchRequestRestModelItem]()
    var filtersCounters: NSDictionary? = nil
    var isMyFilters = true

    @IBOutlet weak var mainView: UIView!

    func initFilters() {
        let customFiltersSize = customFilters.count
        var previousFilterNameLabel: UILabel? = nil
        for index in 0..<customFiltersSize {
            let filterNameLabel = UILabel()
            filterNameLabel.font = UIFont(name: Constants.regularFontName, size: 16)
            filterNameLabel.numberOfLines = 1
            filterNameLabel.lineBreakMode = .byTruncatingTail
            filterNameLabel.textColor = UIColor.black
            filterNameLabel.text = "My filters".localized

            mainView.addSubview(filterNameLabel)

            filterNameLabel.leadingAnchor.constraint(equalTo: mainView.leadingAnchor, constant: 8).isActive = true

            filterNameLabel.trailingAnchor.constraint(equalTo: mainView.trailingAnchor, constant: 8).isActive = true
            filterNameLabel.topAnchor.constraint(equalTo: mainView.topAnchor, constant: 8).isActive = true
            filterNameLabel.bottomAnchor.constraint(equalTo: mainView.bottomAnchor, constant: 8).isActive = true 
            ...

Cell intialization:细胞初始化:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCell(withIdentifier: Constants.filtersMenuCell) as! FiltersMenuCell
            cell.isMyFilters = indexPath.row == 4

            cell.customFilters = cell.isMyFilters ? presenter!.getMyFilters(allCustomFilters: customFilters)
                    : presenter!.getSharedFilters(allCustomFilters: customFilters)
            cell.filtersCounters = allTicketsCountModel.customFiltersCounters
            cell.initFilters()

            return cell)
    }

Why it's not working?为什么它不起作用? Thank you in advance!先感谢您!

Your constraints on views are not working, so you need to add first您对视图的约束不起作用,因此您需要先添加

yourMainView.translatesAutoresizingMaskIntoConstraints = false
yourLabel.translatesAutoresizingMaskIntoConstraints = false   

// and add below code in you cellForRow AtIndexPath
DispatchQueue.main.async {
cell.setNeedsLayout()
cell.layoutIfNeeded()
}

Maybe your filterNameLabel constraints are not properly created.也许您的 filterNameLabel 约束未正确创建。 You need to set the first index filterNameLabel top constraint equal to mainView top constraint.您需要将第一个索引 filterNameLabel 顶部约束设置为等于 mainView 顶部约束。 and last index filterNameLabel bottom constraint equal to mainView bottom constraint.最后一个索引 filterNameLabel 底部约束等于 mainView 底部约束。 And other constraints is relative, that means next filterNameLabel top constraint equal to previous filterNameLabel bottom constraint.其他约束是相对的,这意味着下一个 filterNameLabel 顶部约束等于前一个 filterNameLabel 底部约束。 Always call layout if needed method.如果需要,请始终调用布局方法。

On the other way if you want to take another tableView inside FiltersMenuCell, you can follow these steps:另一方面,如果您想在 FiltersMenuCell 中使用另一个 tableView,您可以按照以下步骤操作:

1) Create a tableView inside FiltersMenuCell class. 1) 在 FiltersMenuCell 类中创建一个 tableView。 Initialize this closure into this class.将此闭包初始化为此类。

@IBOutlet weak var tableView: UITableView!

var tableViewContentSizeHeight: CGFloat {
    view.layoutIfNeeded()
    return tableView.contentSize.height
}

2) Create "mainView" height constraint into FiltersMenuCell class globally. 2)全局创建“mainView”高度约束到FiltersMenuCell类中。

var mainView: UIView!
var heightConstraint: NSLayoutConstraint!

heightConstraint = mainView.heightAnchor.constraint(equalToConstant: 0)
heightConstraint.isActive = true

3) Update "heightConstraint" from cellForRowAt 3) 从 cellForRowAt 更新“heightConstraint”

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as? FiltersMenuCell {
        cell.reloadData()
        cell.heightConstraint.constant = cell.tableViewContentSizeHeight
        return cell
    }
    return UITableViewCell()
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM