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. You need to set the first index filterNameLabel top constraint equal to mainView top constraint. and last index filterNameLabel bottom constraint equal to mainView bottom constraint. And other constraints is relative, that means next filterNameLabel top constraint equal to previous filterNameLabel bottom constraint. Always call layout if needed method.
On the other way if you want to take another tableView inside FiltersMenuCell, you can follow these steps:
1) Create a tableView inside FiltersMenuCell class. 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.
var mainView: UIView!
var heightConstraint: NSLayoutConstraint!
heightConstraint = mainView.heightAnchor.constraint(equalToConstant: 0)
heightConstraint.isActive = true
3) Update "heightConstraint" from cellForRowAt
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()
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.