ios - TableView 不使用 ReloadData() 进行更新

标签 ios swift uitableview tableview

我有一个包含两个单元格的 tableView。第一个单元格包含一个标签,第二个单元格包含一个 DatePicker。第一个单元格使用类:

import UIKit

class cell1: UITableViewCell {
    @IBOutlet weak var label1: UILabel!
}

另一个单元格使用该类:

import UIKit

class cell2: UITableViewCell {
    @IBOutlet weak var outletPicker: UIDatePicker!

    @IBAction func actionPicker(sender: AnyObject) {
        let storyBoard = UIStoryboard(name : "Main" , bundle:  nil)
        let view = storyBoard.instantiateViewControllerWithIdentifier("ViewController") as! ViewController
        view.lblString = String(outletPicker.date)
        view.tableView.reloadData()
        view.tableView.layoutIfNeeded()
    }
}

正如您在 DatePickerAction 中看到的,我想更改第一个单元格的标签。在 mainClass("TableViewController class") 中,我定义了一个名为 lblString 的字符串。并改变它。 MainClass代码如下。

import UIKit

class ViewController: UITableViewController {
    var lblString : String = "initialString"

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 2
    }

    override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 30
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
    {
        if(indexPath.section == 0)
        {
           if(indexPath.row == 0)
           {
            let cell = tableView.dequeueReusableCellWithIdentifier("cell1") as! cell1
            cell.label1.text = lblString;
            return cell
            }
        }
        else
        {
            if(indexPath.row == 0)
            {
                let cell = tableView.dequeueReusableCellWithIdentifier("cell2") as! cell2
                return cell
            }
        }
        return UITableViewCell()
    }

    override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        if(indexPath.section == 0)
        {
           return 40
        }
        else
        {
            return 120
        }
    }
}

在 MyDatePicker 执行并 reloadData 后,我可以看到 cellForRowAtIndexPath 函数正在通过断点运行。但cell1的标签仍然是“initialString”。我该如何解决这个问题?

MyTableViewScreen

最佳答案

在此方法中:

@IBAction func actionPicker(sender: AnyObject) {

    let storyBoard = UIStoryboard(name : "Main" , bundle:  nil)
    let view = storyBoard.instantiateViewControllerWithIdentifier("ViewController") as! ViewController
    view.lblString = String(outletPicker.date)
    view.tableView.reloadData()
    view.tableView.layoutIfNeeded()
}

您正在实例化一个新的 View Controller ,但您没有呈现它 - 它只会在方法结束时被释放。您可能想要将对原始 View Controller 的引用传递给该单元格,或者可能使用委托(delegate)模式从单元格通信回 View Controller ,或者类似的东西。

关于ios - TableView 不使用 ReloadData() 进行更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41249561/

相关文章:

ios - swift 中的协议(protocol)与多态性

objective-c - 如何聚焦 Collection View 可见单元格的最后一个单元格?

ios - 带有 fireDate 的通知会立即触发

iphone - 切换到 iOS 7 后 UITableView 不滚动

uitableview - textField 在 UITableView 中出现两次

ios - 位置服务 : LocationAlways vs LocationWhenInUse

ios - 大图像导致 UITableView 应用程序崩溃

ios - Quickblox 中的 Apple mach-o 链接器错误

ios - 类型 'ViewController' 不符合协议(protocol) 'UITableViewDataSource'

ios - 核心数据更新时自定义 UITableViewCell 子类更新