Swift 获取具有匹配值的indexPath.row
所以,我有两个来自同一个API的Json请求,一个来自今天,另一个来自昨天。我把它们显示在我的TableView中的“MyCell”中,结果就是这样 代码如下:Swift 获取具有匹配值的indexPath.row,swift,tableview,Swift,Tableview,所以,我有两个来自同一个API的Json请求,一个来自今天,另一个来自昨天。我把它们显示在我的TableView中的“MyCell”中,结果就是这样 代码如下: func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "MyCell", for: indexPath) as! CustomTableViewCell
let today = today[indexPath.row]
let yesterday = yesterday[indexPath.row]
let nameOfThe place = today.local
let salesToday = today.sales
let nameOfThePlaceYesterday = yesterday.local
let salesYesterday = yesterday.sales
}
到目前为止,一切顺利。这就是我想要的。当一些地方(例如A和B)昨天还没有开门时,问题就开始了。我明白了:
发生的是,由于这两天并没有相同的“numberOfRowsInSection”,它只是按顺序填充细胞,但位置不匹配。问题:
如何使用另一个indexPath.row的匹配值获取“indexPath.row”。在这种情况下,它将是一个地方的名称
谢谢你看到我对你之前的问题的最后一点评论,这不起作用。为什么不呢?为什么不让昨天=昨天。首先(其中:{$0.name=today.name})工作?我不建议您重复使用这样的变量名,当您有一个属性
today
是数组,而变量today
是数组中的对象时,这只会使代码更难阅读。您能显示JSON吗?@JoakimDanielson。很抱歉反应太晚。你是对的,它最后确实起作用了。非常感谢你
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "MyCell", for: indexPath) as! CustomTableViewCell
let today = today[indexPath.row]
let nameOfThe place = today.local
let salesToday = today.sales
if let yesterday = yesterday[safe: indexPath.row] {
let nameOfThePlaceYesterday = yesterday.local
let salesYesterday = yesterday.sales
}
}