1

At the moment I fill my cells using this method:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    // builds the settingsView tableView
    if (menuList) {
        static NSString *cellIdentifier = @"coachingCell";

        UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        if (!contentLoaded) {
            numberOfCells = 0;
        }
        if (contentLoaded) {
            [cell.textLabel setText:[names objectAtIndex:numberOfCells]];
            NSLog(@"Entries: %@", [names objectAtIndex:numberOfCells]);
            if (numberOfCells == [names count]) {
                numberOfCells = 0;
            }
        numberOfCells ++;
        }
        //add a switch
        coachingSwitch = [[UISwitch alloc] initWithFrame:CGRectZero];
        cell.accessoryView = coachingSwitch;
        [coachingSwitch setOn:YES];

        return cell;
    }
    ...

But I get an error if I scroll all to the bottom and then one object to the top. That's because I fill the cells over a counter: numberOfCells.

Now my question: how can I fix this in order to actualy iterate over my array for the right indexPath.section and indexPath.row?

The names array contains:

2012-12-09 16:34:22.373 Test[18817:c07] Entries: Mein Bauchumfang
2012-12-09 16:34:22.375 Test[18817:c07] Entries: Mein Gewicht
2012-12-09 16:34:22.375 Test[18817:c07] Entries: Mein Entspannungsniveau
2012-12-09 16:34:22.376 Test[18817:c07] Entries: Häufigkeit der Entspannungsmomente
2012-12-09 16:34:22.377 Test[18817:c07] Entries: Rauchverhalten
2012-12-09 16:34:22.378 Test[18817:c07] Entries: Verlangen nach einer Zigarette
2012-12-09 16:34:22.378 Test[18817:c07] Entries: Systolischer Wert
2012-12-09 16:34:22.379 Test[18817:c07] Entries: Diastolischer Wert
2012-12-09 16:34:23.713 Test[18817:c07] Entries: Meine bewusste Ernährung mit Diabetes
2012-12-09 16:34:23.964 Test[18817:c07] Entries: Meine Blutzucker-Werte
2012-12-09 16:34:25.384 Test[18817:c07] Entries: Mein Befinden
2012-12-09 16:34:25.584 Test[18817:c07] Entries: Meine Aktivitäten

this is the multi dimensional array I could also use:

2012-12-09 17:23:06.891 stuff[19161:c07] ArrayList: (
    {
    "Bauchumfang & Gewicht" =         (
        "Mein Bauchumfang",
        "Mein Gewicht"
    );
    "Blutdruck & Puls" =         (
        "Systolischer Wert",
        "Diastolischer Wert"
    );
    "Blutzucker & Diabetes" =         (
        "Meine bewusste Ern\U00e4hrung mit Diabetes",
        "Meine Blutzucker-Werte"
    );
    "Mein Rauchverhalten" =         (
        Rauchverhalten,
        "Verlangen nach einer Zigarette"
    );
    "Meine Stimmung" =         (
        "Mein Befinden",
        "Meine Aktivit\U00e4ten"
    );
    "R\U00fccken & Bewegung" =         (
        "Umsetzung meiner R\U00fccken\U00fcbungen",
        "Das Befinden meines R\U00fcckens",
        "Meine sportlichen Aktivit\U00e4ten"
    );
    Schlafrhythmus =         (
        "Meine Schlafqualit\U00e4t",
        "Mein Energieniveau"
    );
    "Schrittz\U00e4hler" =         (
        "Meine Schritte"
    );
    "Stress & Entspannung" =         (
        "Mein Entspannungsniveau",
        "H\U00e4ufigkeit der Entspannungsmomente"
    );
}
)
2
  • Do you have a separate array for each section? Or one multi-dimensional array for all sections? Or one array that just lists everything without really being broken into sections in any way? Commented Dec 9, 2012 at 15:58
  • At the moment I have the NSMubableArray *names which contains every entrie (for all rows in all sections) but I also have the option to use another array which is multi-dimensional (it stores: sectionName {"rowName", "rowName", "rowName"} sectionName2{"rowName2",...} ...) Commented Dec 9, 2012 at 16:11

1 Answer 1

3

Why don't you just use indexPath.row instead of numberOfCells:

[cell.textLabel setText:[names objectAtIndex:indexPath.row]];

EDIT:

You could get the suitable array index using:

int rowsUntilHere=indexPath.row;
 for (int i=0;i<indexPath.section;i++){
   rowsUntilHere+=[tableView numberOfRowsInSection:i];
   //rowsUntilNow++; //If the array contains the section titles too
 }

 [cell.textLabel setText:[names objectAtIndex:rowsUntilHere]];

Hope this helps

Hoffe das hilft

Sign up to request clarification or add additional context in comments.

2 Comments

this isn't working! I know the common implementation. but indexPath.row gives me only the rows for the current section. so this is not working in a one dimensional array.
thank you so much, I'm dealing with that almoste a day! I used rowsUntilHere+=[settingsTable numberOfRowsInSection:i];in your example and everything works nice and smooth!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.