我在UITableView
内部有一个UITableViewCell
。我根据以下数组设置了父/子表视图的行数:
articlesArray = [articleTemplateStruct(articleName: "", clauses: [""], payments: [paymentStruct(amount: "", desc: "")]), articleTemplateStruct(articleName: "", clauses: ["", ""], payments: [paymentStruct(amount: "", desc: ""), paymentStruct(amount: "", desc: "")]) ]
对于第一个tableView我设置了部分:
func numberOfSections(in tableView: UITableView) -> Int {
if(tableView == articleTableView){
return articlesArray.count
}else{
return 1
}
}
对于第二个tableView我设置numberOfRowsInSection:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if(tableView == articleTableView){
return 1
}else if(tableView.tag == 10){
return articlesArray[section].clauses.count
}else{
return 1
}
}
问题在这里,当我想对带有标签10的表使用节时,在numberOfRowsInSection函数中,我需要访问其中包含tableview的articleTableView节的节!
我不知道任何人都无法理解我想要什么!
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if(tableView == articleTableView){
let cell = tableView.dequeueReusableCell(withIdentifier: "ContractTemplate", for: indexPath) as! ContractTemplateTableViewCell
self.functions.enterValue(textfield: cell.articleTitle, phText: langc.article)
cell.separatorInset = .zero
cell.clauseTableView.register(UINib(nibName: "ContractTemplateSubjectsTableViewCell", bundle: nil), forCellReuseIdentifier: "ContractTemplateSubjects")
cell.paymentTableView.register(UINib(nibName: "ContractTemplatePaymentTableViewCell", bundle: nil), forCellReuseIdentifier: "ContractTemplatePayment")
cell.clauseTableView.delegate = self
cell.clauseTableView.dataSource = self
cell.clauseTableView.reloadData()
cell.clauseTableView.tableFooterView = UIView()
cell.clauseTableView.sizeToFit()
cell.clauseTableConst.constant = cell.clauseTableView.contentSize.height
let artTableHeight = self.articleTableView.contentSize.height
tableViewClause = cell.clauseTableView
clauseTableFlag = true
cell.paymentTableView.delegate = self
cell.paymentTableView.dataSource = self
cell.paymentTableView.reloadData()
cell.paymentTableView.tableFooterView = UIView()
cell.paymentTableConst.constant = cell.paymentTableView.contentSize.height
articleTableConst.constant = artTableHeight + cell.clauseTableView.contentSize.height + cell.paymentTableView.contentSize.height
containerHeightConst.constant = articleTableView.contentSize.height + 281
cell.selectionStyle = .none
cell.add.tag = indexPath.section
cell.add.addTarget(self, action: #selector(addArticle), for: .touchUpInside)
return cell
}else if(tableView.tag == 10){
let cell = tableView.dequeueReusableCell(withIdentifier: "ContractTemplateSubjects", for: indexPath) as! ContractTemplateSubjectsTableViewCell
self.functions.textArea(textArea: cell.subjectDesc, text: self.langc.descriptionTxt)
cell.tag = indexPath.section*1000 + indexPath.row
cell.add.addTarget(self, action: #selector(addClause), for: .touchUpInside)
cell.separatorInset = .zero
return cell
}
else{
let cell = tableView.dequeueReusableCell(withIdentifier: "ContractTemplatePayment", for: indexPath) as! ContractTemplatePaymentTableViewCell
cell.paymentTitle.textColor = titleColor
cell.paymentTitle.text = self.langc.payment + " " + String(indexPath.row+1)
self.functions.enterValue(textfield: cell.amount, phText: self.langc.amount)
self.functions.enterValue(textfield: cell.shortText, phText: self.langc.shortText)
cell.add.tag = indexPath.section*1000 + indexPath.row
cell.add.addTarget(self, action: #selector(addPayment), for: .touchUpInside)
cell.separatorInset = .zero
return cell
}
最佳答案
我想介绍一种更好的结构,以使每个类都可以承担单个责任,例如articleTableView的委托方法将在其父类之内,子句TableView的委托方法将在单元格之内,如下所示:
ViewController.swift
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
private let tableView = UITableView()
var articlesArray = [
ArticleTemplate(articleName: "", clauses: ["A1"], payments: [Payment(amount: "", desc: "")]),
ArticleTemplate(articleName: "", clauses: ["B1", "B2"], payments: [Payment(amount: "", desc: ""), Payment(amount: "", desc: "")])
]
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = .white
self.tableView.backgroundColor = .clear
self.tableView.delegate = self
self.tableView.dataSource = self
self.tableView.rowHeight = UITableViewAutomaticDimension
self.tableView.register(ArticleTableViewCell.self, forCellReuseIdentifier: "ArtCellId")
self.tableView.translatesAutoresizingMaskIntoConstraints = false
self.tableView.tableFooterView = UIView()
self.view.addSubview(tableView)
tableView.topAnchor.constraint(equalTo: self.view.layoutMarginsGuide.topAnchor).isActive = true
tableView.bottomAnchor.constraint(equalTo: self.view.layoutMarginsGuide.bottomAnchor).isActive = true
tableView.leadingAnchor.constraint(equalTo: self.view.layoutMarginsGuide.leadingAnchor).isActive = true
tableView.trailingAnchor.constraint(equalTo: self.view.layoutMarginsGuide.trailingAnchor).isActive = true
}
func numberOfSections(in tableView: UITableView) -> Int {
return self.articlesArray.count
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ArtCellId", for: indexPath) as! ArticleTableViewCell
cell.setData(article: self.articlesArray[indexPath.section])
return cell
}
}
ArticleTableViewCell.swift
class ArticleTableViewCell: UITableViewCell, UITableViewDataSource, UITableViewDelegate {
let clausesTableView = UITableView()
let clausesTableViewHeight: NSLayoutConstraint
var clauses: [String]!
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
self.clausesTableViewHeight = clausesTableView.heightAnchor.constraint(equalToConstant: 0)
super.init(style: style, reuseIdentifier: reuseIdentifier)
self.setupUI()
self.clausesTableViewHeight.isActive = true
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func prepareForReuse() {
super.prepareForReuse()
self.clauses = nil
self.clausesTableView.dataSource = nil
self.clausesTableView.delegate = nil
}
private func setupUI() {
self.selectionStyle = .none
self.clausesTableView.isScrollEnabled = false
self.clausesTableView.register(UITableViewCell.self, forCellReuseIdentifier: "CellId")
self.clausesTableView.translatesAutoresizingMaskIntoConstraints = false
self.contentView.addSubview(clausesTableView)
self.clausesTableView.topAnchor.constraint(equalTo: self.contentView.layoutMarginsGuide.topAnchor).isActive = true
self.clausesTableView.bottomAnchor.constraint(equalTo: self.contentView.layoutMarginsGuide.bottomAnchor).isActive = true
self.clausesTableView.leadingAnchor.constraint(equalTo: self.contentView.layoutMarginsGuide.leadingAnchor).isActive = true
self.clausesTableView.trailingAnchor.constraint(equalTo: self.contentView.layoutMarginsGuide.trailingAnchor).isActive = true
}
func setData(article: ArticleTemplate) {
self.clauses = article.clauses
self.clausesTableViewHeight.constant = CGFloat(44 * self.clauses.count)
self.clausesTableView.delegate = self
self.clausesTableView.dataSource = self
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.clauses.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CellId", for: indexPath)
cell.textLabel?.text = self.clauses[indexPath.row]
return cell
}
}
还更改了一些成员/结构名称,以遵循命名约定:
struct Payment {
let amount: String
let desc: String
}
struct ArticleTemplate {
let articleName: String
let clauses: [String]
let payments: [Payment]
}
关于swift - 根据父UITableVIew的部分索引设置嵌套的UITableView的行数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51462685/