[英]How to add views programmaticaly in tableview cell, Swift
請幫忙。 我想絕對以編程方式創建表格視圖單元格內容。 但它並沒有起作用(我錯在哪里?我的代碼:
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
...
細胞初始化:
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)
}
為什么它不起作用? 先感謝您!
您對視圖的約束不起作用,因此您需要先添加
yourMainView.translatesAutoresizingMaskIntoConstraints = false
yourLabel.translatesAutoresizingMaskIntoConstraints = false
// and add below code in you cellForRow AtIndexPath
DispatchQueue.main.async {
cell.setNeedsLayout()
cell.layoutIfNeeded()
}
也許您的 filterNameLabel 約束未正確創建。 您需要將第一個索引 filterNameLabel 頂部約束設置為等於 mainView 頂部約束。 最后一個索引 filterNameLabel 底部約束等於 mainView 底部約束。 其他約束是相對的,這意味着下一個 filterNameLabel 頂部約束等於前一個 filterNameLabel 底部約束。 如果需要,請始終調用布局方法。
另一方面,如果您想在 FiltersMenuCell 中使用另一個 tableView,您可以按照以下步驟操作:
1) 在 FiltersMenuCell 類中創建一個 tableView。 將此閉包初始化為此類。
@IBOutlet weak var tableView: UITableView!
var tableViewContentSizeHeight: CGFloat {
view.layoutIfNeeded()
return tableView.contentSize.height
}
2)全局創建“mainView”高度約束到FiltersMenuCell類中。
var mainView: UIView!
var heightConstraint: NSLayoutConstraint!
heightConstraint = mainView.heightAnchor.constraint(equalToConstant: 0)
heightConstraint.isActive = true
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.