在 Xcode 中(使用 swift)我有一个包含 3 个部分的 tableView。我希望在用户点击按钮时插入一个新部分,该部分的标题从 textField 中输入(textField 不在 tableView 中)
我熟悉tableView.insertrow
(只需传入正确的indexPath),但我不知道如何使用tableView.insertSection
(这需要一个indexSet ).
我正在使用一个二维数组来填充 TableView 。
var twoDArray = [
["apple", "banana", "pear"],
["carrot", "broccoli"],
["chicken", "beef"]
]
目前我的标题标题存储在一个单独的数组中:
var sections = ["Fruits", "Veggies", "Meats"]
当用户使用 tableView.insertSection
点击按钮时,我如何插入一个包含来自 textField.text
的输入的新部分(理想情况下,我想在没有重新加载整个 tableView)
最佳答案
如果您需要添加第 4 节并为节插入设置动画,您可以这样做:
var twoDArray = [
["apple", "banana", "pear"],
["carrot", "broccoli"],
["chicken", "beef"]
]
var sections = ["Fruits", "Veggies", "Meats"]
//After user presses the button
sections.append("Candy")
twoDArray.append(["Chocolate", "Pure Sugar"])
self.tableView.insertSections(IndexSet(integer: sections.count - 1), with: .automatic)
您只需确保在调用 insertSections
之前设置了数据源数组,否则您会崩溃并出现错误,让您知道您的部分插入需要与源插入相匹配。
关于swift - 使用 swift 在 Xcode 中插入带有 UIButton(或 Bar 按钮)的新部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48633366/