再次显示viewcontroller时,在uitableviewcell上渲染视图

时间:2016-01-27 10:22:33

标签: ios swift uitableview

这是我的cellForRowAtIndexPath方法:

     func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
     let documentPartCell = tableView.dequeueReusableCellWithIdentifier("documentPartCell", forIndexPath: indexPath) as! DocumentPartCell
                documentPartCell.documentPart = documentPart
    return documentPartCell 
    }

这是我的细胞类:

class DocumentPartCell: UITableViewCell {
    @IBOutlet weak var documentPartProgress: UILabel!
    var allFields: Int
    var invalidFields: Int
    var documentPart: DocumentPart? {
        didSet {
self.documentPartProgress.text = "\(Int(allFields)! - Int(invalidFields)!) / \(allFields)"
}

基本上,documentPartProgress标签显示了文档的进度。我的应用程序的流程是有一个单元格显示VC1中的进度。按下单元格后,它将移动到VC2,您可以进行更改。当您回答问题时,进度会发生变化。当您在VC2中按back按钮时,它会返回到VC1并应更新documentPartProgress。但事实并非如此。当按下VC2中的后退按钮时,如何更新UITableViewCell上的视图?

例) 期望:在变化之前 - > 3/5。更改后 - > 4/5

现实:改变之前 - > 3/5。更改后 - > 3/5

进度标签文字不会改变。

2 个答案:

答案 0 :(得分:2)

只需将yourTableView.reloadData()放入viewWillAppear()而不是viewDidLoad()

注意,我想你已经更新了dataSource数组。如果您没有更新dataSource,则需要使用委托或发布通知来更新它。

如果是这种情况,请告诉我,我会更新我的答案以获取更多详情。

答案 1 :(得分:0)

由于您正在处理多个视图控制器,因此我将创建与全局进度相关联的变量(在课前定义它们)。这应该在整个项目中准确更新。 (这可能不是最好的做法,但它会完成工作)