简体   繁体   中英

populate section tableview with 2 arrays

I want to create app that can display location information in sectional table view (like as below).

[section 1]
[row 1 - location information];
[row 2 - location information];

[section 2]
[row 1 - location information];
[row 2 - location information];
[row 3 - location information];
 ...

My thought is to create 2 arrays. One is key array for section header. The other array is for table view row. In my app, the key array has 3 objects. The other array has 18 objects. ie there has 3 section headers, and display 6 objects/table view row in each section. How can I populate those informations into section table view correctly.?

The best solution is to create a dictionary, with two objects (which are the arrays). The key can be the name of the section.

Now, with one object (Dictionary) you have all the information you need.

in the numberOfSections just return [NSDictionary allKeys].count. in the cellForRow grab the object from the right array according to the indexPath. In the numberOfRows, just grab the correct array from the dictionary according to the indexPath and return the number of objects.

//setting number of sections
- (int)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section
{
    return [keyArray count];
}

//setting the title for the section
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    NSString *sectionName;
    sectionName = [keyArray objectAtIndex:section];
    return sectionName;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * MyIdentifier = @"MyIdentifier";
    UITableViewCell * cell = [self.tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] autorelease];
    }
    cell.textLabel.text = [otherArray objectAtIndex:indexPath.row];
    return cell;
}

Hope this helps you to start...

My suggestion will be use a dictionary. The key being the title of the section and the values being the title of the rows.

Its better to have a single array. You can format your array like this

[
    {
        "Title": "Title of section 1",
        "Rows": [
            {
                "Title": "Value 1",
                "SubTitle": "Value 2"
            },
            {
                "Title": "Value 1",
                "SubTitle": "Value 2"
            },
            {
                "Title": "Value 1",
                "SubTitle": "Value 2"
            }
        ]
    }
]

One main array which hold dictionaries of section. Section would have its own data plus array of dictionaries of rows.

If this structure is stored in an array.

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
   [self.array count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSDictionary *section = self.array[section];
    NSArray *rows = section[@"Rows"];
    retrun [rows count];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    NSDictionary *section = self.array[section];
    return section[@"Title"];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //Initialization of cell

    NSDictionary *section = self.array[indexPath.section];
    NSArray *rows = section[@"Rows"];

    NSDictionary *row = rows[indexPath.row];

    NSString *title = row[@"Title"];
    NSString *sutTitle = row[@"SubTitle"];

    //Assign them

    return cell;


}

If someone is still looking for this answer I got it with Swift

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

    let cell = tableView.dequeueReusableCellWithIdentifier("item", forIndexPath: indexPath)

    var item: Item!

    if indexPath.section == 0{
        item = array1[indexPath.row]
    }
    if indexPath.section == 1{
        item = array2[indexPath.row]
    }

    cell.textLabel?.text = item.name
    return cell
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
    if section == 0 {
        return array1.count
    }
    return array2.count
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM