用于填充 SWIFT 中 Tableview 部分的结构和数组
Struct and array to populate sections of Tableview in SWIFT
我有一个定义 post 的结构 (getItems)
当我阅读 posts 时,我将它们存储在一个数组中,如下所示:
var posts = [getItems]()
问题是:
我决定在我的表格视图中创建部分
如何创建 posts 数组?
我试过声明 var items = [posts]()
但这行不通
我还有一个项目计数器,用于计算每个部分中应该有多少项目,但我不知道如何将我的 getItems 数组拆分成类似这样的内容:
[index1 : [getItems1, getItems2 ...], index2:[getItems1, getItems2 ...]...]
这样我就可以在我的 TableView 中调用部分和行
此致
PS : 我没有 post 任何代码,因为这里有很多数据...
抱歉浪费你的时间,很简单,我在尝试声明数组数组的方式上弄错了
只需要做
data = [[getitems]]()
我不确定这是否是您想要的。假设你有一个 counter
函数,它将一个部分索引作为参数,并且 return 这个特定部分应该有多少帖子。
func counter(index: Int) -> Int {
return index
}
然后像这样计算子数组:
var currentIndex = 0
var totalCount = 0
var sections: [Int: [getitems]] = [:]
while totalCount < posts.count {
let currentCounter = counter(currentIndex)
sections[currentIndex] = posts.suffixFrom(totalCount).prefix(currentCounter).flatMap { [=11=] }
currentIndex++
totalCount += currentCounter
}
然后在你的func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return sections[section].count
}
我有一个定义 post 的结构 (getItems) 当我阅读 posts 时,我将它们存储在一个数组中,如下所示:
var posts = [getItems]()
问题是:
我决定在我的表格视图中创建部分
如何创建 posts 数组?
我试过声明 var items = [posts]()
但这行不通
我还有一个项目计数器,用于计算每个部分中应该有多少项目,但我不知道如何将我的 getItems 数组拆分成类似这样的内容:
[index1 : [getItems1, getItems2 ...], index2:[getItems1, getItems2 ...]...]
这样我就可以在我的 TableView 中调用部分和行
此致 PS : 我没有 post 任何代码,因为这里有很多数据...
抱歉浪费你的时间,很简单,我在尝试声明数组数组的方式上弄错了
只需要做
data = [[getitems]]()
我不确定这是否是您想要的。假设你有一个 counter
函数,它将一个部分索引作为参数,并且 return 这个特定部分应该有多少帖子。
func counter(index: Int) -> Int {
return index
}
然后像这样计算子数组:
var currentIndex = 0
var totalCount = 0
var sections: [Int: [getitems]] = [:]
while totalCount < posts.count {
let currentCounter = counter(currentIndex)
sections[currentIndex] = posts.suffixFrom(totalCount).prefix(currentCounter).flatMap { [=11=] }
currentIndex++
totalCount += currentCounter
}
然后在你的func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return sections[section].count
}