2

So I have the main view of my controller that has a table view. This table view will be displaying many different custom classes that subclass UITableViewCell. Some of these cells will ALSO have table views inside of them.

My problem is that I do not know what class I should assign to to be the UITableViewDelegate in this sort of situation for the table view in the table view cell. My intial thought was to make it the cell view class:

class MyTableViewCell: TableViewCell {

    @IBOutlet var tableView: UITableView!;

    var messages: Array<String>?;

    //called by parent tableview when cellForRowAtIndexPath is called in main controller
    //to initialize view with dynamic properties at run time
    override func render(obj: MyObject) {
        messages = obj.getMessages();
    }
}

extension MyTableViewCell: UITableViewDataSource {

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


    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return messages == nil ? 0 : messages!.count;
    }
}

This is problematic because I have no where to register my nib files to the table view:

nib = UINib(nibName: "MyTableViewCell", bundle: nil); self.tableView.registerNib(nib!, forCellReuseIdentifier: "custom");

Also, I feel like making a view a table view data source is violating MVC principles. What is the best way to go about with my table views within table view cells?

10
  • 4
    First question I'd ask myself ... do I really need tableViews within tableViewCells? Commented Oct 10, 2014 at 16:00
  • @MikePollard Are table views not the default solution to display a list of text elements? What would you propose? Obviously I could just iterate over the messages and add labels to the tableviewcell, but then I have to worry about dynamically settings constraints which is a pain. Commented Oct 10, 2014 at 16:10
  • Could section your top level tableView so that each 'list of text elements' is represented by a new section? Commented Oct 10, 2014 at 16:12
  • 2
    Perhaps check out 'Advanced User Interfaces Using Collection Views' here. developer.apple.com/wwdc/resources/sample-code . There is an accompanying WWDC 2014 video too. Commented Oct 10, 2014 at 16:21
  • 2
    UITableView inside UITableViewCell. That looks a bad idea. Commented Oct 16, 2014 at 14:16

2 Answers 2

2
+50

The UITableView within a UITableCell was indeed implemented in Pulse as described by genalipsis. In Obj-C,there is a full tutorial plus posted code located here for UITableView within a UITableCell:

http://iosstuff.wordpress.com/2011/06/29/adding-a-uitableview-inside-a-uitableviewcell/

http://iosstuff.wordpress.com/2011/06/29/creating-pulse-style-scrolling-horizontally-scrolling-uitableview-as-a-subview-of-uitableviewcell/

The was done in Xcode 4. I am not sure if this will work in Xcode 6.1 but it does describe a methodology.

I think an even more descriptive and easier to follow tutorial was posted at Ray Wenderlich's site here:

http://www.raywenderlich.com/4680/how-to-make-an-interface-with-horizontal-tables-like-the-pulse-news-app-part-1

http://www.raywenderlich.com/4723/how-to-make-an-interface-with-horizontal-tables-like-the-pulse-news-app-part-2

  1. Create a regular UITableView
  2. Create a custom UITableViewCell
  3. Add a rotated UITableView as a subview of our UITableViewCell
  4. Create another custom UITableViewCell for our articles
  5. Rotate our custom cell and use it for our horizontal table view

While the tuorial was from 2011, some of the comments are very recent, so the approach must still work.

There is also a github project that references a stack overflow discussion from earlier this year:

https://github.com/hefgi/TableViewInTableViewCell

If you open the project, the storyboard file for the iPhone is instructive (Main_iPhone.storyboard):

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

0

A table view inside of a table view has been done before. One of the first iPad Apps, Pulse, used this strategy to allow uses to scroll vertically between RSS feeds and horizontally, within each cell, between RSS entries. That is, the embedded table view was rotated and the cells inside it where also rotated so that their orientation allowed reading.

For usability reasons, you will probably want to follow a similar pattern, else it might be difficult to scroll.

Tentative architecture: MainTableViewController is the controller for the table view that contains sub table views. SubTableViewController is the controller for the table views contained within the cells of the MainTableViewController. This approach lets you use TableViewControllers in the "standard" fashion

class MainTableViewController: TableViewController {
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
                //dequeue a cell or create an instance of a cell
                //create instances of SubTableViewController or change the data source for indexPath
                //add the view of SubTableViewController to the cell's view hierarchy
                //make necessary view adjustments depending on orientation, etc.
    }

Not a standard setup, so I would expect that a few further hacks will be necessary.

2 Comments

Would you say that there is an ios UI component that could be used more effectively? That is also an option. As per comments above I am currently experimenting with UICollectionView as an alternative to TableView, though I do not understand why table views within table views seem to be such a foreign concept.
UICollectionViewLayoutAttribute allows you to specify center and size for each view, theoretically you can achieve a TableView within TableView layout. What I am not sure about is whether it will meet interaction requirements, i.e. scrolling.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.