【问题标题】:how to remove the cell from uitableview cell如何从uitableview单元格中删除单元格
【发布时间】:2019-08-12 09:18:51
【问题描述】:

我尝试在用户选择“类型 3”时动态排列表格视图。它在用户选择“type 3”时起作用,“type 3-1”将被添加到 tableview 中。但是,当用户选择 type3-1 以外的选项时,程序崩溃了。我不知道如何在调用覆盖函数之前执行“rows.remove(at:2)”。任何建议将不胜感激!

class GuestViewController: UITableViewController {
var rows:[[[String:Any]]] = [[["type":RowType.DetailContent,
                               "subType":DCType.DCRightContent,
                               "name":CPFFields.CID,
                               "content":"9637"],                                  
                             ["type":RowType.DetailContent,
                              "subType":DCType.DCInput,
                              "name":CPFFields.VISIA]],

                             [["type":RowType.DetailTextView,
                               "CPFType":CPFFields.UV,
                               "title":CPFFields.preferenceTitle]],

                             [["type":RowType.DetailContent,
                               "subType":DCType.DCSelection,
                               "name":CPFFields.Phototherapy,
                               "title":CPFFields.anestheticTitle],
                              ["type":RowType.DetailTextView,
                               "CPFType":CPFFields.Phototherapy,
                               "title":CPFFields.preferenceTitle]],                                 
                             ]
var isNewGuestSelected : Bool = false

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {  

    return rows[section].count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let item = rows[indexPath.section][indexPath.row]     

    let type = item["type"] as! RowType

    if type == RowType.DetailContent
    {
        let cell = tableView.dequeueReusableCell(withIdentifier: "DetailNameCell", for: indexPath) as! DetailContentCell

        let cpfType = item["name"] as? CPFFields ?? .Customer
        cell.name.text = CPFFields.localizedString(from: cpfType)
        if let field = item["title"] as? CPFFields
        {
            cell.name.text = CPFFields.localizedString(from: field)
        }
        cell.moreSlectionLeftSpace = true

        var content:String? = ""
        cell.type = cpfType
        switch cpfType {
        case .CID:
            content = (profile?.birthDate.dateFromDateString?.stringForPaitentId ?? "") + (profile?.name ?? "")
        case .CT:
            content = ""
            if let profile = profile
            {
                content = CPFCustomerType.localizedString(from: profile.type)
                //New Guest
                if(content == CPFCustomerType.type1.rawValue){

                    rows[0].insert(["type":RowType.DetailContent,
                                    "subType":DCType.DCRightContent,
                                    "name":CPFFields.CID,
                                    "content":"9637"], at: 1)
                    isNewGuestSelected = true

                } else{
                    if isNewGuestSelected == true{
                        rows[0].remove(at: 1)
                        isNewGuestSelected = false
                    }
                }
            }


let subType = item["subType"] as! DCType
            cell.setcontentType(type: subType, content: content)
            return cell
    }

I expected not to see "rows[0][2]" after running "rows[0].remove(at:1)".
However the log is printing
    rows[0][0]
    rows[0][1]
    rows[0][2]        
    then 
    it crashed at "let item = rows[indexPath.section][indexPath.row]"
    because it is out of range

【问题讨论】:

    标签: swift uitableview


    【解决方案1】:

    您在渲染时正在修改您的内容,因此在调用 numberOfRows:inSection: 之后。因此,tableView 正在尝试访问一个不再存在的元素,因为您已将其删除。

    你的周期:

    → number of rows 4
    → removed item, contents now has 3 items
    → cell for item 0
    → cell for item 1
    → cell for item 2
    - cell for item 3 → crash
    

    考虑替换 cellForRow 方法之外的逻辑,并在重新加载 tableView 之前执行这些操作。

    您应该严格使用tableView:cellForRow:atIndexPath 将单元格出列并配置它们;不是为了修改底层数据存储,因为可能会发生像你现在遇到的奇怪的事情。

    如果您提供更多上下文,我可能会告诉您将代码放置在何处以解决此问题。

    【讨论】:

    • 我已经更新了示例代码,不确定是否足够。感谢您的帮助!
    【解决方案2】:

    其实解决方法很简单。我只是在删除数组后添加了tableView.reloadData(),然后可以更新UI。

                    if isNewGuestSelected == true{
                        rows[0].remove(at: 1)
                        isNewGuestSelected = false
                        tableView.reloadData()
                    }
    

    【讨论】: