根据 UIPickerView 中的选定行将新行插入 UITableView 的一部分 - Swift

Insert new row into a section of UITableView based on selected row in UIPickerView - Swift

我有一个自定义的内联 UIPickerView(本质上只是一个 expanding/collapsing UIPickerView)并且在选择器视图中有 3 个选项(行):"Weekly"、"Monthly" 和 "Enter Manually".我想要完成的是,如果用户选择 "Enter Manually" 行,则会在包含自定义 UIPickerView 的行下方插入一个新行(其中将包含我创建的自定义 TextInputCell - 但除此之外) . UITableView 代表一个表单,用户可以在其中创建一个 "event" 可以这么说。有 4 个部分,每个部分有不同数量的行,一些行有 expanding/collapsing UIPickerViews,其他有自定义 UITextFields,还有一些有 expanding/collapsing UIDatePickers。我只希望 1 个特定的 UIPickerView 发生这种情况,但我似乎无法让它工作。我曾在 didSelectRowAtIndexPath 中尝试过类似的操作,因为当 UIPickerView 从选择它的行中折叠时:

   tableView.beginUpdates()
    if(cell.isKindOfClass(PickerCell)) {
        let pickerTableViewCell = cell as! PickerCell
        if(!pickerTableViewCell.isExpanded() && pickerTableViewCell.rightLabelText() == numClasses[2]) {
            alertOptions.insert("Number of Weeks", atIndex: 1)
            numberOfRowsAtSection[2] = 5
            tableView.insertRowsAtIndexPaths([
                NSIndexPath(forRow: alertOptions.count-3, inSection: 2)
                ], withRowAnimation: .Automatic)
        }
    }
    tableView.endUpdates()

numClasses是一个包含UIPickerView行数据的数组:

let numClasses = ["Weekly", "Monthly", "Enter Manually"]

每个部分的所有 UITableView 数据都在单独的数组中,如下所示:

var numberOfRowsAtSection: [Int] = [3, 4, 4, 3]

let mainOptions = ["Client Name", "Invoicing Email", "Invoicing Address"]
let dateOptions = ["All Day", "Starts", "Ends", "Time Zone"]
var alertOptions = ["How Many Classes", "Shown on Calendar As", "Alert by Email", "Alert by Text"]
let billingOptions = ["Amount Charged", "Tax Included", "Billing Date"]

然后我想我会为 tableBeginUpdates()tableEndUpdates() 之间的所有内容设置一个条件来测试它是否是正确的 UIPickerView,例如:

if(pickerViewTableCell.pickerView == numClassesPicker.pickerView) { ... }

我正在尝试做的事情是否可行,或者我是否在正确的轨道上?求助!

为了更好的视觉效果,这里有一些图片:

选择"Enter Manually"之前:

选择"Enter Manually"时,UIPickerView展开:

之后,它现在折叠并 "Enter Manually" 选中, 我要做的是现在在 "How Many Classes" 和 "Show on Calendar As" 之间有一个新行:

alertOptions中,只需添加一个新对象并在完成pickerView 中的操作后重新加载tableView。

主要问题之一是我没有使用 tableView.reloadData() 但除此之外,我最终编码的实现是:

        tableView.beginUpdates()

        if(cell.isKindOfClass(PickerCell)) {

            let pickerTableViewCell = cell as! PickerCell

            if(pickerTableViewCell.pickerView == numClassesPicker.pickerView && numberOfRowsAtSection[2] < 5) {

                let newRowIndex = alertOptions.count - 4
                let newNumRows = 5

                if(!pickerTableViewCell.isExpanded() && pickerTableViewCell.rightLabelText() == numClasses[2]) {

                    alertOptions.insert("Number of Weeks", atIndex: 1)
                    numberOfRowsAtSection[2] = newNumRows

                    tableView.insertRowsAtIndexPaths([NSIndexPath(forRow: newRowIndex, inSection: 2)],
                        withRowAnimation: .Automatic)
                    tableView.reloadData()
                }

            } else if(numberOfRowsAtSection[2] == 5) {

                let oldRowIndex = alertOptions.count - 5
                let numRows = 4

                alertOptions.removeAtIndex(1)
                numberOfRowsAtSection[2] = numRows

                tableView.deleteRowsAtIndexPaths([NSIndexPath(forRow: oldRowIndex, inSection: 2)],
                    withRowAnimation: .Automatic)
                tableView.reloadData()

            }
        }

        tableView.endUpdates()

然后在 didSelectRowAtIndexPath tableView 委托中,我有一个条件来测试 numberOfRowsAtSection[2] 中有多少行,然后 return 我想要相应的单元格类型