Objective C UITableView-表格单元格在更改单元格的高度后显示错误的内容 [英] Objective C UITableView - Table cells display wrong content after changing cell's height

查看:82
本文介绍了Objective C UITableView-表格单元格在更改单元格的高度后显示错误的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用xcode构建应用程序,该应用程序-与其他人一起-阅读rss供稿并显示帖子。我是Objective-c的新手,有时觉得有点难。



我将NSMutableArray用于检索到的故事(帖子)。每个故事都由NSMutableDictionary表示,其中包含帖子的标题,主题,日期和链接。所有这些都显示在UIViewController的UITableView中。
我已经自定义了自己的单元格,因此可以在其中显示多个标签。



我的问题是,如果我使用tableView:heightForRowAtIndexPath :,则前5个单元格(适合屏幕)显示正常,但是如果向下滚动,则下一个单元格似乎具有与前5个相同的内容(即单元格0-4显示确定,单元格5具有单元格0的内容,单元格1的单元格6等)!如果我删除tableView:heightForRowAtIndexPath:一切都很好(除非没有我想要的像元大小)



以下是代码的外观:

  // NavigationContentsViewController.h 
@interface NavigationContentsViewController:
UIViewController< UITableViewDelegate,UITableViewDataSource> {

UITableView * myTableView;
IBOutlet UITableView * newsTable;
UIActivityIndi​​catorView * activityIndi​​cator;
CGSize cellSize;
NSXMLParser * rssParser;
NSMutableArray *故事;
NSMutableDictionary *项目; //它从上到下解析文档...

NSString * currentElement;
NSMutableString * currentTitle,* currentDate,* currentSummary,* currentLink;

}

@property(nonatomic,retain)NSMutableArray * itemsList;
@property(nonatomic,retain)UITableView * myTableView;
-(void)parseXMLFileAtURL:(NSString *)URL;

  // NavigationContentsViewController.m 
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

//配置单元格。
静态NSString * MyIdentifier = @ MyIdentifier;
CustomCell * cell =(CustomCell *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];

if(cell == nil){
cell = [[[[CustomCell alloc] initWithFrame:CGRectZero复用标识符:MyIdentifier]自动释放];
//设置单元格
int storyIndex = indexPath.row;
// [单元格setText:[[stories objectAtIndex:storyIndex] objectForKey:@ title]]];
//故事标题
//cell.textLabel.text = [[stories objectAtIndex:storyIndex] objectForKey:@ title];
//cell.textLabel.font = [UIFont boldSystemFontOfSize:14];
cell.lTitle.text = [[stories objectAtIndex:storyIndex] objectForKey:@ title];
cell.lSummary.text = [[stories objectAtIndex:storyIndex] objectForKey:@ summary];
cell.lDate.text = [[stories objectAtIndex:storyIndex] objectForKey:@ date];

返回单元格;
}

返回单元格;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

NSString * selectedCellItem = [NSString stringWithFormat:@ %d,indexPath.row];

TableViewController * fvController = [[TableViewController alloc] initWithNibName:@ TableViewController捆绑包:[NSBundle mainBundle]];
fvController.selectedCellItem = selectedCellItem;
[self.navigationController pushViewController:fvController animation:YES];
[fvController版本];
fvController = nil;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 80;
}

有什么线索吗?

解决方案

这是人们经常遇到的表单元重用问题。 / p>

此行尝试重用单元格。这意味着如果单元格0移出屏幕,它将被重新用作单元格5:

  CustomCell * cell =(CustomCell *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier]; 

如果无法重复使用某个单元,则需要创建一个新单元:

  if(cell == nil){
cell = [[[[CustomCell alloc] initWithFrame:CGRectZero redirectIdentifier:MyIdentifier] autorelease];

下一行是您的问题,只有在单元格不能被重用。发生5次(对于表格可见时显示的单元格)。



但是您的表格之后要显示的所有单元格将被重复使用,这些单元格已经满足要求。

  //设置单元格
/*...*/






但不要担心。这很容易解决。您必须将单元的创建与其配置分开。只需像这样移位一些代码即可:

 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *) indexPath {
静态NSString * MyIdentifier = @ MyIdentifier;
CustomCell * cell =(CustomCell *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];

if(cell == nil){
cell = [[[[CustomCell alloc] initWithFrame:CGRectZero复用标识符:MyIdentifier]自动释放];
}
//之前发生的任何事情。您此时有一个有效的单元格。

//设置单元格
int storyIndex = indexPath.row;
// [单元格setText:[[stories objectAtIndex:storyIndex] objectForKey:@ title]]];
//故事标题
//cell.textLabel.text = [[stories objectAtIndex:storyIndex] objectForKey:@ title];
//cell.textLabel.font = [UIFont boldSystemFontOfSize:14];
cell.lTitle.text = [[stories objectAtIndex:storyIndex] objectForKey:@ title];
cell.lSummary.text = [[stories objectAtIndex:storyIndex] objectForKey:@ summary];
cell.lDate.text = [[stories objectAtIndex:storyIndex] objectForKey:@ date];
个返回单元格;
}






编辑:也许我应该下次再阅读问题。但我想我还是100%正确。


如果我删除tableView:heightForRowAtIndexPath:一切都很好(除非没有我想要的像元大小)


我认为这是巧合。你有多少细胞?我猜大概是7或8?一切都很好,因为您的所有单元格都可以同时看到。因此,无需重复使用单元格,它们都具有应有的内容。


I'm trying to build an application in xcode, which -beside others- reads an rss feed and displays the posts. I'm new with objective-c and find it a bit hard sometimes.

I use an NSMutableArray for the retrieved stories(posts). Each story is represented by an NSMutableDictionary which contains the title, subject, date and link of the post. All these are displayed in a UITableView within a UIViewController. I have customized my own cell, so i can display multiple labels in them.

My problem is that if i use tableView:heightForRowAtIndexPath:, first 5 cells (that fit to screen) display ok, but if you scroll down, next cells appear to have the same contents with first 5(that is cells 0-4 display ok, cell 5 has contents of cell 0, cell 6 of cell 1 etc)! If i remove the tableView:heightForRowAtIndexPath: everything is just fine (except not having the cell size i want)

Here's about how the code looks:

//  NavigationContentsViewController.h
@interface NavigationContentsViewController :
UIViewController <UITableViewDelegate, UITableViewDataSource> {

    UITableView *myTableView;
    IBOutlet UITableView * newsTable;
    UIActivityIndicatorView * activityIndicator;
    CGSize cellSize;
    NSXMLParser * rssParser;
    NSMutableArray * stories; 
    NSMutableDictionary * item; // it parses through the document, from top to bottom...

    NSString * currentElement;
    NSMutableString * currentTitle, * currentDate, * currentSummary, * currentLink;

}

@property(nonatomic,retain)NSMutableArray *itemsList;
@property(nonatomic,retain)UITableView *myTableView;
- (void)parseXMLFileAtURL: (NSString *)URL;

.

//NavigationContentsViewController.m
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

// Configure the cell.
static NSString *MyIdentifier = @"MyIdentifier";
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];

if (cell == nil){
    cell = [[[CustomCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
    // Set up the cell 
    int storyIndex = indexPath.row;
    //[cell setText:[[stories objectAtIndex: storyIndex] objectForKey: @"title"]];
    //Story title
    //cell.textLabel.text = [[stories objectAtIndex: storyIndex] objectForKey: @"title"];
    //cell.textLabel.font = [UIFont boldSystemFontOfSize:14];
    cell.lTitle.text = [[stories objectAtIndex: storyIndex] objectForKey: @"title"];
    cell.lSummary.text = [[stories objectAtIndex: storyIndex] objectForKey: @"summary"];
    cell.lDate.text = [[stories objectAtIndex: storyIndex] objectForKey: @"date"];

    return cell;
}

return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    NSString *selectedCellItem = [NSString stringWithFormat:@"%d", indexPath.row];

    TableViewController *fvController = [[TableViewController alloc] initWithNibName:@"TableViewController" bundle:[NSBundle mainBundle]];
    fvController.selectedCellItem = selectedCellItem;
    [self.navigationController pushViewController:fvController animated:YES];
    [fvController release];
    fvController = nil; 
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 80;
}

Any clues? [EDIT: changed int storyIndex = indexPath.row;]

解决方案

This is the usual problem people have with table cell reuse.

this line tries to reuse a cell. this means if cell 0 moves off screen it will be reused as cell 5:

CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];

if a cell could not be reused you are creating a new one:

if (cell == nil){
    cell = [[[CustomCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];

and on the next line is your problem, you set up the cell only if a cell couldn't be reused. Which happens 5 times (for the cells that are visible when the table becomes visible).

But all the cells that your table wants to display afterwards will be reused cells that have already content.

    // Set up the cell 
    /*...*/


but don't worry. this is very easy to fix. You have to separate the creation of your cell from its configuration. Just shift some code around like this:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *MyIdentifier = @"MyIdentifier";
    CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];

    if (cell == nil){
        cell = [[[CustomCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
    }
    // whatever happened before. You have a valid cell at this point.

    // Set up the cell 
    int storyIndex = indexPath.row;
    //[cell setText:[[stories objectAtIndex: storyIndex] objectForKey: @"title"]];
    //Story title
    //cell.textLabel.text = [[stories objectAtIndex: storyIndex] objectForKey: @"title"];
    //cell.textLabel.font = [UIFont boldSystemFontOfSize:14];
    cell.lTitle.text = [[stories objectAtIndex: storyIndex] objectForKey: @"title"];
    cell.lSummary.text = [[stories objectAtIndex: storyIndex] objectForKey: @"summary"];
    cell.lDate.text = [[stories objectAtIndex: storyIndex] objectForKey: @"date"];
    return cell;
}


EDIT: maybe I should read the question next time. But I guess I'm still 100% correct.

If i remove the tableView:heightForRowAtIndexPath: everything is just fine (except not having the cell size i want)

I think this is coincidence. How much cells do you have? I guess around 7 or 8? Everything is fine because all your cells are visible at the same time. So there is no need to reuse a cell, and they all have the content they should have.

这篇关于Objective C UITableView-表格单元格在更改单元格的高度后显示错误的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆