【问题标题】:UITableView embedded in UIView, empty rowsUITableView 嵌入在 UIView 中,空行
【发布时间】:2016-05-08 10:50:51
【问题描述】:

我正在开发一个新应用程序,但遇到了一些问题:

我正在以编程方式开发所有内容,因此我为我的所有页面创建了一个 SCROLLVIEW 和一个 CONTAINERVIEW。 containerView 嵌入在 scrollView 中。

每个页面都有不同的控制器,在本例中为“SearchViewController”。 containerView 包含属于“SearchViewController”的 UIView“selectView”。见下文:

let scrollView: UIScrollView = UIScrollView()
let containerView: UIView = UIView()

//SCROLLVIEW
scrollView.frame =  CGRectMake(0, 0, view.frame.width, view.frame.height)
self.view.addSubview(scrollView)

//CONTAINERVIEW
containerView.frame = CGRectMake(menuWidth, 0, view.frame.width, view.frame.height)
containerView.backgroundColor = UIColor.whiteColor()
scrollView.addSubview(containerView)

let s: SearchViewController = SearchViewController()
s.setup(bannerHeight, containerViewWidth: containerView.frame.width, containerViewHeight: containerView.frame.height)

containerView.addSubview(s.selectView)


import UIKit

class SearchViewController: UIViewController, UITextFieldDelegate, UITableViewDelegate, UITableViewDataSource {

    let selectView: UIView = UIView()

    override func viewDidLoad() {
        super.viewDidLoad()
    }


    let table: UITableView = UITableView()
    var items: [String] = ["A", "B", "C"]
    var filteredItems = [String]()


    func setup(bannerHeight: CGFloat, containerViewWidth: CGFloat, containerViewHeight: CGFloat){
        //SELECT PAGE
        selectView.frame = CGRectMake(0, bannerHeight, containerViewWidth, containerViewHeight - bannerHeight)

        //UITABLEVIEW
        table.frame = CGRectMake(0, 0, selectView.frame.width, selectView.frame.height)
        table.delegate = self
        table.dataSource = self
        table.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
        selectView.addSubview(table)
    }


    func numberOfSectionsInTableView(tableView:UITableView)->Int
    {
        return 1
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        print("tableViewCOUNT")
        return self.items.count
    }

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

        let cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier("cell")! as UITableViewCell

        cell.textLabel!.text = items[indexPath.row]
        cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator

        return cell
    }
}

所以现在我有了这个层次结构:ScrollView

问题是没有数据出现!在输出中我可以看到“tableViewCOUNT”,但从来没有看到“tableViewMAIN”,除了空行没有数据。

有人可以帮我理解为什么吗? 我使用正确的方法吗?我想要一个主控制器,每个页面都有一个子控制器,然后我会将我需要的页面嵌入到 containerView 中(就像 html 中的 iframe)。

提前谢谢你!

【问题讨论】:

    标签: ios iphone uitableview uiview


    【解决方案1】:

    尝试调用

    table.reloadData()
    

    在设置中。

    【讨论】:

    • 试过了,还是不行。我设法通过在self.containerView.addSubview(s.selectView) 之后调用dispatch_async(dispatch_get_main_queue(),{ s.table.delegate = s s.table.dataSource = s }); 使一些行出现,但触摸一行或滚动它们又消失了。
    猜你喜欢
    • 2020-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-26
    • 2015-08-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多