Ios 如何组合数组中的元素并在swift的tableView中显示

Ios 如何组合数组中的元素并在swift的tableView中显示,ios,arrays,swift,uitableview,Ios,Arrays,Swift,Uitableview,我有这样一个数组:- var priceArray = [0, 200, 400, 600, 800, 1000] 我从我的应用程序中的Api获取了这些数据。我正在使用tableView显示数据,以便提供如下价格选择:- func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return priceArray.count } func tableView(_

我有这样一个数组:-

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
的组合。我怎样才能做到这一点。请提供帮助?

您需要使用
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
}

您需要使用
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
}
这应该可以完成任务

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
    }

这应该可以完成任务。

您的价格数组有6个元素。但是你需要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
    }
所以你们需要从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
}

您的价格数组有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
}

您只需创建函数,即可为tableView创建数据源数组:

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
}


之后,只需创建类似于
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-1
    ,或
    array.count+1
    ,因此潜在崩溃的位置更少)
  • 当然,您可以重用此函数:)

快乐编码

您只需创建函数,即可为tableView创建数据源数组:

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
}


之后,只需创建类似于
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-1
    ,或
    array.count+1
    ,因此潜在崩溃的位置更少)
  • 当然,您可以重用此函数:)

快乐编码

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

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])
        }
    }
}
}

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

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])
        }
    }
}
}


好的,用依赖于
indexath.row
的东西替换索引
0
1
。你应该有
var priceArray=[[0,200],[200,400]等等。
。注意,你可以从你当前的
priceArray
定义中构造它。这样做会更简单,
let values=priceArray[indexath.row];cell.pricellabel.text=“\(值[0])-\(值[1])”
但是Sir@Larme这不是一个很长的过程吗?好吧,用依赖于
indexPath.row
的东西替换索引
0
1
。你应该有
var priceArray=[[0,200],[200,400]请注意,您可以从当前的
priceArray
定义构造它。这样会更容易,
让values=priceArray[indexath.row];cell.priceLabel.text=“\(values[0])-\(values[1])”
但是Sir@Larme这不是一个很长的过程吗?因为到达最后一个元素时,代码将崩溃为
(last+1)
将不存在,因为到达最后一个元素时,代码将崩溃为
(last+1)
不会出现为什么
priceArray.count-2
?我们应该使用return priceArray.count-1,因为我们没有只使用一个值1000作为第一个值应该是
priceArray.count-1
完美现在我们有3个类似的答案:PHard选择它:DWhy
priceArray.count-2
?我们应该使用return priceArray.ccount-1因为我们没有使用只有一个值1000作为第一个值应该是
priceArray.count-1
Perfect现在我们有3个类似的答案:PHard to choose then:d此代码将不起作用,因为当
案例失败时单元格没有返回。如果
案例失败,这是编译时错误。对不起,请检查我编辑的answer、 @AakashAlso对于更新的答案,将不会设置最后一个单元格的
pricelab
标签文本,这将是不可取的行为。此代码将不起作用,因为当
案例失败时单元格不会返回。如果
案例失败,这是一个编译时错误。对于我的错误,请检查