【问题标题】:2 different types of custom UITableViewCells in UITableViewUITableView 中 2 种不同类型的自定义 UITableViewCells
【发布时间】:2010-11-27 04:50:17
【问题描述】:

在我的 UITableView 中,我想为 rss 提要的第一个新闻设置一个自定义 tableViewCell(可以说是 A 类),另一个新闻是第二个、第三个等。另一个自定义 tableViewCell(trype B)问题是为第一个新闻创建的自定义 tableViewCell(trype A) 被重复使用,但奇怪的是第一次使用 customViewCell(type 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 型自定义单元格仅出现在第一个新闻中(不被重用)?谢谢

【问题讨论】:

  • 有关此问题的 Swift 版本,请参阅 here

标签: iphone uitableview


【解决方案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;
}

【讨论】:

  • 谢谢它的工作。你知道是否可以为 tableView 的不同部分设置不同的 customcells 吗?第 0 节的类型为 A,第 1 节的类型为 B.. 谢谢
  • 找到了答案.. (NSIndexPath *)indexPath 具有 row 属性以及有用的 section 属性:) Sorin
  • int feedIndex = indexPath.row; - 在第一行就足够了
  • 很好的答案。读完这篇文章后我唯一需要知道的是:为单元注册 2 个笔尖是完全可以的。 [self.tableView registerNib:[UINib nibWithNibName:@"Cell1" bundle:nil] forCellReuseIdentifier:cellId1]; [self.tableView registerNib:[UINib nibWithNibName:@"Cell2" bundle:nil] forCellReuseIdentifier:cellId2]; 该解决方案适用于 Xcode 6 和 iOS 8 :)
  • 感谢您的帮助。节省我的时间。
【解决方案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;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-18
      相关资源
      最近更新 更多