2017-09-14 108 views
0

我是新來的自動佈局和iOS的一般,所以我想創建Facebook的新聞源頁面,以便我的手在自動佈局。 我正在使用自定義單元格的表格視圖。直到現在完成的事情是:表格視圖中的自定義單元格不顯示內容

在故事板中丟棄了tableview。

在各自的控制器類別中創建它的出口。

創建自定義單元格的控制器類。

刪除了兩個單元格中的必要視圖,並在必要的控制器類中創建了出口。

在每個自定義單元格中設置合適的標識符。

現在我面臨的問題是兩個單元格都在界面生成器中正確顯示,但在設備或模擬器中只顯示第一個單元格。這裏的圖片是因爲我的聲譽。 interface builder

simulator

我搜索了很多,但沒有找到任何合適的答案。我錯在哪裏? 我錯過了什麼嗎?顯然沒有自動佈局問題。

例如,我也粘貼了我的控制器類的代碼。

class HomeScreen: UIViewController, UITableViewDelegate, UITableViewDataSource { 

     func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 
      if (indexPath.row == 0){ 
       return 100 
      } else { 
       return 200 
      } 
     } 

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

     @available(iOS 2.0, *) 
     func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
      return 1 
     } 

     @available(iOS 2.0, *) 
     func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
      if indexPath.row == 0 { 
       guard let cell : StatusUpdateCustomTableViewCell = tableView.dequeueReusableCell(withIdentifier: "StatusUpdateCell" , for: indexPath) as? StatusUpdateCustomTableViewCell else { 
        fatalError("The dequeued cell is not an instance of MealTableViewCell.") 
       } 

       cell.profilePicImageView.image = #imageLiteral(resourceName: "teenGirl") 
       return cell 
      } else if indexPath.row == 1 { 
       guard let cell : PostCustomTableViewCell = tableView.dequeueReusableCell(withIdentifier: "PostCell1" , for: indexPath) as? PostCustomTableViewCell else { 
        fatalError("The dequeued cell is not an instance of MealTableViewCell.") 
       } 
       cell.profilePicImageView.image = #imageLiteral(resourceName: "teenBoy") 
       return cell 
      } else { 
       let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "thirdCustomCell") 
       //set the data here 
       return cell 
      } 
     } 

     @IBOutlet weak var headerViewHeight: NSLayoutConstraint! 

     @IBOutlet weak var imageView1: UIImageView! 
     @IBOutlet weak var imageView2: UIImageView! 
     @IBOutlet weak var imageView3: UIImageView! 
     @IBOutlet weak var imageView4: UIImageView! 

     @IBOutlet weak var imageView1Height: NSLayoutConstraint! 
     @IBOutlet weak var imageView2Height: NSLayoutConstraint! 
     @IBOutlet weak var imageView3Height: NSLayoutConstraint! 
     @IBOutlet weak var imageView4Height: NSLayoutConstraint! 
     @IBOutlet weak var tableViewPosts: UITableView! 

     var windowWidth = 0.0; 
     var windowHeight = 0.0; 

     override func viewDidLoad() { 
      super.viewDidLoad() 

      tableViewPosts.delegate = self 
      tableViewPosts.dataSource = self 

      // calling rotated function when device orientation changes. 
      NotificationCenter.default.addObserver(self, selector: Selector("rotated"), name: NSNotification.Name.UIDeviceOrientationDidChange, object: nil) 
     } 

     override func viewDidLayoutSubviews() { 
      //print(self.view.frame.size) 
      windowWidth = Double(self.view.frame.width); 
      windowHeight = Double(self.view.frame.height); 
     } 

     func rotated(){ 
      if(UIDeviceOrientationIsLandscape(UIDevice.current.orientation)){ 
       // hiding imageview and underlining textfields 
       print("landscape") 
       headerViewHeight.constant = CGFloat(windowHeight * 0.07) 
       imageView1Height.constant = CGFloat(windowHeight * 0.07) 
       imageView2Height.constant = CGFloat(windowHeight * 0.07) 
       imageView3Height.constant = CGFloat(windowHeight * 0.07) 
       imageView4Height.constant = CGFloat(windowHeight * 0.07) 
      } 

      if(UIDeviceOrientationIsPortrait(UIDevice.current.orientation)){ 
       headerViewHeight.constant = CGFloat(windowHeight * 0.001) 
       imageView1Height.constant = CGFloat(windowHeight * 0.001) 
       imageView2Height.constant = CGFloat(windowHeight * 0.001) 
       imageView3Height.constant = CGFloat(windowHeight * 0.001) 
       imageView4Height.constant = CGFloat(windowHeight * 0.001) 
      } 
     } 
    } 
+0

與問題無關,但'@available(iOS 2.0,*)'沒有任何意義。 iOS1.0沒有AppStore,因此您的應用無法在運行iOS版本低於2.0的設備上運行,並且[運行iOS10的設備佔89%](https://developer.apple.com/support/app-store/ )和98%的運行iOS9或10,你真的認爲你的用戶的大量仍然會與原來的iPhone運行,他們從來沒有軟件更新? –

+0

這件事是由Xcode自動生成的。 – kinza

回答

0

在你的代碼正在返回1行,以便之一

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

     @available(iOS 2.0, *) 
     func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
      return 1// It's Wrong 
     } 
+0

我想你只是複製我的答案 – OverD

1

我可以從你的代碼,你已經實現了兩次numberOfRowsInSection看到在你返回10的情況下,另一隻1

0
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return 10 
} 

@available(iOS 2.0, *) 
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return 1 
} 

你可能會與斯威夫特3. 返回1適用於斯威夫特3,但不是方法巫婆返回10.本實現方法具的方法來工作d將被視爲實例方法(本地控制器)。所以實際上你的桌子只會顯示1行。

相關問題