0

There are several questions similar to this one but I just cannot figure out how to deal with them.

I'm parsing an xml similar to this one;

title-a content-b category-cat1

title-c content-d category-cat2

and so on..

Then I save all the values I catch into the nsmutable arrays .. So I have nsmutable arrays of title, content and category.an object for example index of 10 all the values for that object in the arrays ( [title indexOfObject:10],[content indexOfObject:10],[category indexOfObject:10] )

After the parsing I get how many category I have with this loop;

mySections=[[NSMutableArray alloc] init];
    for (int i=0; i<[category count]; i++) {
        if (![mySections containsObject:[category objectAtIndex:i]]) {
            [mySections addObject:[category objectAtIndex:i]];
        }
    }
//--------------------


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return [mySections count];

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSInteger count=0;
    for (int i=0; i<[baslik count]; i++) {
        if ([[category objectAtIndex:i]isEqualToString:[category objectAtIndex:section]]) {
            count++;
        }
    }
    return count;
}

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

        return [mySections objectAtIndex:section];
}


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

//But in this function I cannot figure out how to access the right element 

[title objectAtIndex:indexPath.row]

}

I know maybe I'm doing it wrong but this is the first time im coding for section.Can someone help me how can I write it in a right way?

4

1 回答 1

0

A good approach would be to populate all your data into an NSDictionary with a format like

section1
  items
section2
  items

Then you can do:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return [yourDictionary count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSString *aSection = yourDictionary.allKeys[section];
    id theData = yourDictionary[aSection];
    return [theData count];
}

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

    return yourDictionary.allKeys[section];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *aSection = yourDictionary.allKeys[indexPath.section];
    id theData = yourDictionary[aSection][indexPath.row];
    ....
}
于 2013-11-13T13:46:22.057 回答