gpt4 book ai didi

swift - 如何在两个彼此相等的变量上过滤 UITableViewCell

转载 作者:可可西里 更新时间:2023-11-01 01:09:05 24 4
gpt4 key购买 nike

我想在 Swift 4 中填充一个 UITableView,我只想显示属于登录用户的记录。显然我需要一个 IF 语句来查看两个值是否等于彼此。如何返回 null 这样,即没有单元格添加到表中。请看下面的代码

func tableView(_ tableView: UITableView, 
cellForRowAt indexPath: IndexPath) -> UITableViewCell {

guard let cell = tableView.dequeueReusableCell(withIdentifier:
"RunCell") as? RunCell else { return UITableViewCell() }

let user_id_main = String(MainMenuViewController.myVariables.user_id.prefix(2))

if (user_id_main==runs[indexPath.row].user_id)
{
cell.idLbl.text = "ID: " + runs[indexPath.row].run_id
cell.dateLbl.text = "Date: " + runs[indexPath.row].date_of_run

return cell
}
return cell
}

我知道 IF 之外的 return cell 仍然会返回一个单元格,直到我找到答案为止。

最佳答案

这不是 UITableViews 的工作方式。您无法即时决定是否显示某个单元格。您需要在加载或重新加载表之前执行此操作。

在加载或重新加载 tableView 之前,将您的数组过滤为一个新数组,该数组仅包含具有匹配 user_id 的记录。然后使用这个新数组作为您的表的模型。新数组的大小将决定表中的行数,每一行将对应新数组的一项。

每次 user_id_main 更改时,重新过滤您的 runs 数组并重新加载表。

关于swift - 如何在两个彼此相等的变量上过滤 UITableViewCell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49587470/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com