gpt4 book ai didi

ios - 当单元格插入表格时,自定义表格 View 单元格内的 ImageView 消失

转载 作者:行者123 更新时间:2023-11-28 08:31:51 26 4
gpt4 key购买 nike

这是我第一次接触 iOS 开发。我正在创建一个应用程序,它使用自定义 UITableViewCell 来显示从 Core Data 检索到的信息。我遇到这个问题,当单元格插入表格时,显示颜色的 imageView 随机消失。如果 ImageView 确实消失了,关闭应用程序并重新启动将解决问题。

插入表后:pic强制关闭应用程序并重新启动后:pic

任何关于问题所在的建议都将不胜感激。

这里是一些相关的代码。如果有帮助,我也在使用 NSFetchedResultsController。

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> CustomTableViewCell {
let cell = self.tableView.dequeueReusableCellWithIdentifier("Cell") as! CustomTableViewCell
let object = self.fetchedResultsController.objectAtIndexPath(indexPath) as! NSManagedObject
self.configureCell(cell, withObject: object)

return cell
}

func configureCell(cell: CustomTableViewCell, withObject object: NSManagedObject) {

let imageData: NSData = object.valueForKey("image") as! NSData
let colorImageBlank: UIImage = UIImage(named: "blank.png")!
let picture: UIImage = UIImage(data: imageData)!
let date: NSDate
let dateFormatter = NSDateFormatter()
dateFormatter.locale = NSLocale.currentLocale()

cell.cellImage.image = picture

let colorString = object.valueForKey("color")!.description
var color: UIColor = UIColor()
if colorString == "Blue" {
color = UIColor.blueColor()
} else if colorString == "Red" {
color = UIColor.redColor()
} else if colorString == "Green" {
color = UIColor.greenColor()
} else if colorString == "Yellow" {
color = UIColor.yellowColor()
} else if colorString == "Orange" {
color = UIColor.orangeColor()
} else if colorString == "Purple" {
color = UIColor.purpleColor()
}

cell.colorImage.image = colorImageBlank
cell.colorImage.backgroundColor = color
cell.colorImage.layer.cornerRadius = 5.0

cell.cellImage.layer.borderWidth = 2
cell.cellImage.layer.borderColor = color.CGColor
cell.cellImage.layer.cornerRadius = 5.0


cell.locationNameLabel.text = object.valueForKey("name")!.description
cell.locationNameLabel.font = UIFont.boldSystemFontOfSize(14.0)
cell.locationNameLabel.lineBreakMode = NSLineBreakMode.ByTruncatingTail

cell.categoryLabel.text = object.valueForKey("category")!.description
cell.categoryLabel.font = UIFont.italicSystemFontOfSize(12.0)
cell.categoryLabel.lineBreakMode = NSLineBreakMode.ByTruncatingTail

date = object.valueForKey("date")! as! NSDate

dateFormatter.dateStyle = NSDateFormatterStyle.ShortStyle
cell.dateLabel.text = dateFormatter.stringFromDate(date)

cell.descriptionLabel.text = object.valueForKey("descript")!.description
cell.descriptionLabel.lineBreakMode = NSLineBreakMode.ByTruncatingTail

}

func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) {
switch type {
case .Insert:
tableView.insertRowsAtIndexPaths([newIndexPath!], withRowAnimation: .Fade)
case .Delete:
tableView.deleteRowsAtIndexPaths([indexPath!], withRowAnimation: .Fade)
case .Update:
self.configureCell(tableView.cellForRowAtIndexPath(indexPath!)! as! CustomTableViewCell, withObject: anObject as! NSManagedObject)
case .Move:
tableView.moveRowAtIndexPath(indexPath!, toIndexPath: newIndexPath!)
}
}

最佳答案

这似乎是一个框架问题,因为不仅 ImageView 还有两个其他日期标签和其他标签也丢失了,您是否向其添加了约束?

关于ios - 当单元格插入表格时,自定义表格 View 单元格内的 ImageView 消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38705893/

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