UITableView中的2种不同types的自定义UITableViewCells

在我的UITableView中,我想为rss feed的第一条消息设置一个自定义tableViewCell(typesA可以说)和其他消息第二,第三等。另一个自定义tableViewCell(trype B)问题是自定义tableViewCell trype A)创build的第一条新闻被重用,但奇怪的是,第一次使用customViewCell(type A)和第二次出现的同一types的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; } 

这两种types的细胞具有不同的高度,正确设置。 有人能指出我正确的方向如何使types的自定义单元格出现只有第一个消息(不被重用)? 谢谢

您应该为两种样式的单元格创build不同的单元格标识符:

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

我不完全理解你的问题,但注意到两件奇怪的事情。 如果您使用两种不同的单元格types,则在调用“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;