2016-03-01 27 views
0

我有一個結構(getItems),它定義後 當我看到我的帖子,我將它們存儲在一個數組,像這樣:struct和數組來填充泰伯維的部分在SWIFT

var posts = [getItems]() 

問題是: 我決定在我的tableview中創建部分 如何創建一個帖子數組? 我已經試過申報var items = [posts]()但是這並未將不起作用

我也有一個統計許多項目應該如何在每個部分項目計數器,但我無法弄清楚如何我getItems的陣列分成類似:

[索引1:getItems1,getItems2 ...],索引2:getItems1,getItems2 ...] ...]

這個,所以我可以調用部分,在我的TableView

行真誠 PS:我沒有發佈任何代碼,因爲這裏有很多數據...

+0

你的問題太模糊。根據櫃檯,你只是想把'posts'分成幾個子排列嗎? –

+0

是的確切的說,我需要將它們分段 –

回答

0

我不能,如果完全確定這是你想要的。假設您有一個counter函數,它將採用段索引作爲參數,並返回此特定段中應該有多少帖子。

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 { $0 } 
    currentIndex++ 
    totalCount += currentCounter 
} 

,然後在func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return sections[section].count 
} 
+0

美麗的句法,在這裏學到了很多 –

0

對不起,我在浪費你的時間,很簡單,我是在我試圖聲明數組

數組的方式錯了必須這麼做

data = [[getitems]]()