我已经创建了一个UITableViewCotroller,并且试图使其具有扩展的单元格。
当我单击一个单元格时,它正在扩展。但是,展开的单元格的文本与标题相同。因此,当我展开标题1时,新展开的单元格都说出了标题1。同样,标题2也如此。当我展开它时,新展开的单元格应该具有标题单元格1、2等。它们似乎没有从结构中读取我创建。
我只是在学习如何执行此操作,因此我遗漏了一些非常简单的内容,但似乎找不到错误的地方。 答案 0 :(得分:1) 通过添加 不知道我是怎么想念的。private let reuseidentifier = "Cell"
struct cellData {
var opened = Bool()
var title = String()
var sectionData = [String]()
}
//next contact
struct Contact {
var fullname: String
var hello: String
}
class ContactController: UITableViewController {
//new
var tableViewData = [cellData]()
var contacts = [Contact]()
override func viewDidLoad() {
super.viewDidLoad()
//new
tableViewData = [cellData(opened: false, title: "title1", sectionData: ["cell1" , "cell2", "cell3"]),
cellData(opened: false, title: "title2", sectionData: ["cell1" , "cell2", "cell3"]),
cellData(opened: false, title: "title3", sectionData: ["cell1" , "cell2", "cell3"]),
cellData(opened: false, title: "title4", sectionData: ["cell1" , "cell2", "cell3"]),
cellData(opened: false, title: "title5", sectionData: ["cell1" , "cell2", "cell3"])]
self.navigationController?.navigationBar.prefersLargeTitles = true
self.navigationItem.title = "Contacts"
self.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(handleAddContact))
view.backgroundColor = .white
tableView.register(UITableViewCell.self, forCellReuseIdentifier: reuseidentifier)
}
@objc func handleAddContact () {
//here
let controller = AddContactController()
controller.delegate = self
self.present(UINavigationController(rootViewController: controller), animated: true, completion: nil)
}
//UITABLEVIEW
override func numberOfSections(in tableView: UITableView) -> Int {
return tableViewData.count
}
override func tableView(_ tableView: UITableView,
numberOfRowsInSection section: Int) -> Int {
if tableViewData[section].opened == true {
return tableViewData[section].sectionData.count
}else {
return 1
}
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
//old needed let cell = tableView.dequeueReusableCell(withIdentifier: reuseidentifier, for: indexPath)
// cell.textLabel?.text = contacts[indexPath.row].fullname
// return cell
if indexPath.row == 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: reuseidentifier, for: indexPath)
cell.textLabel?.text = tableViewData[indexPath.section].title
return cell
}else {
//use a different cell identifier if needed
let cell = tableView.dequeueReusableCell(withIdentifier: reuseidentifier, for: indexPath)
// cell.textLabel?.text = tableViewData[indexPath.section].sectionData[indexPath.row]
cell.textLabel?.text = tableViewData[indexPath.section].title
return cell
}
}
//Clicking on row
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if tableViewData[indexPath.section].opened == true {
tableViewData[indexPath.section].opened = false
let sections = IndexSet.init(integer: indexPath.section)
tableView.reloadSections(sections, with: .none) //play around with animation
}else {
tableViewData[indexPath.section].opened = true
let sections = IndexSet.init(integer: indexPath.section)
tableView.reloadSections(sections, with: .none) //play around with animation
}
}
}
//extension for VC2
extension ContactController: AddContactDelegate {
func addContact(contact: Contact) {
self.dismiss(animated: true) {
self.contacts.append(contact)
self.tableView.reloadData()
}
}
}
1 个答案:
cell.textLabel?.text = tableViewData[indexPath.section].sectionData[indexPath.row]
而不是cell.textLabel?.text = tableViewData[indexPath.section].title