UITableView 中的 2 种不同类型的自定义 UITableViewCells [英] 2 different types of custom UITableViewCells in UITableView
问题描述
在我的 UITableView 中,我想为 rss 提要的第一个新闻设置一个自定义 tableViewCell(可以说是类型 A),并为其他新闻设置第二个、第三个等等.另一个自定义 tableViewCell(trype B)的问题是为第一个消息创建的custom tableViewCell(trype A)被重用了,但奇怪的是第一次使用customViewCell(type A)和第二次出现相同类型customViewCell的行数不相等..
in my UITableView i want to set for the first news of an rss feed a custom tableViewCell(Type A lets say) and for the other news second, third etc.. another custom tableViewCell(trype B) the problem is that the custom tableViewCell(trype A) created for the first news is reused, but curiously the number of rows between the first use of the customViewCell(type A) and the second appearance of the same type of customViewCell is not equal..
我的 cellForRowAtIndexPath 看起来像这样.
my cellForRowAtIndexPath it looks like this.
- (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 型自定义单元格仅出现在第一个新闻中(未重用)的正确方向吗?谢谢
the the two types of cells have different heights which is set correctly. could someone point me in the right direction on how to make the type A custom cell to appear only for the first news(not being reused)? thank you
推荐答案
您应该为两种样式的单元格创建不同的单元格标识符:
You should create a different cell identifier for the two styles of cell:
- (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;
}
这篇关于UITableView 中的 2 种不同类型的自定义 UITableViewCells的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!