2009-09-10 19 views
52

w moim UITableView Chcę ustawić dla pierwszych wiadomości RSS rss niestandardowy tableViewCell (typ A pozwala powiedzieć) i dla innych wiadomości drugi, trzeci itp .. inny niestandardowy tableViewCell (trype B) problemem jest to, że niestandardowy tableViewCell (trype A) stworzony dla pierwszych wiadomości jest ponownie wykorzystywany, ale z ciekawością liczba wierszy między pierwszym użyciem customViewCell (typ A) i drugim wyglądem tego samego typu customViewCell jest nie równy ..2 różne typy niestandardowych UITableViewCells w UITableView

my cellForRowAtIndexPath wygląda to tak.

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

dwa typy komórek mają różne wysokości, które są ustawione poprawnie. Czy ktoś mógłby wskazać mi właściwy kierunek, w jaki sposób ustawić niestandardową komórkę typu A, aby pojawiała się tylko w przypadku pierwszych wiadomości (które nie są ponownie wykorzystywane)? dziękuję

+0

dla Szybka wersja tego pytania zobacz [tutaj] (http://stackoverflow.com/ pytania/30774671/uitableview-with-more-than-one-custom-cell-with-swift) – Honey

Odpowiedz

78

Należy utworzyć inny identyfikator komórki do komórki dwóch stylach:

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

dziękuję, że zadziałało. czy wiesz, czy można ustawić różne niestandardowe komórki dla różnych sekcji tableView? sekcja 0 będzie miała typ A, a sekcja 1 będzie miała typ B. Dziękuję, –

+0

, znaleziono answear .. (NSIndexPath *) indexPath ma właściwość row również właściwość section, która była użyteczna :) Sorin –

+0

int feedIndex = indexPath.row; - jest wystarczająco dobry w pierwszej linii –

8

nie całkowicie rozumiem pytanie, ale zauważyłem dwa ciekawe rzeczy. Jeśli używasz dwóch różnych typów komórek, musisz użyć dwóch różnych identyfikatorów komórek podczas wywoływania "dequeueReusableCellWithIdentifier". Aktualnie używasz tego samego identyfikatora dla obu, co jest nieprawidłowe. Spróbuj czegoś takiego:

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

Ponadto, nigdy nie widziałem czegoś podobnego:

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

Dlaczego nie wystarczy użyć:

int feedIndex = indexPath.row; 
0

w 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; 
Powiązane problemy