屏幕旋转时ReloadData()

时间:2015-01-20 13:43:30

标签: ios swift reloaddata

当您关闭我的设备屏幕时,我无法更新自定义视图。

我试着这样做:

override func viewDidLoad() {
        super.viewDidLoad()


       var myCustomView = Customview()

       self.view.addsubview(myCustomView)
        }
    }

    override func didRotateFromInterfaceOrientation(fromInterfaceOrientation: UIInterfaceOrientation) {

        // Reload Data here

        self.view.reloadData() // Incorrect

    }

但是请告诉我错误:

方法" UIView"没有名为reloadData的方法

3 个答案:

答案 0 :(得分:3)

您想在reloadData()的实例上调用UITableView(假设有任何内容)。 UIView没有提供这样的方法。

修改以在屏幕方向更改时收到通知,设置通知:

NSNotificationCenter.defaultCenter().addObserver(self, selector: "didRotate:", name: UIDeviceOrientationDidChangeNotification, object: nil)

并实现选择器,例如

func didRotate(notification: NSNotification)
{
    myTableView.reloadData()
}

答案 1 :(得分:3)

override func didRotateFromInterfaceOrientation(fromInterfaceOrientation: UIInterfaceOrientation) {

    // Reload Data here

    self.view.reloadData() // Incorrect

}

UIView不是UITableView,它没有开箱即用的reloadData方法。

您可以手动实施此方法 - >玩得开心:)

答案 2 :(得分:1)

我认为它适合你

<强>目标-C

  

[self.view setNeedsDisplay];

<强>迅速

  

self.view.setNeedsDisplay()

 override func viewDidLoad() {
                super.viewDidLoad()


               var myCustomView = Customview()

               self.view.addsubview(myCustomView)
                }
            }

            override func didRotateFromInterfaceOrientation(fromInterfaceOrientation: UIInterfaceOrientation) {

                // Reload Data here

                self.view.setNeedsDisplay() 

            }
相关问题