swift - 使用 swift 在 Xcode 中插入带有 UIButton(或 Bar 按钮)的新部分

标签 swift xcode insert tableview sections

在 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/

相关文章:

python - 在列表中的特定索引处插入元素并返回更新后的列表

swift - 在 Swift 中比较小数(不是 float )

xcode - 如何在 Xcode 4 中更改自动完成大括号

ios - XCTest 中的 Alamofire cocoapod 集成问题

Xcode 委托(delegate)链接到对象

php - 如何在 PHP 中设置我自己的时区

linux - ubuntu 15.04 终端中的 swift 命令出错

ios - Xcode 7 : Shadow does NOT show around UIView in Interface Builder

swift - 有没有办法打乱一个数组,使得没有两个连续的值是相同的?

php - 如何在MySql中存储多个选择选项以便于搜索