2017-07-27 42 views
0

高效方式我有如下的數據模型:顯示數據的多個陣列成單個的tableview部

class Model { 
var field1 = Data() 
var field2 = Data() 
var field3 = [Data]() 
var field4 = [Data]() 
} 
var model = Model() // Provide data here 

該模型將被獲取並從API解析,我使用另一個類的數據作爲一個子模型形成模型。我需要顯示Model對象爲順序單表視圖部分:

字段1 - >場2 - > [字段3] - > [字段4]

什麼,我計劃做是如下:

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
return model.count 

}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
let cell = tableView.dequeueReusableCell(withIdentifier: "StatusCell") as! StatusCell 
switch indexPath.row { 
    case 0: 
    // Do something with field1 
    case 1: 
    // Do something with field2 
    case 2...(2 + field3.count): 
    // Do something with field3 
    case (2 + field3.count)...(2 + field3.count + field4.count) 
    // Do something with field4 
return cell 
} 

至於我的觀點,這是一個很大低效的,因爲每次調用cellForRowAt時,它都必須一次又一次地計算數組大小,但我必須檢查數組以便按照上面預定義的順序顯示它們。但是,這是我目前唯一的方式。

我想改善代碼以獲得更好的效率,可能是數據應該存儲在模型中(而不是數組或類似的東西)或如何將數據正確填充到表視圖中。請幫忙指出我應該在哪裏改進代碼以及如何改進代碼。

感謝

+0

什麼是「數據」類(數據類型) –

回答

0

最簡單的方法是你可以使用的tableView節,但你描述要填充在單節這一切的數據字段根本沒有實現titlefroHeader委託或傳遞空字符串在這個委託中,你可以看到tableview像單個部分一樣填充。

override func numberOfSections(in tableView: UITableView) -> Int { 
    return numberOfFields 
} 
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    if section == 0 
    { 
     return [field1 count] 
    } 
    else 
    { 
     return [otherfield count] 
    } 
} 
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView .dequeueReusableCell(withIdentifier: "cell")! 
    if indexPath.section == 0 
    { 
     cell.textLabel?.text = field1 .object(at: indexPath.row) 
    } 
    else 
    { 
     cell.textLabel?.text = otherfield .object(at: indexPath.row) 
    }// assuming fileds array containing string 
    return cell 
}//pass empty string in this delegate or dont implement it, or if you want to display the first header text the pass in other empty string 
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 
    if section == 0 
    return "section" 
    else 
     return "" 
} 

}

,如果你需要討論別的請隨時

+0

這是一個很好的把戲。如果你能向我展示更多改進方法,我真的很感激!我正在考慮將這些數據字段合併到另一個數組中,以便將它們填充到tableView中,但它必須創建額外的內存 –

0

這裏爲你的最佳選擇是使用表格視圖部分每個字段屬性,但如果你希望只使用1節,你可以簡單地合併數據:

class Model { 
    var field1 = Data() 
    var field2 = Data() 
    var field3 = [Data]() 
    var field4 = [Data]() 

    func getAllFields() -> [Data] { 
      return [field1, field2] + field3 + field4 
    } 
} 

let allModelFields = model.getAllFields()  

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int 
    return allModelFields.count 
} 

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "StatusCell") as! StatusCell 
    let fieldItem = allModelFields[indexPath.row] 
    // TODO: Configure your cell with current field item 
    return cell 
} 

更新時間: 並非是最好的解決辦法,而應針對特定種類的要求工作,但我寧願建議你改變你的模型,如果你的邏輯允許這樣做,或者簡單地使用table view部分,就使用1數組。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
let cell = tableView.dequeueReusableCell(withIdentifier: "StatusCell") as! StatusCell 
let row = indexPath.row 
switch row { 
    case 0, 1: 
    // Do something with field1 or field2 
    default: 
    let field3count = model.field3.count 
    if row - 1 <= field3count { 
     let field3item = model.field3[row - 2] 
     //Do something with field3item 
    } else { 
     let field4item = model.field4[row - 2 - field3count] 
     //Do something with field4item 
    } 
return cell 
} 
+0

感謝您的回答,但如果我們將數組組合在一起,我們將複製數據並浪費內存。 –

+0

@TuyenVoQuang我已經爲你更新了我的答案,但請注意,這不是一個乾淨的解決方案。隨意問你是否有什麼不清楚的東西 – livenplay

+0

謝謝你的努力! –

相關問題