iOS - UITableView in UIVIewController doesn't display all rows [Swift]

自作多情 提交于 2019-12-19 09:37:40

问题


I'm facing a bug with my UITableView in UIViewController. I'll try to explain what's the situation and my problem.

I have a TabBarController which a view is a UIViewController with a UITableView. I don't use a UITableViewController because I have some specifics controls on my UIViewController.

So, when my UIViewController isn't a child of my TabBarController, everything is okay : all rows are displayed correctly.

But, when I set my UIViewController as a child of my TabBarController, I have this bug : only some rows are displayed (one over threem exactly, in all scenarios).

I just don't understand what's happening. My code seems working, but only when my UIViewController isn't a child of my TabBarController.

Is there another method to add in this case ?

There is the code of my UIViewController :

class ProfileController: UIViewController, UITableViewDataSource, UITableViewDelegate {

    @IBOutlet weak var tableChallenges: UITableView!
    @IBOutlet weak var profileTabs: UISegmentedControl!

    var pageViewController : UIPageViewController!

    override func viewDidLoad() {
        super.viewDidLoad()

        self.tableChallenges.dataSource = self
        self.tableChallenges.delegate = self

        self.tableChallenges.reloadData()

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

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

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCellWithIdentifier("challengeCustomCell", forIndexPath: indexPath) as! ChallengeCustomCell

        switch(self.profileTabs.selectedSegmentIndex) {
            case 0:
                cell.challengeSuccededOrFailedImage.hidden = true
            break

            case 1:
                cell.challengeSuccededOrFailedImage.hidden = false
                cell.challengeSuccededOrFailedImage.image = UIImage(named: "challenge-succeded")
            break

            case 2:
                cell.challengeSuccededOrFailedImage.hidden = false
                cell.challengeSuccededOrFailedImage.image = UIImage(named: "challenge-failed")
            break

            case 3:
                cell.challengeSuccededOrFailedImage.hidden = true
            break

            default: break

        }

        return cell
    }

    @IBAction func selectedItemChanged(sender: AnyObject) {
        self.tableChallenges.reloadData()
    }
}

Thanks in advance for your answers ! :)


回答1:


Ever since MainStory boards came out I'm not a big fan of it, I had the same error the other night, I had to get rid of my View Controller and make a View Controller out of a .xib file.

  • I delete the ViewController from my porject
  • Then I went to the AppDelagate and added a variable to create my own ViewController
    var viewController: ViewController?
  • Under the didFinishLaunchingWithOptions: function before the return

  • I added the following code
    self.window = UIWindow(frame:UIScreen.mainScreen().bounds) self.viewController = ViewController(nibName:"ViewController",bundle:nil) var navController = UINavigationController(rootViewController: self.viewController!) navController.navigationBarHidden = true self.window?.rootViewController = navController self.window?.makeKeyAndVisible()

Basically after drawing the window frames we created the ViewController and embed it in a navigation controller, then sets the navigation controller as the root view of the window.

  • So then you have to create a xib file File > New > File choose IOS/User Interface; name it ViewController and create. After you can add the UITableView Controller and everything should work fine.



回答2:


Sorry about the long time without feedback.

I have finally found a solution to my problem. I embedded my UIViewController which contains the UITableView in a UINavigationController. After a few researchs, it seems to be the good hierarchy for this case.

Hope this will help somebody helse. :)



来源:https://stackoverflow.com/questions/33177968/ios-uitableview-in-uiviewcontroller-doesnt-display-all-rows-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!