【发布时间】: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 的组合。我怎样才能做到这一点。请帮忙?
【问题讨论】:
-
好吧,将索引
0和1替换为依赖于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