У меня есть массив расширяемых кнопок с одинаковым названием «Открыть».Я бы хотел, чтобы у каждой кнопки было свое имя, поскольку все они будут иметь свои особенности.Как мне настроить каждый заголовок кнопки на что-то уникальное?Должен ли я создать каждую кнопку самостоятельно и отойти от расширяемого массива?
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let button = UIButton(type: .system)
button.setTitle("Open", for: .normal)
button.setTitleColor(.black, for: .normal)
button.backgroundColor = UIColor.lightGray
button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 14)
button.addTarget(self, action: #selector(handleExpandClose), for: .touchUpInside)
button.tag = section
return button
}
var showIndexPaths = true
@objc func handleExpandClose(button: UIButton) {
print("trying to expand and close section")
print(button.tag)
let section = button.tag
var indexPaths = [IndexPath]()
button.setTitle(isExpanded ? "Open" : "Close", for: .normal)
if isExpanded {
tableView.deleteRows(at: indexPaths, with: .fade)
}else{
tableView.insertRows(at: indexPaths, with: .fade)
}
}
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 40
}
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if !twodimensionalArray[section].isExpanded {
return 0
}
return twodimensionalArray[section].list.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath)
return cell
}
}
Я также выложу картинку для описания.![enter image description here](https://i.stack.imgur.com/JEC5a.png)