简体   繁体   中英

Set text for custom UITableViewCell from array

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"
    );
}
)

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

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