ios - 如何通过 numberOfRowsInSection 函数联系发件人?

标签 ios swift

我想制作带标题的可扩展表格 View 单元格。我有我的数据模型。我添加 isExpanded 变量 bool 来确定单元格的情况,因此在 numberOfRowsInSection 函数中给了我索引错误。我的逻辑有 isExpanded 变量,但它无法正常工作。我该如何解决?这是我的代码:

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if section == 0 {
        if self.userAdressDict[0].isExpanded == true {
            return 0
        }
        if self.userAdressDict[0].isExpanded == false {
            return self.userAdressDict.count
        }
    }
    if section == 1 {
        return self.userAdressDictForTeslim.count

    }
    return 1
}

这是我的标题 View 代码:

    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    if section == 0 {
        let button = UIButton(type: .system)
        button.setTitle("Kapat", for: .normal)
        button.setTitleColor(.black, for: .normal)
        button.backgroundColor = .lightGray
        button.titleLabel!.font = UIFont.boldSystemFont(ofSize: 14)
        button.addTarget(self, action: #selector(handleExpandCloseForAlim(sender:)), for: .touchUpInside)
        return button
    }
    if section == 1 {
        let button = UIButton(type: .system)
        button.setTitle("Kapat", for: .normal)
        button.setTitleColor(.black, for: .normal)
        button.backgroundColor = .lightGray
        button.titleLabel!.font = UIFont.boldSystemFont(ofSize: 14)
        button.addTarget(self, action: #selector(handleExpandCloseForTeslim), for: .touchUpInside)
        return button
    }
    else{
        return nil
    }
}

这里的按钮操作功能:

@objc func handleExpandCloseForAlim(sender: UIButton) {
    var indexPaths = [IndexPath]()
    for row in self.userAdressDict.indices{
        print(0 , row)
        let indexPath = IndexPath(row: row, section: 0)
        indexPaths.append(indexPath)
    }
    let indexPath = IndexPath(row: (sender.tag), section: 0)
    let adresso = self.userAdressDict[indexPath.row]
    if adresso.isExpanded == true {
        tableView.deleteRows(at: indexPaths , with: .fade)
    }
    if adresso.isExpanded == false {
        tableView.insertRows(at: indexPaths, with: .fade)
    }
    //self.userAdressDict.removeAll()

}

我可以使用removeAll()代码进行删除过程。但我也想插入行,所以这种方式不是正确的方法。 (userAdressDict 是我保存数据的模型)。在handleExpandCloseForAlim函数的sender中获取isExpanded变量。我最后对 numberOfRowsInSection 部分有疑问。

最佳答案

使用guard来查看数组中是否有任何元素,如下所示:

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if section == 0 {
        guard let element = self.userAdressDict.first as? AdresStruct else { return 0 }

        if element.isExpanded {
            return 0
        }
        else {
            return self.userAdressDict.count
        }
    }

    if section == 1 {
        return self.userAdressDictForTeslim.count
    }

    return 1
}

关于ios - 如何通过 numberOfRowsInSection 函数联系发件人?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58415045/

相关文章:

ios - 原生 UIKeyboard 无缘无故被拉伸(stretch)

ios - 从 WAV 文件中读取原始音频样本

swift - 给定相同的回调实现,抽象函数调用

ios - 具有协议(protocol)扩展的 Swift 通用未知成员

ios - 如何在 XIB 文件中设计自定义 UIButton?

ios - 从 ScrollView 委托(delegate)分配 alpha

c++ - 我如何在 JUCE 中录制标题没有 'JUNK' 子 block 的音频?

ios - 在 Swift 2.2 中将 Int64 转换为 Float

ios - Swift-如何在单击按钮时在自定义表格 View 单元格中获取标签值

swift - LLDB 快速打印对象