ios - 如何修复快速索引超出范围(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 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
}
}
不知道为什么我的索引超出范围错误..(
解决方案
您正在为一个部分返回单元格,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
}
希望您从上面的示例中了解如何实现它。
推荐阅读
- c# - 对 Web.config、ConnectionString.config 和 Unicorn 应用转换
- python - 为什么 pandas 和 R 之间的数据帧的内存使用量有如此大的差异?
- reactjs - 如果在回调函数中调用 useState 不会重新渲染组件
- autodesk-forge - Forge Viewer,光栅图像
- python - 如何将嵌套字典转换为 Python 元组?
- javascript - javascript for循环中的异步/等待变量影响
- tensorflow - 张量转换为具有 dtype bool 的张量请求 dtype float32 | 在 LSTM 中添加掩码
- python - 为 LQR 方法定义 Python 类(线性二次调节器)
- ios - 是否可以在不使用 APNs 的情况下在关闭的 Intranet 应用程序中让 iOS 推送通知?
- controls - ExoPlayer 的底部控件在视频中不可见但可点击