Swift:静态UITableViewCell中的TableView [英] Swift: TableView within Static UITableViewCell

查看:143
本文介绍了Swift:静态UITableViewCell中的TableView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UITableViewController,其中包含大约五个不同的静态单元格.在这些单元格之一中,我尝试加载动态UITableView

I have a UITableViewController with about five different static cells. In one of these cells I'm attempting to load a dynamic UITableView

在界面构建器中,我标记了主 UITableViewController的 tableView0,然后标记了动态tableView1.

In the interface builder I tagged the main UITableViewController's tableView0, then I tagged the dynamic tableView1.

每次尝试加载控制器时,我都崩溃了.到目前为止,这是我的粗略代码:

I'm crashing every time I attempt to load the controller. Here is my rough code so far:

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    switch tableView.tag {
    case 0:
        return 2;

    case 1:
        return 1;


    default:
        break;

    }
    return 0
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    switch tableView.tag {
    case 0:
        return 5;

    case 1:
        return 2;


    default:
        break;

    }
    return 0

}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("ComboInfoCell", forIndexPath: indexPath) as! UITableViewCell
    if tableView.tag == 1 {


    // Honestly not sure how to configure only the reusablecells, and not affect the static cells


    }
    return cell

}

所以我需要知道是否可以在静态tableviewcell中嵌入动态tableview,以及如何做到这一点.

So I need to know if it's possible to embed dynamic tableviews inside static tableviewcells, and how to do that.

推荐答案

据我所进行的实验确定,您不能将相同的UITableViewController用作数据源和两个表视图的委托.使用静态表视图,您根本不应该实现数据源方法.奇怪的是,即使我断开了数据源并委托了静态表视图和表视图控制器之间的连接,该表视图仍会在表视图控制器类中调用numberOfRowsInSection.如果我在代码中将数据源显式设置为nil,这将阻止它调用数据源方法,但是嵌入式动态表视图也无法调用它们,因此此结构不起作用.

As far as I can determine by experimenting with this, you can't use the same UITableViewController as the data source and delegate of both table views. With a static table view, you're not supposed to implement the data source methods at all. The strange thing is, even if I disconnect the data source and delegate connections between my static table view and the table view controller, that table view still calls numberOfRowsInSection in my table view controller class. If I explicitly set the data source to nil in code, that stops it from calling the data source methods, but the embedded dynamic table view also fails to call them, so this structure doesn't work.

但是,您可以通过使用其他对象作为嵌入式动态表视图的数据源和委托来解决此问题.为您的嵌入式表视图创建IBOutlet,并将其数据源设置为该新对象(此示例中的类为DataSource,并且是NSObject的子类).

However, you can get around this by using a different object to be the data source and delegate of your embedded dynamic table view. Make an IBOutlet for your embedded table view, and set its data source and delegate to this new object (The class is DataSource in this example, and it's a subclass of NSObject).

class TableViewController: UITableViewController {

    @IBOutlet weak var staticTableView: UITableView!
    @IBOutlet weak var dynamicTableView: UITableView!
    var dataSource = DataSource()

    override func viewDidLoad() {
        super.viewDidLoad()
        dynamicTableView.dataSource = dataSource
        dynamicTableView.delegate = dataSource
    }

    override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        if indexPath.row != 1 {
            return 44
        }else{
            return 250 // the second cell has the dynamic table view in it
        }
    }
}

在DataSource类中,只需像往常一样实现数据源和委托方法.

In the DataSource class, just implement the data source and delegate methods as you normally would.

这篇关于Swift:静态UITableViewCell中的TableView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆