【问题标题】:When screen rotation ReloadData()屏幕旋转时 ReloadData()
【发布时间】:2015-01-20 13:43:29
【问题描述】:

当你转动我的设备屏幕时,我无法更新我的自定义视图。

我尝试过这样做:

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 的方法

【问题讨论】:

    标签: ios swift reloaddata


    【解决方案1】:
    override func didRotateFromInterfaceOrientation(fromInterfaceOrientation: UIInterfaceOrientation) {
    
        // Reload Data here
    
        self.view.reloadData() // Incorrect
    
    }
    

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

    你可以手动实现这个方法->玩得开心:)

    【讨论】:

      【解决方案2】:

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

      编辑要在屏幕方向改变时得到通知,设置通知:

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

      并实现选择器,例如

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

      【讨论】:

      • 旋转屏幕时如何调用reloadData?
      • 但是 OP 在他的问题中没有提到 tableViews
      • 这只是一个假设(我也注意到了)。他要了reloadData,我给了他。
      • 我喜欢你的方法 :) 我的意思是我只是在寻找问题以找到 tableView 并没有找到!
      • @user3351949 你可以投票,这比评论更有价值;)
      【解决方案3】:

      我认为这对你有用

      objective-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()
          
      }
      

      【讨论】:

      • 不,这不起作用。当我打开我的设备布局时,我的设备布局没有重新生成,你会看到邪恶
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-03
      • 2011-07-04
      • 1970-01-01
      相关资源
      最近更新 更多