简体   繁体   中英

Custom cells in table view not displaying content

I am new to auto layouts and iOS in general therefore I wanted to create Facebook newsfeed page in order to have my hands on auto layouts. I am using table view with custom cells. Things done till now are:

Dropped a tableview in storyboard.

Created its outlet in respective controller class.

Created controller classes for custom cells.

Dropped necessary views in both cells and created there outlets in necessary controller classes.

Set the appropriate and identifiers in of each custom cell.

Now the problem which I am facing is that the both cells are displaying correctly in the interface builder but in device or simulator only the first cell displays. Images here because of my reputation. interface builder

simulator

I searched alot but didn't find any appropriate answer. Where am I wrong? Did I miss anything? Apparently there are no auto layout issues.

For instance, I am pasting the code of my controller class too.

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。

In your code you are returning 1 row so

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
}

You might be working with Swift 3. The method which returns 1 is applicable in Swift 3 but not the method witch returns 10. This method will be treated as a an instance method(local to your controller). So in reality your table will show only 1 row.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM