在我的UITableView中我想设置rss feed的第一个新闻自定义tableViewCell(类型A让我们说)和另一个新闻第二,第三等等。另一个自定义tableViewCell(trype B)的问题是,为第一个新闻创建的自定义tableViewCell(trype 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自定义单元格只出现在第一个新闻(不被重用)?谢谢
答案 0 :(得分:78)
您应该为两种样式的单元格创建不同的单元格标识符:
- (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;
}
答案 1 :(得分:8)
我不完全理解你的问题,但注意到两件奇怪的事情。如果您使用两种不同的单元格类型,则在调用“dequeueReusableCellWithIdentifier”时需要使用两个不同的单元格标识符。您当前正在为两者使用相同的标识符,这是不正确的。尝试类似:
static NSString *MainArticleIdentifier = @"MainArticle";
static NSString *NewsIdentifier = @"News";
另外,我从未见过像:
int feedIndex = [indexPath indexAtPosition:[indexPath length] - 1];
为什么不使用:
int feedIndex = indexPath.row;
答案 2 :(得分:0)
in 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;