Domanda

ho un problema con due sezioni in UITableView raggruppati stile. Prima sezione avente 6 righe e seconda sezione avente 3 righe. Quando ho scorrere tableView su e giù per l'ultima fila contenuto della sezione 2 è aggiunta nella prima sezione prima riga e la prima fila contenuto nella prima sezione è aggiunta nell'ultima riga della seconda sezione. Posso controllare il mio livello migliore se la cella diventare vuota per caricare il contenuto, ma, non sta accadendo durante il debug. Il mio codice è,

- (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;
    }

Questo è per il campione. In questo modo la sezione 0 avere 6 righe e la sezione 1 avere 3 righe. Dove sto facendo male. Si prega di aiutare il mio per risolvere questo problema.

Grazie in anticipo.

È stato utile?

Soluzione

modificare il codice al seguente e riprovare:

{
 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;
}

Il motivo è che si sta impostando il contenuto della cella solo quando non si sta riutilizzando la cellula. Questo è sbagliato nella maggior parte dei casi.

Altri suggerimenti

Oltre alla risposta di dasdom Vorrei anche suggerire di rimuovere le subviews prima di aggiungere le etichette.

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

e autoreleasing le subviews:

UILabel *Nameabel = [[[UILabel alloc] initWithFrame:CGRectMake(5, 5, 140, 30)] autorelease];
UITextField *textField = [[[UITextField alloc] initWithFrame:CGRectMake(145, 5, 150, 30)] autorelease];
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top