gpt4 book ai didi

ios - 在 Swift 2 中重新加载数据时,如何防止表格格式发生变化?

转载 作者:行者123 更新时间:2023-11-28 12:54:34 24 4
gpt4 key购买 nike

这是我的 UITableView 最初的样子。如您所见,它看起来很棒。:

enter image description here

重新加载数据后,它看起来像这样(不太好):

enter image description here

我的 Realm.io 对象如下所示:

import RealmSwift
import Foundation

class Set: Object {

dynamic var id = NSUUID().UUIDString
dynamic var set_id = ""
dynamic var descr = ""
dynamic var img_sm = ""
dynamic var img_tn = ""
dynamic var pieces = ""
dynamic var qty = ""
dynamic var theme = ""
dynamic var year = ""

// Specify properties to ignore (Realm won't persist these)

// override static func ignoredProperties() -> [String] {
// return []
// }

override static func primaryKey() -> String? {
return "id"
}
}

我给用户一个选项,通过一个警告来改变表格的排序,他们可以在其中选择几个选项:

func showSorting(errorTitle:String, errorMessage:String) {
let alert = UIAlertController(title: "\(errorTitle)", message: "\(errorMessage)", preferredStyle: .Alert) // 1
let firstAction = UIAlertAction(title: "Sort by Set Number", style: .Default) { (alert: UIAlertAction!) -> Void in
NSLog("Sorting by Number")

self.array = try! Realm().objects(Set).sorted("set_id", ascending: true)
self.tableView.reloadData()

} // 2
alert.addAction(firstAction) // 4

let secondAction = UIAlertAction(title: "Sort by Set Name", style: .Default) { (alert: UIAlertAction!) -> Void in
NSLog("Sorting by Name")
self.array = try! Realm().objects(Set).sorted("descr", ascending: true)
self.tableView.reloadData()
} // 3

alert.addAction(secondAction) // 5

presentViewController(alert, animated: true, completion:nil) // 6
}

很简单的东西。 Realm 对象被拉出并按所需方法排序,然后重新加载表。是数据的重新加载把事情搞砸了。

我正在为我的表格 View 单元格使用字幕样式。

单元格的渲染方式如下:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

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

let imageView = cell.viewWithTag(30) as! UIImageView

let Title = cell.viewWithTag(10) as! UILabel

let subTitle = cell.viewWithTag(20) as! UILabel

Title.textColor = UIColor.whiteColor()
subTitle.textColor = UIColor.whiteColor()
cell.backgroundColor = UIColor(red: 0.7176, green: 0.1647, blue: 0.2, alpha: 1.0) /* #b72a33 */

let object = array[indexPath.row]

let img_url = object.img_sm
cell.textLabel?.text = object.set_id

imageView.contentMode = .ScaleAspectFit

if let checkedUrl = NSURL(string: "\(img_url)") {
imageView.contentMode = .ScaleAspectFit

getDataFromUrl(checkedUrl) { (data, response, error) in
dispatch_async(dispatch_get_main_queue()) { () -> Void in
guard let data = data where error == nil else { return }
imageView.image = UIImage(data: data)
}
}
}

subTitle.text = object.descr

return cell
}

希望对您有所帮助。我不确定我做错了什么搞砸了?谢谢你的帮助,我非常感激! :)

最佳答案

  1. 请记住,当重新加载单元格时,它们会被重用。细胞的每个方面都必须重新设置;否则它可能是以前使用时遗留下来的。

  2. 看起来您正在尝试在 cellForRowAtIndexPath 中进行某种网络连接。你不能那样做。您必须立即返回单元格。如果您的数据需要联网,请返回占位符并单独进行联网(并在您不需要进行任何联网时重新加载单元格)。

因此,例如,这段代码是错误的:

if let checkedUrl = NSURL(string: "\(img_url)") {
imageView.contentMode = .ScaleAspectFit

getDataFromUrl(checkedUrl) { (data, response, error) in
dispatch_async(dispatch_get_main_queue()) { () -> Void in
guard let data = data where error == nil else { return }
imageView.image = UIImage(data: data)
}
}
}

网络延迟后,您不得像这样直接更改单元的任何方面。就你所知,到那时这个细胞甚至可能不存在。要从网络中获取数据,请将其获取到模型,然后重新加载表格。

关于ios - 在 Swift 2 中重新加载数据时,如何防止表格格式发生变化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35366183/

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