将数据提供给具有多个标签的自定义UITableViewCell的最佳方法是什么 - Swift



我有一个带有自定义视图的tableView,它有三个标签,displayCarNamedisplayMpgdisplayCarPrice。我使用三个不同的数组来输入 tableView 中的每个标签,一切正常,但我觉得有更好的方法可以做到这一点,可能是使用单个字典或类似的东西。

这是我正在使用的代码。

    private var carNameList = [String]()
    private var mpgList = [Int]()
    private var carPriceList = [Double]()

    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return carNameList.count
    }
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("reusableCell", forIndexPath: indexPath) as! CustomCell
        cell.displayCarName!.text= carNameList[indexPath.row]
        cell.displayMpg!.text = String(mpgList[indexPath.row])
        cell.displayCarPrice!.text = String(carPriceList[indexPath.row])
        return cell
    }

这是在表格行中馈送多个标签的常用方法吗?如果没有,有人可以帮助我改进上面显示的代码吗?

仅供参考 - 我实际上每行有三个以上的标签,但为简单起见,我只显示三个。

谢谢

您使用的内容有时称为并行数组。这种方法在缺乏结构支持的语言中很常见。

由于 Swift 确实支持结构,更好的方法是定义一个表示Car的类,具有汽车NameMpgPrice三个属性,然后使用单个[Car]列表代替三个单独的列表:

class Car {
    let Name : String
    let Mpg : Int
    let Price : Double
    init(name: String, mpg : Int, price : Double ) {
        Name = name
        Mpg = mpg
        Price = price
    }
}
...
private var carList = [Car]()
...
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("reusableCell", forIndexPath: indexPath) as! CustomCell
    Car car = carList[indexPath.row]
    cell.displayCarName!.text= car.Name
    cell.displayMpg!.text = String(car.Mpg)
    cell.displayCarPrice!.text = String(car.Price)
    return cell
}

最好的方法是为数据源创建一个类

class SingleCellData {
  var displayCarName : String!
  var displayMpg : Int! 
  var displayCarPrice : Double! 
}

在表视图中

var cellData : [SingleCellData] = []
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("reusableCell", forIndexPath: indexPath) as! CustomCell
    let sData = cellData[indexPath.row]
    cell.displayCarName!.text= sData.displayCarName
    cell.displayMpg!.text = String(sData.displayMpg)
    cell.displayCarPrice!.text = String(StringsData.displayCarPrice)
    return cell
}

您还可以将文本赋值逻辑移动到CustomCell类本身:

// Car.swift
class Car {
    var name : String
    var mpg : Int
    var price : Double
}
// ViewController.swift
...
private var cars = [Car]()
...
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("reusableCell", forIndexPath: indexPath) as! CustomCell
    cell.car = cars[indexPath.row]
    return cell
}
// CustomCell.swift
class CustomCell: UITableViewCell {
   ...
   var car: Car? {
      didSet {
         displayCarName.text = car?.name
         displayMpg.text = car?.mpg
         displayPrice.text = car?.price
      }
   }
}

最新更新