获取具有匹配值的indexPath.row

问题描述 投票:0回答:1

因此,我有两个来自同一API的Json请求,不同的是,一个来自今天,另一个来自昨天。我将它们显示在TableView的“ MyCell”中,结果是这样。

enter image description here这是代码:

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

        let cell = tableView.dequeueReusableCell(withIdentifier: "MyCell", for: indexPath) as! CustomTableViewCell

        let today = today[indexPath.row]
        let yesterday = yesterday[indexPath.row]

        let nameOfThe place = today.local
        let salesToday = today.sales 

        let nameOfThePlaceYesterday = yesterday.local
        let salesYesterday = yesterday.sales


}

到目前为止,很好。那就是我想要的。问题开始于昨天某些地方没有营业(例如A和B)。我明白了:

enter image description here

发生的事情是,由于这两天都没有相同的“ numberOfRowsInSection”,所以它只是按顺序填充单元,但位置不匹配。题:如何获得具有其他indexPath.row匹配值的“ indexPath.row”。在这种情况下,它将是该地点的名称。

谢谢

swift tableview
1个回答
1
投票
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: "MyCell", for: indexPath) as! CustomTableViewCell

        let today = today[indexPath.row]
        let nameOfThe place = today.local
        let salesToday = today.sales 

        if let yesterday = yesterday[safe: indexPath.row] {
           let nameOfThePlaceYesterday = yesterday.local
           let salesYesterday = yesterday.sales
        }
}
© www.soinside.com 2019 - 2024. All rights reserved.