Question

J'ai un problème avec deux sections regroupées uitableview style. La première section ayant 6 rangées et seconde section ayant 3 rangées. Lorsque i défiler la tableview haut et en bas de la dernière ligne de contenu de l'article 2 est l'ajout dans la première section première rangée et la première rangée de contenu dans la première section est en ajoutant à la dernière rangée de la deuxième section. Je vérifie mon mieux si la cellule se vide pour charger le contenu, mais il ne se produit pas en cours de débogage. Mon code est,

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    static NSString *CellIdentifier = @"Cell";
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) 
        {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
            cell.backgroundColor = [UIColor whiteColor];

            if (indexPath.section == 0 && indexPath.row == 0) 
            {
                UILabel *Nameabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, 140, 30)];
                Nameabel.text = @"Name";
                Nameabel.font = [UIFont fontWithName:@"Helvetica" size:15];
                Nameabel.backgroundColor = [UIColor clearColor];
                Nameabel.font = [UIFont boldSystemFontOfSize:15];
                [cell.contentView addSubview:Nameabel];

                textField = [[UITextField alloc] initWithFrame:CGRectMake(145, 5, 150, 30)];
                textField.placeholder = @"Student Name";
                textField.borderStyle = UITextBorderStyleNone;
                textField.font = [UIFont fontWithName:@"Helvetica" size:14];
                [cell.contentView addSubview:paymentCatTextField];

            }
            else if (indexPath.section == 1 && indexPath.row == 0) 
            {
                UILabel *RLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, 140, 30)];
                RLabel.text = @"Class";
                RLabel.backgroundColor = [UIColor clearColor];
                RLabel.font = [UIFont fontWithName:@"Helvetica" size:15];
                RLabel.font = [UIFont boldSystemFontOfSize:15];
                [cell.contentView RLabel];

                RTextField = [[UITextField alloc] initWithFrame:CGRectMake(145, 5, 150, 30)];
                RTextField.placeholder = @"class Name";
                RTextField.borderStyle = UITextBorderStyleNone;
                RTextField.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
                RTextField.font = [UIFont fontWithName:@"Helvetica" size:14];
                [cell.contentView addSubview:RTextField];
            }
        }
       return cell;
    }

Ceci est pour l'échantillon. De cette manière la section 0 ont 6 rangées et une section avoir 3 rangées. Où que je fais mal. S'il vous plaît aider mon à résoudre ce problème.

Merci à l'avance.

Était-ce utile?

La solution

Changer votre code à la suivante et essayez à nouveau:

{
 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) 
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        cell.backgroundColor = [UIColor whiteColor];
    }


    if (indexPath.section == 0 && indexPath.row == 0) {
         //bla bla
    } else if (indexPath.section == 1 && indexPath.row == 0) {
         //bla bla
    }

    return cell;
}

La raison en est que vous définissez le contenu cellulaire uniquement lorsque vous n'êtes pas réutilisez la cellule. Ce qui ne va pas dans la plupart des cas.

Autres conseils

En plus de la réponse de dasdom Je suggère également la suppression des sous-vues avant d'ajouter les étiquettes.

if ([[cell.contentView subviews] count] > 0) {
    UIView *labelToClear = [[cell.contentView subviews] objectAtIndex:0];
    [labelToClear removeFromSuperview];
    UIView *textToClear = [[cell.contentView subviews] objectAtIndex:1];
    [textToClear removeFromSuperview];
}

et autoreleasing les sous-vues:

UILabel *Nameabel = [[[UILabel alloc] initWithFrame:CGRectMake(5, 5, 140, 30)] autorelease];
UITextField *textField = [[[UITextField alloc] initWithFrame:CGRectMake(145, 5, 150, 30)] autorelease];
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top