0


I'm looking for the best possible solution for a complex UITableView.

What I want :
I have an arbitrary number of sections.
In this section is just one static cell and then should come any number of dynamic cells.

My Data are stored in some NSMutableArray and i tried somethink like this:
Combine static and prototype content in a table view
But i dont know how to handle it with my kind of problem. So can somebody give me a hint or a best practice?
UPDATE: 1
http://jsfiddle.net/konstheinrich188/HKkA8/ Fiddle shows how my data looks and what im trying todo

This pic shows how i want to code my tableview...
What i want

Community
  • 1
  • 1
  • Your problem would seem identical so what issues did you have implementing the solution from the other question? – Wain Aug 24 '13 at 14:29
  • I dont know how to create x sections with different type of cells.. My thinking is : foreach item in myArray create a section. Foreach section add one static cell and [myotherArray count] dynamic cells ... – Konstantin Heinrich Aug 24 '13 at 14:34
  • Why not an array of arrays? Outer array is sections and each inner array is the dynamic rows for the section... – Wain Aug 24 '13 at 14:41
  • You can use the tableView delegate method: -`(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return [self.myArray count];}` to create the sections you need. Use this in conjuction with the answer in the thread you read – Raúl Juárez Aug 24 '13 at 14:41
  • @Wain This is what im actual trying to do... ill test somethink like this but im not sure if this is the best practice – Konstantin Heinrich Aug 24 '13 at 14:55
  • update my question, please see fiddle – Konstantin Heinrich Aug 24 '13 at 15:24
  • An array of dictionaries (with one dictionary) with a single key that contains another array ?? Go with the array of arrays. Or, better, Core Data and an FRC. – Wain Aug 24 '13 at 15:46
  • 1
    What you're calling a static cell, isn't static, since it will have a different name for each section, and so you still need to get its content from your data structure. To get the number of rows in each section, you're going to have to add the count of elements in SubItem and SubItem 2 Plus 1 for the title cell (the one you're calling static). Trying to use your data structure to populate a table is going to be convoluted, so I would probably parse that data first into an array of arrays to make it simpler. – rdelmar Aug 24 '13 at 16:03

3 Answers3

1

When you are using the term "static", are you really talking about a "uitableview static cell", or rather "cell with static content"? If the former; why do you need that? If the latter; how about testing in each section whether this is the first cell in the section, and then presenting static content? For all other cells, display dynamic content:

if (indexPath.row == 0) {
    // First row in section. Display static content
    ...
} else {
    // Display dynamic content
    ...
}

For what purpose do you need the static cells?

Marius Waldal
  • 9,537
  • 4
  • 30
  • 44
  • BTW, array with arrays is totally acceptable for your setup. You might want to add dummy-entries in the content-arrays to match up with the static first cells in each section. – Marius Waldal Aug 24 '13 at 15:04
  • Why do you want the item name in the first section row, and not in the section header? I imagine it like this: Your "Item" could be a music record. You know that the record has a name (item.name), but you don't know how many tracks (subitems) a record can have. So for your record collection, you want a section for each record, and an arbitrary number of rows for the tracks. Does this match your type of data? If so, use the section header instead of the "static cell", and then use ordinary dynamic cells for the subitems. – Marius Waldal Aug 24 '13 at 22:39
  • Youre right, it was just for testing! My section header become another description.. – Konstantin Heinrich Aug 24 '13 at 22:45
  • You can accomplish this by using one array for the section headers (the record names), and one array with arrays for the subitems (tracknames). – Marius Waldal Aug 24 '13 at 22:48
  • How is your supplied code giving a visual clue to separate subitem1 from subitem2 fields if they are all in the same section? – Marius Waldal Aug 24 '13 at 22:51
  • I dont want to seperate subitem 1 and subitem 2 visual, subitem2 has a little different between subitem1 and i need these seperation later on my server when the items order change.. – Konstantin Heinrich Aug 25 '13 at 07:26
1

So after a while and some testing and listen to your ideas and technicals i solved my problem!
Here is a little code :

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [mappedSprints count];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{

    UISprint*s = [mappedSprints objectAtIndex:section];
    return s._name;

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    UISprint* s = [mappedSprints objectAtIndex:section];
    return [s.internalUserStorey count] + [s.externalUserStorey count] + 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        return 130;
    }
    return 80;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

   UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[NSString stringWithFormat:@"Cell%d%d", indexPath.row, indexPath.section]];

            if (cell == nil) {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[NSString stringWithFormat:@"Cell%d%d", indexPath.row, indexPath.section]];
            }

    UISprint*s = [mappedSprints objectAtIndex:indexPath.section];


    if (indexPath.row == 0) {
        //Here is my first cell 
        //cell.textLabel.text =s._name;


    }
    else if(indexPath.row >= 0 && indexPath.row<=[s.internalUserStorey count]){

       //here are the cells for SubItem


    }

    else if(indexPath.row >= [s.internalUserStorey count]){


    //here are the cells for SubItem 2   

    }
    return cell;


So thanks to all !! Best Konstantin

-1

You can take one NSMuatableArray and add references on that i.e.

NSMuatableArray *arr=[NSMuatableArray alloc]init];

//section no 1 
[arr addObject:@"2"]; 

//section no 2 
[arr addObject:@"1"];

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

   switch(arr objectAtIndex[indexPath.row])
  {
  } 
}

you can use this way by using references you want set cells.

Kundan
  • 3,084
  • 2
  • 28
  • 65