Question

dans UITableView, je souhaite définir pour la première nouvelle d’un flux RSS une tableViewCell personnalisée (type A), et pour les autres nouvelles deuxième, troisième, etc., une autre tableViewCell personnalisée (type B), le problème tableViewCell personnalisé (type A) créé pour la première nouvelle est réutilisé, mais curieusement, le nombre de lignes entre la première utilisation de customViewCell (type A) et la seconde apparence du même type de customViewCell n'est pas égal.

my cellForRowAtIndexPath ressemble à ceci.

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    int feedIndex = [indexPath indexAtPosition:[indexPath length] - 1];
    Feed *item = [[[[self selectedButton] category] feedsList] objectAtIndex:feedIndex + 1];
    static NSString *CellIdentifier = @"Cell";

    if(feedIndex == 0){
        MainArticleTableViewCell *cell = (MainArticleTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

        if (cell == nil)
        {
            cell = [[[MainArticleTableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
            [[[cell subviews] objectAtIndex:0] setTag:111];
        }

        cell.feed = item;

        return cell;

    }
    else{
        NewsTableViewCell *cell = (NewsTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

        if (cell == nil)
        {
            cell = [[[NewsTableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier orientation:currentOrientation] autorelease];
            [[[cell subviews] objectAtIndex:0] setTag:111];
        }

        cell.feed = item;

        return cell;

    }
    return nil;
}    

Les deux types de cellules ont des hauteurs différentes qui sont définies correctement. Quelqu'un pourrait-il m'indiquer comment faire en sorte que la cellule de type A personnalisée n'apparaisse que pour les premières nouvelles (non réutilisées)? merci

Était-ce utile?

La solution

Vous devez créer un identifiant de cellule différent pour les deux styles de cellule:

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

int feedIndex = [indexPath indexAtPosition:[indexPath length] - 1];
Feed *item = [[[[self selectedButton] category] feedsList] objectAtIndex:feedIndex + 1];

static NSString *CellIdentifier1 = @"Cell1";
static NSString *CellIdentifier2 = @"Cell2";

if(feedIndex == 0) {

   MainArticleTableViewCell *cell = (MainArticleTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier1];

   if (cell == nil) {
       cell = [[[MainArticleTableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier1] autorelease];
       [[[cell subviews] objectAtIndex:0] setTag:111];
   }

   cell.feed = item;

   return cell;
}
else {
   NewsTableViewCell *cell = (NewsTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier2];

   if (cell == nil) {
       cell = [[[NewsTableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier2 orientation:currentOrientation] autorelease];
       [[[cell subviews] objectAtIndex:0] setTag:111];
   }

   cell.feed = item;

   return cell;
}

return nil;
}

Autres conseils

Je ne comprends pas tout à fait votre question, mais j'ai remarqué deux choses curieuses. Si vous utilisez deux types de cellules différents, vous devez utiliser deux identificateurs de cellule distincts lorsque vous appelez 'dequeueReusableCellWithIdentifier'. Vous utilisez actuellement le même identifiant pour les deux, ce qui est incorrect. Essayez quelque chose comme:

static NSString *MainArticleIdentifier = @"MainArticle";
static NSString *NewsIdentifier = @"News";

En outre, je n'ai jamais rien vu de tel que:

int feedIndex = [indexPath indexAtPosition:[indexPath length] - 1];

Pourquoi ne pas simplement utiliser:

int feedIndex = indexPath.row;

dans cellForRowAtIndexPath

if ("Condition for cell 1") {
        cellV = ("customCell" *)[tableView dequeueReusableCellWithIdentifier:@"your ID cell in .xib"];

        if (cellV == nil) {
            [[NSBundle mainBundle] loadNibNamed:@"YOUR-CELL-FILENAME" owner:self options:nil];
            cellV = "OUTLET-CEll-IN-VC";
        }
    } else {
        cellV = ("customCell" *)[tableView dequeueReusableCellWithIdentifier:@"your ID cell2 in .xib"];

        if (cellV == nil) {
            [[NSBundle mainBundle] loadNibNamed:@"YOUR-CELL2-FILENAME" owner:self options:nil];
            cellV = "OUTLET-CEll-IN-VC";
        }
    }

[self configureCell:cellV indexpath:indexPath withClipVo:clip];

return cellV;
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top