2017-09-14 84 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行。

相关问题