SWIFT:通过单击 UIButton 将节/行插入到 UICollectionView
SWIFT: Insert Section / Rows into UICollectionView by clicking a UIButton
我正在尝试设置一个 UIButton,单击它会在我的 UICollectionView 中添加一个新部分。我在网上搜索了一些示例,但可以使它们起作用。以下是看起来不错的答案之一,但仍然不适合我。
我错过了什么吗?谁能提供一些帮助?非常感谢!
func buttonAction(sender:UIButton!) {
var paths = [NSIndexPath]()
paths.append(NSIndexPath(forRow: 0, inSection: 0))
self.table.insertItemsAtIndexPaths(paths)
}
您必须更新 table 视图的 datasource
并重新加载 table。
例如,如果您的数据源是一个包含节标题的数组和一些 objects 可以提供单元格数据的数组,您可以这样声明它:
var dataArray : [String: [CellObject]]?
然后您提供通常的数据源,例如在 numberOfSectionsInTableView
中 dataArray?.count
;在 numberOfRowsInSection
中 dataArray[section]!.count
等
该按钮将简单地修改 dataArray
,然后在 table 视图上调用 reloadData
(或者,插入部分并在 beginUpdates
中调整数据) -endUpdates
括号)。
我正在尝试设置一个 UIButton,单击它会在我的 UICollectionView 中添加一个新部分。我在网上搜索了一些示例,但可以使它们起作用。以下是看起来不错的答案之一,但仍然不适合我。
我错过了什么吗?谁能提供一些帮助?非常感谢!
func buttonAction(sender:UIButton!) {
var paths = [NSIndexPath]()
paths.append(NSIndexPath(forRow: 0, inSection: 0))
self.table.insertItemsAtIndexPaths(paths)
}
您必须更新 table 视图的 datasource
并重新加载 table。
例如,如果您的数据源是一个包含节标题的数组和一些 objects 可以提供单元格数据的数组,您可以这样声明它:
var dataArray : [String: [CellObject]]?
然后您提供通常的数据源,例如在 numberOfSectionsInTableView
中 dataArray?.count
;在 numberOfRowsInSection
中 dataArray[section]!.count
等
该按钮将简单地修改 dataArray
,然后在 table 视图上调用 reloadData
(或者,插入部分并在 beginUpdates
中调整数据) -endUpdates
括号)。