Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/111.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/react-native/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何修复swift索引超出范围(444索引超出范围)。。?_Ios_Arrays_Json_Swift - Fatal编程技术网

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

Ios 如何修复swift索引超出范围(444索引超出范围)。。?,ios,arrays,json,swift,Ios,Arrays,Json,Swift,我有一个struct data,我试图在指定的索引处填充表视图,但是如果尝试对数据和单元格标签使用indexath.row属性,每次都会出现索引超出范围的错误 我的结构: struct TradingPairs: Codable { var id: Int var name: String var baseAsset: String var quoteAsset: String } struct PairByTicker: Codable { var p

我有一个struct data,我试图在指定的索引处填充表视图,但是如果尝试对数据和单元格标签使用indexath.row属性,每次都会出现索引超出范围的错误

我的结构:

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]()
在“我的TableView”单元格中,用于“出列时的行”方法:

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

不确定为什么索引超出范围错误

返回的节单元格为self.tradingPair.count+[self.pairbycker].count,因此,如果两个数组都有5个成员,则tableview特性10行数据位于cellForRowAtIndexPath中

但是在cellForRowAtIndexPath中,它可以很好地用于索引4,并且当应用程序试图从该数组中获取索引5的对象时,它会很快崩溃

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

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

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)
]
假设您有以下模型

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
}

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

发布tableview委托和数据源方法的代码。您的cellForRowAt方法访问self.tradingPair[indexPath.row],但numberOfRowsInSection方法返回self.tradingPair.count+[self.PairBycker].count–您能发现问题吗?@Martin,您的反馈有点让人困惑,您能详细说明一下吗?将此添加到cellForRowAt方法中,您应该会看到问题:printAccess元素带索引,indexPath.row,数组中的元素数为,self.tradingPair.count您的表应该有一个数据源,而不是两个tradingPair&pairByTicket,尝试将它们合并到一个数组中。