【问题标题】:Expanding UITableViewCell not reading struct elements扩展 UITableViewCell 不读取结构元素
【发布时间】:2019-12-30 13:37:02
【问题描述】:

我已经创建了一个 UITableViewCotroller,我正在尝试让它具有扩展单元格。 当我单击一个单元格时,它正在扩展。但是,展开的单元格的文本与标题相同。因此,当我展开标题 1 时,新展开的单元格都说标题 1。同样与标题 2 等一样。当我展开它时,新展开的单元格应该有标题单元格 1、2 等。它们似乎没有从结构中读取我建立。 我只是在学习如何做到这一点,所以我错过了一些非常简单的东西,但我似乎找不到哪里出错了。

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()
    }
}
}

【问题讨论】:

    标签: arrays swift uitableview struct tableview


    【解决方案1】:

    通过添加 cell.textLabel?.text = tableViewData[indexPath.section].sectionData[indexPath.row] 而不是 cell.textLabel?.text = tableViewData[indexPath.section].title 来解决这个问题

    不知道我是怎么错过的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-04-30
      • 1970-01-01
      • 1970-01-01
      • 2017-09-01
      • 2023-03-30
      • 1970-01-01
      • 2018-05-12
      相关资源
      最近更新 更多