views:

300

answers:

3

Hi guys,

I need to implement a 1 level outline view out of UITableView. The cells which have children in them will have a '+' symbol and if user taps on it, the cells below it should slide down and the children cells of current selected row should appear. The sliding of cells should be visible and if the user taps '-' button of the already expanded row, the children cells should slide back into the parent.

I have tried googling but did not find any pointers.

How should I start, should I subclass UITableView? Or should I implement my own view sub-class and handle all the stuff there?

I somehow feel it would be easier to sub-class UITableView because it will handle all the row selection for us, but I cannot figure out how to do it.

Thanks and Regards, Raj

+1  A: 

You must subclass and UITableViewCell

@interface OutlineCell : UITableViewCell

Then, in the ViewController of your table

- (UITableViewCell*) tableView: (UITableView*) tableView cellForRowAtIndexPath: (NSIndexPath*) indexPath {
    NSString* MyIdentifier = @"MyIdentifier";

    OutlineCell* cell = (OutlineCell*) [tableView dequeueReusableCellWithIdentifier: MyIdentifier];
    if (cell == nil) {
        cell = [[OutlineCell alloc] init];
    }

    // Populate cell data
}

You can define the interface via IB, creating an Empty XIB and dragging an UITableViewCell, with your custom OutlineCell class as File Owner. Then add buttons, labels and so on.

EDIT: if you want an inner table within a cell, try the same approach adding a subview to the UITableViewCell that you define, with a UITableView inside it.

Espuz
Brilliant approach, why didnt I think of it? Well, I have one doubt though, how do I achieve sliding effect of the rows which are below the row to be expanded? Any ideas for that? According to this approach, I believe, we have to provide the row height for each row, so when a table row is expanded we provide new row depending on the number of children. Say original row height was 20 and now it has 5 children, new row height will be 100. I guess that the table will just re-appear with the new row height without the sliding effect, how to circumvent that?
Raj
Well, I don't know if this works but, when the + button has been tapped, to expand the row and show the children rows, you must get the frame of the cell and change the size.height property within a UIView animations block.This work for me when I use information panels than collapses and expands. Maybe it works also in this situation ^^
Espuz
Note: This is a good approach, but I found it very difficult when it comes to reloading the table data and during animations. The new tableView can be embedded in cell by increasing its heigh, but when animations come into play, the tableView added as subview goes behind the cell, reason - I dont know! Also I really doubt if the tableView reload call for main tableView will reload the inner tableView or not. If not, we can reload the data when delegate -tableView:cellForRowAtIndexPath: of first tableView, but I doubt the order of reload. Better approach - Flatten the tree structure.
Raj
+2  A: 

Can't you maintain a dictionary as a datasource? dictionary holding array of dictionaries. And inner dictionary with two key value pairs:

  1. isExpanded
  2. children information.

By checking the bool isExpanded you will get the number of rows. And you need to customize the cell to add buttons. Will it work for you?

you can differentiate the children cells by pushing you cell contents slightly to right side.

Manjunath
Ok, so if the row is expanded, I will introduce more numberOfRows in the tableView delegate. How can I achieve the sliding effect of items below the row which has to be expanded by using this method? I suppose, here also the same problem occurs, the re-appearance of tableView when table view is reloaded on a row expansion.
Raj
Well, I suppose that sliding of the rows below the row which has to expand is handled by table view automatically. I will have to use the mixed solution of yours and Espuz's. Thanx both . . .
Raj
A: 

Raj did you ever get this to work, if you did could you post a link to the project.

wjwood