【问题标题】:Tableview automatically reload contentTableview 自动重新加载内容
【发布时间】:2016-08-04 23:00:11
【问题描述】:

我在从 tableview 移动到视图时遇到问题。

为了解释这个问题,我有一个名为 myObjects 的视图,其中包含一个表视图,其中加载了一些用户对象,如果用户点击单元格,则使用模态 segue 调用一个新视图(objectConsult)。

在这个视图中,用户将找到关于他的对象的所有信息,当他点击与模态 segue 链接的后退按钮时,他会回到 myObjects。

我的问题是,每次我从 objectConsul 中点击后退按钮时,我的 tableview 都会重新加载单元格,我已经成功阻止了 Objective-C 中的重新加载,但很快我不明白我必须做什么才能解决问题.

override func viewDidAppear(animated: Bool) {
             tableView.reloadData()
    }


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

    let cell:CustomCell = self.tableView.dequeueReusableCellWithIdentifier("Cell") as! CustomCell
    cell.picture.frame = CGRectMake(5,0,120,103)

    cell.title.text =  repos[indexPath.row].title

        let i = settings.ImagesCachedArray.indexOf({$0 == repos[indexPath.row].idobjet!})            
        let myImageName = "\(settings.ImagesCachedArray[i!]).png"            
        let imagePath = fileInDocumentsDirectory(myImageName)
        if let _ = self.loadImageFromPath(imagePath) {
            cell.picture.image = self.loadImageFromPath(imagePath)
        }
        else {
            let imgzeroresult = UIImage(contentsOfFile: NSBundle.mainBundle().pathForResource("noimage", ofType: "png")!)
            cell.picture.image = imgzeroresult
        }
}

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    performSegueWithIdentifier("mysegue", sender: self.view)
}

任何帮助将不胜感激。

【问题讨论】:

    标签: swift tableview segue reload


    【解决方案1】:

    问题是:

    override func viewDidAppear(animated: Bool) {
             tableView.reloadData()
    }
    

    将这个方法更改为 viewDidLoad

    override func viewDidLoad() {
             tableView.reloadData()
    }
    

    【讨论】:

    • @f1rstsurf viewDidAppear,顾名思义,每次视图出现在屏幕上时都会被调用。这意味着即使按下后退按钮也会调用它。另一方面,viewDidLoad 仅在视图第一次显示时调用,因为那是在加载时。
    • @f1rstsurf 我还建议仅出于一般理解目的查看通用视图控制器和 iOS 应用程序的生命周期。还有许多其他方法肯定会与您未来的发展有关。
    猜你喜欢
    • 1970-01-01
    • 2020-05-25
    • 1970-01-01
    • 2021-11-09
    • 2021-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多