首页 > 解决方案 > 如何修复快速索引超出范围(444索引超出范围)..?

问题描述

我有一个结构数据,我试图在指定的索引处填充表格视图,但是如果尝试将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 atdequeue 方法中:

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

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

标签: iosarraysjsonswift

解决方案


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

但是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)
]

表视图的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
}

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


推荐阅读