2 различных типа пользовательских ячеек UITableViewCells в UITableView

StackOverflow https://stackoverflow.com/questions/1405688

  •  05-07-2019
  •  | 
  •  

Вопрос

в моем UITableView я хочу установить для первой новости rss-канала пользовательскую ячейку tableViewCell (допустим, введите A), а для других новостей вторую, третью и т.д..другая пользовательская ячейка tableViewCell (тип B) проблема в том, что пользовательская ячейка tableViewCell (тип A), созданная для первой новости, используется повторно, но, что любопытно, количество строк между первым использованием customViewCell (тип A) и вторым появлением того же типа customViewCell не равно..

мой cellForRowAtIndexPath выглядит примерно так.

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

ячейки двух типов имеют разную высоту, которая установлена правильно.может ли кто-нибудь указать мне правильное направление относительно того, как сделать так, чтобы пользовательская ячейка типа A отображалась только для первой новости (не использовалась повторно)?Спасибо

Это было полезно?

Решение

Вы должны создать другой идентификатор ячейки для двух стилей ячейки:

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

Другие советы

Я не совсем понимаю ваш вопрос, но заметил две любопытные вещи.Если вы используете два разных типа ячеек, вам необходимо использовать два разных идентификатора ячеек при вызове 'dequeueReusableCellWithIdentifier'.В данный момент вы используете один и тот же идентификатор для обоих, что неверно.Попробуйте что-то вроде:

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

Кроме того, я никогда не видел ничего подобного:

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

Почему бы просто не использовать:

int feedIndex = indexPath.row;

в 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;
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top