Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
269 views
in Technique[技术] by (71.8m points)

ios - Swift: TableView within Static UITableViewCell

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

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

}

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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
        }
    }
}

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...