【问题标题】:How to fix swift Index Out of Range ( 444 Index out of range) ..? [closed]如何修复快速索引超出范围(444索引超出范围)..? [关闭]
【发布时间】:2021-01-27 09:17:54
【问题描述】:

我有一个结构数据,我正在尝试在指定索引处填充表格视图,但如果尝试将 indexPath.row 属性用于数据和单元格标签,我每次都会遇到 index out of range 错误..

我的结构:

struct TradingPairs: Codable {
    var id: Int
    var name: String
    var baseAsset: String
    var quoteAsset: String
}

struct PairByTicker: Codable {
    var price: Float
    var ask: Float
    var askVolume: Float
    var bid: Float
    var bidVolume: Float
    var volume: Float
    var time: String
    
    enum CodingKeys: String, CodingKey {
        case price = "price"
        case ask = "ask"
        case askVolume = "askVolume"
        case bid = "bid"
        case bidVolume = "bidVolume"
        case volume = "volume"
        case time = "time"
    }
}

和 ViewController 中的结构实例:

 var tradingPair = [TradingPairs]()
 var pairByTicker = [PairByTicker]()

在 My TableView Cell cell for row at dequeue 方法中:

 cell.name.text = self.tradingPair[indexPath.row].name
 cell.price.text = String(describing: self.pairByTicker[indexPath.row].price)
 cell.volume.text = String(describing: self.pairByTicker[indexPath.row].volume)
 return cell

--TableView 数据源:


extension ViewController: UITableViewDataSource {
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.tradingPair.count + [self.pairByTicker].count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "TradingPairCell") as! TradingPairCell
//        for data in self.pairByTicker {
//            let pairs = data
//            cell.configure(data: pairs)
//        }
//        for data in self.tradingPair {
//            cell.configure(data: data)
//        }
        
        cell.name.text = self.tradingPair[indexPath.row].name
        cell.price.text = String(describing: self.pairByTicker[indexPath.row].price)
        cell.volume.text = String(describing: self.pairByTicker[indexPath.row].volume)
        return cell
    }
}

extension ViewController: UITableViewDelegate {
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 44
    }
}

不知道为什么我的索引超出范围错误..(

【问题讨论】:

  • 贴出tableview委托和数据源方法的代码
  • 您的cellForRowAt 方法访问self.tradingPair[indexPath.row]numberOfRowsInSection 方法返回self.tradingPair.count + [self.pairByTicker].count - 你能发现问题吗?
  • @MartinR,对你的反馈有点困惑,你能详细说明一下吗?
  • 将此添加到您的 cellForRowAt 方法中,您应该会看到问题:print("Access element with index", indexPath.row, "number of elements in array is", self.tradingPair.count)
  • 您的表应该有一个数据源,而不是两个(tradingPair 和 pairByTicket),尝试将它们组合成一个数组。

标签: ios arrays json swift


【解决方案1】:

您要返回一个部分的单元格是self.tradingPair.count + [self.pairByTicker].count,因此如果两个数组都有 5 个成员,那么 tableview 会显示 cellForRowAtIndexPath 中的 10 行数据。

但在cellForRowAtIndexPath 中,它适用于最大为 4 的索引,并且一旦尝试从该数组中为索引 5 获取对象,应用程序就会崩溃。

这就是崩溃的原因,它将在self.tradingPair[indexPath.row].name 行崩溃,因为数组 tradingPair 只有 5 个成员(从索引 0 到 4)并且它试图获取第 5 个索引的成员。

您可以做的是您需要确定 tableview 的确切行数,然后返回方法 numberOfRowsInSection 的值。如下...

假设您有以下模型。

struct TradingPair {
    var name: String
    var price: Double
    var volume: Double
}

像下面这样初始化数组...

let tradingPair = [
    TradingPair(name: "name1", price: 12, volume: 15)
    TradingPair(name: "name2", price: 26, volume: 25),
    TradingPair(name: "name3", price: 37, volume: 12),
    TradingPair(name: "name4", price: 22, volume: 6)
]

table view的delegate和datasource方法的实现...

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

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

    let cell = tableView.dequeueReusableCell(withIdentifier: "TradingPairCell") as! TradingPairCell

    let obj = tradingPair[indexPath.row]
    cell.name.text = obj.name
    cell.price.text = String(obj.price)
    cell.volume.text = String(obj.volume)

    return cell
}

希望您从上面的示例中了解如何实现它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多