如何在swift3中的其他方法更新UITableviewcell标签?

时间:2017-09-29 07:40:43

标签: ios uitableview swift3

我正在尝试用其他方法更新UITableviewcell的标签。我也看到很多关于stackoverflow的帖子,非对我有用。我正在使用Swift 3,我尝试了这个:

    let indexPath = IndexPath.init(row: 0, section: 0)
    let cell : CategoryRow = tableView.cellForRow(at: indexPath) as! CategoryRow

我收到此错误:

fatal error: unexpectedly found nil while unwrapping an Optional value

我在其他方法中使用这个,我在24小时后更新tableviewcell标签文本。 代码在这里

var myQuoteArray = ["“If you have time to breathe you have time to meditate. You breathe when you walk. You breathe when you stand. You breathe when you lie down. – Ajahn Amaro”","We are what we repeatedly do. Excellence, therefore, is not an act but a habit. – Aristotle","The best way out is always through. – Robert Frost","“If you have time to breathe you have time to meditate. You breathe when you walk. You breathe when you stand. You breathe when you lie down. – Ajahn Amaro”","We are what we repeatedly do. Excellence, therefore, is not an act but a habit. – Aristotle","The best way out is always through. – Robert Frost"

    ]
func checkLastRetrieval(){

   //        let indexPath = IndexPath.init(row: 0, section: 0)
   //        let cell : CategoryRow = tableView.cellForRow(at: indexPath) as? CategoryRow


    let indexPath = IndexPath.init(row: 0, section: 0)
   guard let cell = tableView.cellForRow(at: indexPath) as? CategoryRow else { return }

    print("Getting Error line 320")// This is not printing on console

let userDefaults = UserDefaults.standard

if let lastRetrieval = userDefaults.dictionary(forKey: "lastRetrieval") {
if let lastDate = lastRetrieval["date"] as? NSDate {
if let index = lastRetrieval["index"] as? Int {
if abs(lastDate.timeIntervalSinceNow) > 86400 { // seconds in 24 hours
// Time to change the label
var nextIndex = index + 1

// Check to see if next incremented index is out of bounds
if self.myQuoteArray.count <= nextIndex {
// Move index back to zero? Behavior up to you...
nextIndex = 0
}



cell?.quotationLabel.text = self.myQuoteArray[nextIndex]

let lastRetrieval : [NSObject : AnyObject] = [
"date" as NSObject : NSDate(),
"index" as NSObject : nextIndex as AnyObject
]

userDefaults.set(lastRetrieval, forKey: "lastRetrieval")
userDefaults.synchronize()
}
// Do nothing, not enough time has elapsed to change labels
}
}
} else {

// No dictionary found, show first quote
cell?.quotationLabel.text = self.myQuoteArray.first!

     print("line 357")
// Make new dictionary and save to NSUserDefaults
let lastRetrieval : [NSObject : AnyObject] = [
"date" as NSObject : NSDate(),
"index" as NSObject : 0 as AnyObject
]

userDefaults.set(lastRetrieval, forKey: "lastRetrieval")
userDefaults.synchronize()
}

}

并在ViewDidLoad()

中调用此方法
 override func viewDidLoad() {
    super.viewDidLoad()

     checkLastRetrieval()
    }

我该怎么办? 在此先感谢

2 个答案:

答案 0 :(得分:1)

如果请求的单元格当前不在屏幕上,则函数cellForRow(at:)将返回nil。由于您在viewDidLoad中调用了您的函数,因此屏幕上不会显示任何单元格。这会导致崩溃,因为您强制转发cellForRow(at:)的结果。

除非你是绝对的,否则你不应该使用武力展开或强制降低力量 某些结果不能是nil和/或唯一可能的操作,如果nil是您的应用崩溃。

通常,您不应直接更新单元格。最好更新表的数据模型,然后调用reloadRows(at:,with:)并让数据源cellForRow(at:)函数处理更新单元格。

如果您要直接更新单元格,请确保使用if letguard let的条件向下转换,以避免在单元格不可见时发生崩溃。在这种情况下,您仍然需要更新数据模型,以便在单元格进入屏幕时单元格数据正确。

答案 1 :(得分:0)

您应该使用guard let声明:

let indexPath = IndexPath(row: 0, section: 0)
guard let cell = tableView.cellForRow(at: indexPath) as? CategoryRow else { return }

如果您的cell类型不是CategoryRow,您将从该方法返回。尝试找出为什么cell类型不是CategoryRow