2017-07-27 16 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

谢谢你的努力! –

相关问题