how to display dictionary keys as sections and values as rows in a table view in ios?

user2523879

I have a NSMutableDictionary as below,

NSMutableDictionary *aSite;
NSArray *aDocs=[[NSArray alloc]initWithObjects:@"aaaa",@"bbbb",@"cccc", nil];
NSArray *bDocs=[[NSArray alloc]initWithObjects:@"aaaa",@"bbbb",@"cccc", nil];
NSArray *cDocs=[[NSArray alloc]initWithObjects:@"aaaa",@"bbbb",@"cccc", nil];
aSite=[[NSMutableDictionary alloc]init];
[aSite setObject:aDocs forKey:@"A library"];
[aSite setObject:bDocs forKey:@"B library"];
[aSite setObject:cDocs forKey:@"C library"];

Now,I have to display the keys as section and values as rows in the section,how to do that? please help

Matías R

In your UITableViewDataSource implementation:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return aSite.allKeys.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSString *arrayKey = [aSite.allKeys objectAtIndex:section];
    NSArray *a = [aSite objectForKey:arrayKey];

    return a.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];

    NSString *arrayKey = [aSite.allKeys objectAtIndex:indexPath.section];
    NSArray *a = [aSite objectForKey:arrayKey];
    cell.textLabel.text = [a objectAtIndex:indexPath.row];

    return cell;
}

Notice however that allKeys and allValues do not define a specific order (from the docs, "The order of the elements in the array is not defined"). I would convert your structure to an array of arrays before loading the data in the table view.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

In an iOS View, how can I display a list or table of an object's variable values, hiding rows where the variable's value is nil?

From Dev

How to add columns and rows of a dataframe as keys and values of a dictionary in python

From Dev

How to display and remove values in dictionary which holds duplicate keys

From Dev

IOS Number Of Sections In Table View Dynamic

From Dev

IOS Number Of Sections In Table View Dynamic

From Dev

How to display values in a dictionary?

From Dev

Display data from JSON in alphabetical sections in Table View in Swift

From Dev

Different sections in a view in iOS

From Dev

how to calculate the values in table view and to display in separate label

From Dev

Using different cell type in different sections of iOS table view

From Java

iOS 7 - How to display a date picker in place in a table view?

From Dev

How to display activity indicator in table view cell in ios7

From Dev

How to display unicoded dictionary values

From Dev

SQLite Create View to Display Table Columns in Rows

From Dev

Error applying dictionary to table view in IOS

From Dev

How to display the string values of a table refering to Two foreign ID keys on the main table

From Dev

How to display hash table's values by delivering a array consist of hash table's keys in powershell?

From Dev

Python writing a csv to dictionary with headers as keys and rows as values

From Dev

How to add the values of dictionary with same keys in Python

From Dev

How to convert Dictionary to List with mixing of values and keys?

From Dev

How to add values in keys in a dictionary inside a loop?

From Dev

How to sort keys in dictionary if values are same ?

From Dev

How to sum up values of duplicate keys in a dictionary?

From Dev

how to access values of nested dictionary keys in python?

From Dev

How to print dictionary keys and values in python

From Dev

How to populate dictionary values as list with the same keys

From Dev

Display columns headers values in rows with PHP table

From Dev

grouping rows into sections or groups IOS

From Dev

How to use different table view cell class for different UITableView sections

Related Related

  1. 1

    In an iOS View, how can I display a list or table of an object's variable values, hiding rows where the variable's value is nil?

  2. 2

    How to add columns and rows of a dataframe as keys and values of a dictionary in python

  3. 3

    How to display and remove values in dictionary which holds duplicate keys

  4. 4

    IOS Number Of Sections In Table View Dynamic

  5. 5

    IOS Number Of Sections In Table View Dynamic

  6. 6

    How to display values in a dictionary?

  7. 7

    Display data from JSON in alphabetical sections in Table View in Swift

  8. 8

    Different sections in a view in iOS

  9. 9

    how to calculate the values in table view and to display in separate label

  10. 10

    Using different cell type in different sections of iOS table view

  11. 11

    iOS 7 - How to display a date picker in place in a table view?

  12. 12

    How to display activity indicator in table view cell in ios7

  13. 13

    How to display unicoded dictionary values

  14. 14

    SQLite Create View to Display Table Columns in Rows

  15. 15

    Error applying dictionary to table view in IOS

  16. 16

    How to display the string values of a table refering to Two foreign ID keys on the main table

  17. 17

    How to display hash table's values by delivering a array consist of hash table's keys in powershell?

  18. 18

    Python writing a csv to dictionary with headers as keys and rows as values

  19. 19

    How to add the values of dictionary with same keys in Python

  20. 20

    How to convert Dictionary to List with mixing of values and keys?

  21. 21

    How to add values in keys in a dictionary inside a loop?

  22. 22

    How to sort keys in dictionary if values are same ?

  23. 23

    How to sum up values of duplicate keys in a dictionary?

  24. 24

    how to access values of nested dictionary keys in python?

  25. 25

    How to print dictionary keys and values in python

  26. 26

    How to populate dictionary values as list with the same keys

  27. 27

    Display columns headers values in rows with PHP table

  28. 28

    grouping rows into sections or groups IOS

  29. 29

    How to use different table view cell class for different UITableView sections

HotTag

Archive