iCulture forum | iPhone, iPad,  iPod touch, Apple TV en iOS

iCulture forum | iPhone, iPad, iPod touch, Apple TV en iOS (https://forum.iculture.nl/)
-   Ontwikkelen voor iOS (https://forum.iculture.nl/f133/development/f58/ontwikkelen-voor-ios/)
-   -   Tableview doet vreemd (https://forum.iculture.nl/f133/development/f58/ontwikkelen-voor-ios/97334-tableview-doet-vreemd.html)

Jeroen0704 13-08-11 20:53

Tableview doet vreemd
 
Hee,

Ik ben met een muziek app bezig, genaamd Play It. Nu heb ik een probleem met mijn tableview bij de albums. Het is de bedoeling dat als er meer als 1 nummer in het album zit je een shuffle optie krijgt in de eerste cell en anders niet. Deze cell is ook de enige met een accessoryview, namelijk een shuffle icon. De andere cellen bestaan uit de titel van het nummer en de artiest. Alleen hierbij gaat iets fout. Sommige cellen krijgen ook een shuffle icon en de eerste cell met de shuffle optie krijgt af en toe een artiest.

Zo ziet het er dan uit:

http://img849.imageshack.us/img849/2855/albumerror.png

Ik heb deze code gebruikt:

Code:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
   
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
       
        if (shuffleOption ) {
            if ([indexPath row] == 0 && [indexPath section] == 0) {
               
                cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
               
            } else {
                cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
            }
           
        } else {
           
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        }
       
    }
   
    cell.textLabel.textColor = [UIColor colorWithHue:((1.0/360)*120) saturation:((1.0/100)*2.94) brightness:((1.0/100)*13.33) alpha:1.0];
    cell.detailTextLabel.textColor = [UIColor colorWithHue:((1.0/360)*180) saturation:((1.0/100)*1.18) brightness:((1.0/100)*33.33) alpha:1.0];
       
        CGRect selectedBackgroundViewRect = CGRectMake(0, 0, 500, 80);
        UIImageView *backgroundImage = [[UIImageView alloc] initWithFrame:selectedBackgroundViewRect];
    backgroundImage.image = [[UIImage imageNamed:@"RedSelectedWithImage.png"] stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0];
        [backgroundImage setContentMode:UIViewContentModeScaleToFill];
        cell.selectedBackgroundView = backgroundImage;
    [backgroundImage release];
   
        cell.textLabel.font = [UIFont systemFontOfSize:20];
    cell.detailTextLabel.font = [UIFont systemFontOfSize:15];
   
    if (shuffleOption) {
        if ([indexPath row] == 0 && [indexPath section] == 0) {
           
            UIImageView *accessoryImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ShuffleButtonSelected.png"]];
            cell.accessoryView = accessoryImageView;
            [accessoryImageView release];
           
            cell.textLabel.text = [albumSongs objectAtIndex:[indexPath row]];
           
           
        } else {
            cell.detailTextLabel.text = [songArtist objectAtIndex:[indexPath row]-1];
            cell.textLabel.text = [albumSongs objectAtIndex:[indexPath row]];
        }
 
    } else {

        cell.textLabel.text = [albumSongs objectAtIndex:[indexPath row]];
        cell.detailTextLabel.text = [songArtist objectAtIndex:[indexPath row]];
    }
   
    return cell;
}


het lijkt alsof hij wat cellen door elkaar haalt.

Weet iemand hoe ik dit probleem kan verhelpen?

Groeten Jeroen

Hollance 13-08-11 21:30

Dat is niet zo raar want je hergebruikt cells van verschillende types door elkaar heen.

Je doet dequeueReusableCellWithIdentifier. Als die nil geeft maak je de nieuwe cell aan. To zover geen probleem. Maar als er genoeg cells zijn om te hergebruiken dan geeft dequeueReusableCellWithIdentifier je een bestaand cell object terug.

Normaalgesproken gebruik je verschillende identifiers voor verschillende typen cells, maar jij gebruikt verschillende typen cells door elkaar heen met dezelfde identifier. De ene keer zal dequeueReusableCellWithIdentifier dus een normale cell teruggeven, de andere keer een "subtitle" cell, en dan klopt het dus niet meer.

Gebruik dus verschillende identifiers voor verschillende cell types en dan werkt het wel goed.

Jeroen0704 13-08-11 23:13

Bedankt!

Nu werkt het goed, en heb ik weer wat bijgeleerd:)

JornZ 15-08-11 13:35

Een andere methode is om in else statements (geen shuffle) de eigenschappen van de cell die mogelijk veranderd zijn toen deze cell wel werd gebruikt voor de shuffleoptie te veranderen naar de standaardwaarden.

Je hoeft niet twee "pools" (zo noem ik het maar even) met type cellen te gebruiken, dit kan ongunstiger zijn in geheugengebruik.


Alle tijden zijn GMT +2. Het is nu 20:58.