gpt4 book ai didi

swift - 无法在一个 View 单元格中使用两个不同的 TableView 单元格

转载 作者:行者123 更新时间:2023-11-30 13:49:26 26 4
gpt4 key购买 nike

我从解析中获取数据并将它们放入 TableView 中(想想帖子)。在我的应用程序中,有用户分享的帖子;有些有图像,有些没有。我想制作两个不同的 View 单元,其中一个具有 ImageView ,另一个没有。

但是,我在设置数据时遇到了麻烦。如果在解析过程中帖子中没有图像,我会得到一个 nil 异常。我该如何解决这个问题并使它们处于不同的 View 中?

这是我从解析中获取数据的地方:

 if let objects = objects {
for object in objects {
let titles = object["titleOfPost"] as! String
self.postTitles.append(titles)
let messages = object["messageOfPost"] as! String
self.postMessages.append(messages)

if (object["imageOfPost"] != nil) {
let feedImageFile:PFFile = object["imageOfPost"] as! PFFile
feedImageFile.getDataInBackgroundWithBlock({ (imageData, error) -> Void in
if (imageData != nil) {
self.postImages.append(UIImage(data:imageData!)!)
}
})
}
}
}

这是我在表格 View 中使用的代码:

 override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("TextViewCell", forIndexPath: indexPath) as! TextPostTableViewCell

cell.titleOfPost.text = postTitles[postTitles.count - indexPath.row-1]
cell.messageOfPost.text = postMessages[postMessages.count - indexPath.row-1]
cell.username.text = "osman"
cell.profileImage.image = UIImage(named:"icon_ios_user")
cell.email.text = "email@email.com"

return cell
}

如何解决数据问题,然后将 2 个不同的 View 单元格放入一个 TableView 中,并按共享帖子的时间排序?

最佳答案

您可以使用 if 语句来检查图像是否存在,例如:

 if let image = dictData.objectForKey("Image"){
cell.imageView?.image = image as? UIImage
}

如果您需要在表格中显示 2 个完全不同的单元格,则创建自定义单元格,为它们提供唯一的重用标识符并在 viewDidLoad 中注册:

override func viewDidLoad() {
super.viewDidLoad()

// Custom Cell with Image
self.tableView.registerNib(UINib(nibName: "CustomCellImage", bundle: nil), forCellReuseIdentifier: "CustomCellImage")

// Custom Cell with Text
self.tableView.registerNib(UINib(nibName: "CustomCellText", bundle: nil), forCellReuseIdentifier: "CellText")

}

然后在 cellForRowAtIndexPath 中选择要在任何条件下使用的单元格:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let dictData : NSDictionary = arrayOfData.objectAtIndex(indexPath.row) as! NSDictionary

let strTypeObject = dictData.objectForKey("ObjectType") as! String


if strTypeObject == "ImageType"
{
let cell: CustomCellImage = tableView.dequeueReusableCellWithIdentifier("CustomCellImage", forIndexPath: indexPath) as! CustomCellImage
cell.lblImageText.text = dictData.objectForKey("ObjectMessage") as? String
cell.customImageView.image = dictData.objectForKey("Image") as? UIImage
return cell
}

if strTypeObject == "StringType"
{
let cell: CustomCellText = tableView.dequeueReusableCellWithIdentifier("CellText", forIndexPath: indexPath) as! CustomCellText
cell.lblJustSomeText.text = dictData.objectForKey("ObjectMessage") as? String
return cell
}


let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)

cell.textLabel?.text = dictData.objectForKey("ObjectMessage") as? String
if let image = dictData.objectForKey("Image"){
cell.imageView?.image = image as? UIImage
}
return cell
}

关于swift - 无法在一个 View 单元格中使用两个不同的 TableView 单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34479645/

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