4Manuals

  • PDF Cloud HOME

扩展UITableViewCell不读取结构元素 Download

    如何删除外部索引并合并内部json API集成以验证ResearchKit ORKFormItem 如何在Swift中将数组的每个元素与下一个数组的每个元素相乘? 字符串数组在for循环中初始化时忽略索引0 UITableView动画大小调整问题 通过键合并两个数组,并将值附加到另一个数组中 在Perl中将哈希转换为哈希数组 PDFkit无法快速选择本地文件URL 结构的原始输出 如何快速设置基本身份验证

我已经创建了一个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()
    }
}
}

1 个答案:

答案 0 :(得分:1)

通过添加cell.textLabel?.text = tableViewData[indexPath.section].sectionData[indexPath.row]而不是cell.textLabel?.text = tableViewData[indexPath.section].title

来解决它

不知道我是怎么想念的。



Similar searches
    父组件未在Vue中接收子事件 WPF,将3个RenderTargetBitmap合并为一个 无法在Paperboy中提供require.txt? 在webkit下的xhtml中将div设置为特定高度 需要一些帮助来减少使用OpenCV 3.4.1 DNN模块通过MobileNet SSD Object Detection检测到的对象类别