如何修复swift索引超出范围(444索引超出范围)..?

时间:2020-10-13 04:10:57

标签: ios arrays json swift

我有一个结构数据,我试图在指定的索引处填充表视图,但是如果尝试对数据和单元格标签使用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中的struct实例:

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

并在“我的TableView单元格cell for row at”中出队方法:

 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 DataSourse:


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

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

1 个答案:

答案 0 :(得分:0)

您要返回的单元格区域为self.tradingPair.count + [self.pairByTicker].count,因此,如果两个数组都具有5个成员,则表视图方面的cellForRowAtIndexPath中将包含10行数据。

但是在cellForRowAtIndexPath中,它对于高达4的索引将运行良好,并且一旦尝试从该数组中为索引5获取对象,应用程序将崩溃。

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

您能做的是,需要确定表视图的确切行数,然后返回方法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)
]

表视图的委托和数据源方法的实现...

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
}

从以上示例希望,您将了解如何实现它。