【问题标题】:How to make the combination of elements from an array and display on tableView in swift如何快速组合数组中的元素并在tableView上显示
【发布时间】:2018-11-13 09:44:51
【问题描述】:

我有一个这样的数组:-

var priceArray = [0, 200, 400, 600, 800, 1000]

我从我的应用中的 Api 获得了这些数据。我正在使用tableView 来显示数据,所以我可以提供这样的价格选择:-

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return priceArray.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "stickCell", for: indexPath) as! StcikCell
    cell.priceLabel.text = "\(priceArray[0]) - \(priceArray[1])"
    return cell
}

但问题是我是否会使用这个:

cell.priceLabel.text = "\(priceArray[0]) - \(priceArray[1])" 

我只会在tableView 中获得第一个和第二个值,但我真正想要的是在第一行我想获得0 - 200 然后在第二行200 - 400 在第三个400 - 600 等等,直到我会获取800 - 1000 的组合。我怎样才能做到这一点。请帮忙?

【问题讨论】:

  • 好吧,将索引01 替换为依赖于indexPath.row 的东西...
  • 你应该有var priceArray = [[0, 200], [200, 400], etc.。请注意,您可以从当前的 priceArray 定义中构造它。那就更简单了,let values = priceArray[indexPath.row]; cell.priceLabel.text = "\(values[0]) - \(values[1])"
  • 但是@Larme 先生,这不是一个漫长的过程吗?

标签: ios arrays swift uitableview


【解决方案1】:

您需要使用indexPath.row 来索引cellForRowAt 中的priceArray,还需要修改numberOfRowsInSection,因为价格范围少于priceArray 中的元素数量。

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

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "stickCell", for: indexPath) as! StcikCell
    cell.priceLabel.text = "\(priceArray[indexPath.row]) - \(priceArray[indexPath.row+1])"
    return cell
}

【讨论】:

  • 为什么是priceArray.count - 2
  • 我们应该使用 return priceArray.count - 1 因为我们不只使用一个值 1000 作为第一个值
  • 应该是priceArray.count - 1
  • 完美,现在我们有 3 个类似的答案:P
  • 很难选择它然后:D
【解决方案2】:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return (priceArray.count - 1)
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "stickCell", for: indexPath) as! StcikCell
    let index = indexPath.row
    cell.priceLabel.text = "\(priceArray[index]) - \(priceArray[index + 1])"
    return cell
}

这应该可以完成工作。

【讨论】:

  • 因为到达最后一个元素时,代码会因为(last + 1) 不存在而崩溃
【解决方案3】:

您的价格数组有 6 个元素。但您需要 5 个价格范围。 所以你需要从 numberOfRowsInSection 中减去 1,这将给你 5 个元素。

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

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "stickCell", for: indexPath) as! StcikCell
    cell.priceLabel.text = "\(priceArray[indexPath.row]) - \(priceArray[indexPath.row+1])"
    return cell
}

【讨论】:

    【解决方案4】:
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return priceArray.count - 1
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
            let cell = tableView.dequeueReusableCell(withIdentifier: "stickCell", for: indexPath) as! StcikCell
    
    
        cell.priceLabel.text = "\(priceArray[indexPath.row]) - \(priceArray[indexPath.row + 1])"
    
    
            return cell
        }
    

    【讨论】:

    • 此代码不起作用,因为if 案例失败时未返回单元格,这是编译时错误。
    • 抱歉我的错误,请检查我编辑的答案。 @Aakash
    • 对于更新后的答案,最后一个单元格的 priceLabel 标签文本也不会设置,这不是理想的行为。
    【解决方案5】:

    您可以简单地创建函数来为您的 tableView 创建 dataSource 数组:

    func makeTableDataSource(from priceArray: [Int]) -> [String] {
        var resultArray = [String]()
    
        for (index, price) in priceArray.enumerated() where index < priceArray.count - 1 {
            resultArray.append("\(price) - \(priceArray [index + 1])")
        }
    
        return resultArray
    }
    

    结果:

        var priceArray = [0, 200, 400, 600, 800, 1000]
    
        Result:
        - 0 : "0 - 200"
        - 1 : "200 - 400"
        - 2 : "400 - 600"
        - 3 : "600 - 800"
        - 4 : "800 - 1000"
    

        var priceArray = [0, 200, 400]
    
        Result:
        - 0 : "0 - 200"
        - 1 : "200 - 400"
    

        var priceArray = [0]
    
        Result:
        []
    

    之后,只需创建像 private var tableViewDataSource = [String]() 这样的属性 然后在你的viewDidLoad() 添加:

    override func viewDidLoad() {
        super.viewDidLoad() 
        var priceArray = [0, 200, 400, 600, 800, 1000] 
        tableViewDataSource = makeTableDataSource(from: priceArray)
    }
    

    在你的情况下很容易使用:

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return tableViewDataSource.count 
    } 
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "stickCell", for: indexPath) as! StcikCell
        cell.priceLabel.text = tableViewDataSource[indexPath.row]
        return cell
    }
    

    这种方法的优点:

    • 您只在打开 viewController 时创建一次数据源
    • 您的单元格的数据源未“即时”生成
    • 单独负责生成 tableDataSource 数据(如果您需要更新字符串,只需在 makeTableDataSource 中完成,其他一切正常)
    • 容易定位的潜在错误
    • 所有表的单一“真实数据”(您不需要在不同情况下使用array.count - 1array.count + 1,因此可能发生崩溃的地方更少)
    • 你当然可以重用这个功能:)

    编码愉快!

    【讨论】:

    • 感谢您的回答
    【解决方案6】:

    您只需要正确准备数据源即可。
    为此,我建议将现有价格数组拆分为块并将其映射到您要表示的对象。在您的情况下为字符串类型。
    您可以使用以下扩展名:

    extension Sequence {
    func split(by chunkSize: Int) -> [[Self.Element]] {
        return self.reduce(into:[]) { memo, cur in
            if memo.count == 0 {
                return memo.append([cur])
            }
            if memo.last!.count < clump {
                memo.append(memo.removeLast() + [cur])
            } else {
                memo.append([cur])
            }
        }
    }
    }
    

    用法:

    var priceArray = [0, 200, 400, 600, 800, 1000]
    let dataSource = priceArray.split(by: 2).map { "\($0[0]) - \($0[1])" }
    // result: ["0 - 200", "400 - 600", "800 - 1000"]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多