Подтвердить что ты не робот

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

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

my 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 появляться только для первых новостей (не повторяясь)? спасибо

4b9b3361

Ответ 1

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

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

Ответ 2

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

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

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

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

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

int feedIndex = indexPath.row;

Ответ 3

в 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;