代码之家  ›  专栏  ›  技术社区  ›  Sorin Antohi

uiTableView中2种不同类型的自定义uiTableViewCells

  •  52
  • Sorin Antohi  · 技术社区  · 15 年前

    在我的UITableView中,我想为RSS提要的第一条新闻设置一个自定义的TableViewCell(比如A型),为其他新闻设置第二条、第三条等。另一个自定义TableViewCell(色组B)问题是,为第一条新闻创建的自定义TableViewCell(色组A)被重用,但奇怪的是,第一次使用CustomViewCell(类型A)和同一类型CustomViewCell的第二个外观之间的行数不相等。

    我的牢房是这样的。

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

    这两种类型的单元格高度不同,设置正确。有人能给我指出正确的方向,如何使类型成为一个自定义单元格,只显示第一条新闻(不被重用)?谢谢您

    3 回复  |  直到 9 年前
        1
  •  78
  •   Philippe Leybaert    15 年前

    您应该为两种类型的单元格创建不同的单元格标识符:

    - (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
  •  8
  •   Chris Karcher    15 年前

    我不完全理解你的问题,但注意到了两件奇怪的事情。如果使用两种不同的单元格类型,则在调用“DequeueRusableCellWithIdentifier”时需要使用两个不同的单元格标识符。您当前对两者都使用相同的标识符,这是不正确的。尝试以下方法:

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

    而且,我从未见过这样的事情:

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

    为什么不直接使用:

    int feedIndex = indexPath.row;
    
        3
  •  0
  •   jose920405    9 年前

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