gpt4 book ai didi

ios - 如何在搜索后只加载 TableView ?

转载 作者:行者123 更新时间:2023-11-28 06:23:15 25 4
gpt4 key购买 nike

我希望创建一个 View Controller ,它可以让用户搜索设备,然后在他们单击搜索按钮后,我希望填充下面的 TableView 。问题是 tableview 在加载 View Controller 时加载正确,并且由于用户尚未搜索任何内容,应用程序崩溃,因为表中没有填充数据。我希望在用户单击搜索按钮之前不加载 tableview。

到目前为止,我有以下内容:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CustomTableViewCell

cell.deviceName.text = deviceArray[indexPath.row]

return cell
}

如何让这个在用户首先点击搜索按钮之前不加载?

最佳答案

我认为你应该在 func 中检查你的数据源

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return deviceArray.count
}

你应该在 viewDidLoad 中初始化 deviceArray

deviceArray = [];

如果您没有搜索,deviceArray 将有 count = 0 这样就不会加载更多的单元格

关于ios - 如何在搜索后只加载 TableView ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42743882/

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