gpt4 book ai didi

ios - Swift UITableView 索引超出范围错误

转载 作者:行者123 更新时间:2023-11-28 10:49:00 25 4
gpt4 key购买 nike

我正在 Swift 中运行一个 Firebase 应用程序。在这里,我正在获取关注者和关注用户。在用户刚刚注册的情况下,应用程序会崩溃,因为可能还没有关注者/正在关注的用户。我已经尝试了所有我能想到的方法来处理错误,但都无济于事。我的问题是:1.如何避免这种崩溃?2. 有什么方法可以放置图片或按钮而不是空白表格,从而将它们重定向以关注其他用户?

代码如下:

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

let cell = tableView.dequeueReusableCell(withIdentifier: "InProfileCell", for: indexPath) as! ConnectionsCell

if currentIndex == 1 {
let user = followerusers[indexPath.row]
cell.showFollowersUsers(val: user)
print("follower")
} else {
let user = followingUsers[indexPath.row]
cell.showFollowingUsers(val: user)
print("following")
}

return cell
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

if currentIndex == 0 {
return followingUsers.count
} else {
return followerusers.count
}
}

func numberOfSections(in tableView: UITableView) -> Int {
return 1
}

最佳答案

您的numberOfRowscellForRow 中的逻辑不一致。您的 numberOfRows 指出

if currentIndex == 0 {
followingUsers
} else {
followerUsers
}

但是,您的 cellForRow 与此不一致,因为它声明

if currentIndex == 1 { //NOTE THAT YOU ARE USING 1 HERE INSTEAD OF 0
followerUsers
} else {
followingUsers
}

现在,当 currentIndex 为 2 时,numberOfRows 将返回 followerUsers 的计数,但 cellForRow 会尝试访问 followingUsers 的值(而不是 followerUsers)。这就是索引超出范围的原因。

关于ios - Swift UITableView 索引超出范围错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47023742/

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