表格中的自定义单元格视图不显示内容



我是自动布局和iOS的新手,因此我想创建Facebook NewsFeed页面,以便使用自动布局。我正在使用自定义单元格使用表观。到目前为止完成的事情是:

在故事板上放了一个桌面。

在各自的控制器类中创建了其插座。

为自定义单元创建了控制器类。

在两个单元格中删除了必要的视图,并在必要的控制器类中创建了插座。

在每个自定义单元格中设置适当的标识符。

现在,我面临的问题是两个单元格在接口构建器中正确显示,但仅在设备或模拟器中仅显示第一个单元格。由于我的声誉,图像在这里。接口构建器

模拟器

我搜索了很多,但没有找到任何合适的答案。我在哪里错了?我想念什么吗?显然没有自动布局问题。

例如,我也粘贴了控制器类的代码。

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

我可以从您的代码中看到您已经实现了两次的numberOfrowsInsection,而在您返回10的一个实例中,您只有1个。

在您的代码中,您要返回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
        }
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 10
}
@available(iOS 2.0, *)
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 1
}

您可能正在与Swift 3合作。返回1的方法适用于Swift 3,但不适用于Witch返回的方法10.此方法将被视为实例方法(您的控制器本地)。因此,实际上,您的表仅显示1行。

最新更新