objective-c – Objective C UITableView – 更改单元格高度后,表格单元格显示错误的内容

objective-c – Objective C UITableView – 更改单元格高度后,表格单元格显示错误的内容,第1张

概述我正在尝试在 xcode中构建一个应用程序,其中包括其他人 – 读取RSS源并显示帖子.我是 Objective-c的新手,有时候发现它有点困难. 我使用NSMutableArray来检索故事(帖子).每个故事都由NSMutableDictionary表示,其中包含帖子的标题,主题,日期和链接.所有这些都显示在UIViewController中的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;    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?一切都很好,因为你的所有细胞都是同时可见的.因此,不需要重复使用单元格,它们都具有应具有的内容.

总结

以上是内存溢出为你收集整理的objective-c – Objective C UITableView – 更改单元格高度后,表格单元格显示错误的内容全部内容,希望文章能够帮你解决objective-c – Objective C UITableView – 更改单元格高度后,表格单元格显示错误的内容所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1033897.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-24
下一篇 2022-05-24

发表评论

登录后才能评论

评论列表(0条)

保存