gpt4 book ai didi

ios - Tableview 拖到底部时导致应用程序崩溃

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

当滚动超过表格 View 中的最后一个帖子/项目时,应用程序崩溃,并且收到错误消息:“展开可选值时意外发现 nil”。就像它试图找到另一个不存在的帖子一样,但我不知道为什么......表格 View 中的帖子按其应有的方式显示。

我认为这里有问题:

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

let post = posts[indexpath.row]

if let cell = tableView.dequeueReusableCell(withIdentifier: "PostCell", for: indexpath) as? PostCell{
cell.configureCell(post: post)

return cell
} else {
return PostCell()
}
}

(当我删除 else 部分时,出现此错误:“预期返回 'UITableViewCell' 的函数中缺少 return )

var posts = [Post]()

配置单元:

func configureCell(post: Post) {

self.post = post



if post.altA.isEmpty == false {
altALabel.text = post.altA["text"] as? String
if let votes = post.altA["votes"]{
self.altAVotesLbl.text = "\(votes)"
}
} else {
print("No data found in Alt A")
}

if post.altB.isEmpty == false {
altBLabel.text = post.altB["text"] as? String
if let votes = post.altB["votes"]{
self.altBVotesLbl.text = "\(votes)"
}
} else {
print("No data found in Alt B")
}

if post.altD.isEmpty == false {
altDLabel.text = post.altD["text"] as? String
if let votes = post.altD["votes"]{
self.altDVotesLbl.text = "\(votes)"
}
} else {
print("No data found in Alt D")
altDView.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0)
altDVotesView.removeFromSuperview()
altDLabelView.removeFromSuperview()
}

if post.altC.isEmpty == false {
altCLabel.text = post.altC["text"] as? String
if let votes = post.altC["votes"]{
self.altCVotesLbl.text = "\(votes)"
}
} else {
print("No data found in Alt C")
self.altCView.removeFromSuperview()
self.botBtnsStackView.removeFromSuperview()
}



import Foundation

类(class)帖子{

private var _text: String!
private var _postKey: String!
private var _backgroundImg: Int!

private var _altA: Dictionary<String, Any>!
private var _altB: Dictionary<String, Any>!
private var _altC: Dictionary<String, Any>!
private var _altD: Dictionary<String, Any>!




//TEXT
var text: String {
return _text
}

//BACKGROUND
var backgroundImg: Int{
return _backgroundImg
}

//POSTKEY
var postKey: String{
return _postKey
}


//ALTERNATIVES
var altA: Dictionary<String, Any> {
return _altA
}

var altB: Dictionary<String, Any> {
return _altB
}

var altC: Dictionary<String, Any> {
return _altC
}

var altD: Dictionary<String, Any> {
return _altD
}



init(postKey: String, postData: Dictionary<String, Any>) {
self._postKey = postKey

if let text = postData["text"]{
self._text = text as! String
} else {
self._text = ""
}


if let backgroundImg = postData["backgroundImg"]{
self._backgroundImg = backgroundImg as! Int
}



if let altA = postData["altA"]{
self._altA = altA as! Dictionary<String, Any>
}

if let altB = postData["altB"]{
self._altB = altB as! Dictionary<String, Any>
}


if let altC = postData["altC"]{
self._altC = altC as! Dictionary<String, Any>
} else {
let emptyDictionary = [String: Any]()
self._altC = emptyDictionary
}


if let altD = postData["altD"]{
self._altD = altD as! Dictionary<String, Any>
} else {
let emptyDictionary = [String: Any]()
self._altD = emptyDictionary
}
}

}

其余的表格 View 代码(没什么特别的):

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


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

最佳答案

是的,所以问题是您正在从 super View 中删除 self.altCView 和其他 View 。

您必须记住,单元格是可重复使用的,因此每当它们离开屏幕时,它们就会“出队”并重新使用。

因此,如果一个单元格最初是 altD 单元格,并且您删除了所有其他 super View ,但随后它会被重新用作 altC 单元格。尝试设置标签将使应用程序崩溃,因为从 super View 中删除将从内存中释放它(如果您使用弱引用,由于某种原因,该引用是 IBOutlet 的默认值。另一个提示,使所有 IBOutlet 都强大,我认为这样更好)

一旦 View 从 super View 中删除,它们就会永远消失,除非您重新添加它们,所以我会避免在像 cellForRowAtIndex 这样多次运行的代码上执行此操作

尝试将它们设置为隐藏(或框架为CGZero)(但再次,请注意单元格重用。假设您获得的单元格永远不会有正确的值,因此如果您'正在使用它们,并且如果不使用它们,则始终设置为隐藏。如果确实需要,则忘记 setHidden = false 是一个常见错误。它可能在上次使用单元格时仍处于隐藏状态)

关于ios - Tableview 拖到底部时导致应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47339148/

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