【问题标题】:Custom cells in table view not displaying content表格视图中的自定义单元格不显示内容
【发布时间】:2017-09-14 09:47:47
【问题描述】:

我是自动布局和一般 iOS 的新手,因此我想创建 Facebook 新闻源页面,以便掌握自动布局。 我正在使用带有自定义单元格的表格视图。到目前为止所做的事情是:

在情节提要中删除了一个表格视图。

在各自的控制器类中创建了它的出口。

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

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

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

现在我面临的问题是两个单元格都在界面生成器中正确显示,但在设备或模拟器中只有第一个单元格显示。由于我的声誉,这里的图像。 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)
            }
        }
    }

【问题讨论】:

  • 与问题无关,但@available(iOS 2.0, *) 没有意义。 iOS1.0 没有 AppStore,因此您的应用程序无法在运行低于 2.0 的 iOS 版本和 89% of devices running iOS10 且 98% 运行 iOS9 或 10 的设备上运行,您真的认为您的大量用户仍然会在附近运行吗?使用他们永远不会更新软件的原装 iPhone?
  • 这个东西是 Xcode 自动生成的。

标签: ios iphone swift autolayout


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    在您的代码中,您将返回 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
            }
    

    【讨论】:

    • 我想你只是抄袭了我的答案
    【解决方案3】:
    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,但不适用返回 10 的方法。此方法将被视为实例方法(控制器本地)。所以实际上您的表格只会显示 1 行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多