DungProton
DungProton

Reputation: 229

How to custom UITableView contain many rows, each row contain many subrow?

enter image description here

How to custom UITable like this:UITableView contain many rows, each row contain many subrows. Use add button to add a subrow ?

Upvotes: 0

Views: 611

Answers (4)

pmk
pmk

Reputation: 1897

I have a similar project to this on Github. Basically its a UITableView with expandable sections. The following steps made it work for me:

  1. NSMutableDictionary for the dataSource, the dict contains NSMutableArrays as values for each row as key.
  2. You will have another NSMutableArray with the same size as your NSMutableDictionary. This NSMutableArray contains BOOL values, YES stands for the section is unfolded and NO for a folded section. In my Case, by default all sections are folded. 3.The number of sections in your tableView is the size of your NSMutableDictionary.
  3. If your NSMutableArray of Bool values contains a YES at a certain indexpath, the number of rows for this section is the size of the array you retrive as a value for the indexpath from your NSMutableDictionary. Otherwise you will have only 1 row for this section.

Example:

if ([[boolArray objectAtIndex:section] boolValue]) {
    return [[yourDict valueForKey:yourKeyForIndexPAth] count];
}else{
    return 1;
}

Just check it out und feel free to use the code:

GitHub Link

Upvotes: 0

Anupdas
Anupdas

Reputation: 10201

You can create a structure like this

[
    {
        "Title": "Section 1",
        "Rows": [
            {
                "Title": "Row 1",
                "Rows": [
                    {
                        "Title": "Sub Row 1", 
                        "Rows":[]
                    }
                ]
            }
        ]
    }
]

It's an array of dictionaries. Each section will have an array of rows and each row will have an array of subrows.

Source Code

Upvotes: 1

Divyam shukla
Divyam shukla

Reputation: 2048

I think you should use custom table view cell that having a table view.

http://www.appcoda.com/customize-table-view-cells-for-uitableview/

Check this hope this will help you.

Upvotes: 0

SpaceDust__
SpaceDust__

Reputation: 4914

You can use grouped tableview for that but it wont give you the full row width for iphone like in your picture.

If you use plain tableview it should give you the full row look but adding that plus button may become difficult.

You need to have a source lets say an array or a dictionary for each section rows

For the image use cell.imageView.image= yourimage;

For the arrow use cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

For that (10) you need either use cell.accessoryView= yourcustomview

//create multiple section according to your picture it is 2 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    //return an integer here from a source,variable or just return static int 
    return 2; 
}
//add plus button for each section
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
// Create header view and add label as a subview
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(100, 400, 320, 400)];

    //add a button to open edit
    UIButton *declineButton=[UIButton buttonWithType:UIButtonTypeCustom];
    [declineButton setTag:section];
    declineButton.titleLabel.tag=1;
    [declineButton setFrame:CGRectMake(450,50,70,40)];
    [declineButton setTitle:@"Edit" forState:UIControlStateNormal];
    UIImage *declinebuttonImage = [[UIImage imageNamed:@"[email protected]"] resizableImageWithCapInsets:UIEdgeInsetsMake(20, 20, 20, 20)];
    [declineButton setBackgroundImage:declinebuttonImage forState:UIControlStateNormal];
    [declineButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [declineButton addTarget:self action:@selector(declineEvent:) forControlEvents:UIControlEventTouchUpInside];

    [view addSubview:declineButton];
    return view;
}

- (IBAction)declineEvent:(id)sender {
    UIButton *button = (UIButton *)sender;
    NSInteger row = button.tag;
   NSInteger column = button.titleLabel.tag;
    //NSIndexPath *selectedSection= [NSIndexPath indexPathForRow:column inSection:row];
    NSLog(@"Row is %i column is %i",row,column);
    //populate your source array for the section according to your needs here 

}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return yoursourceforthissection;

}

Upvotes: 0

Related Questions