我使用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; 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; }
有线索吗? [编辑:更改了int storyIndex = indexPath.row;]
解决方法
该行尝试重用一个单元格.这意味着如果单元格0移出屏幕,它将被重用为单元格5:
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil){ cell = [[[CustomCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
在下一行是您的问题,只有在无法重复使用单元格时才设置单元格.这发生了5次(对于表变得可见时可见的单元格).
但是,您的桌子之后想要显示的所有单元格都将重新使用已经包含内容的单元格.
// Set up the cell /*...*/
但别担心这很容易解决.您必须将单元的创建与其配置分开.只需转移一些代码,如下所示:
- (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; }
编辑:也许我下次应该读这个问题.但我想我仍然100%正确.
If i remove the tableView:heightForRowAtIndexPath: everything is just fine (except not having the cell size i want)
我认为这是巧合.你有多少细胞?我想大约7或8?一切都很好,因为你的所有细胞都是同时可见的.因此,不需要重复使用单元格,它们都具有应具有的内容.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。