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

标签 objective-c xcode uitableview height cell

我正在尝试在 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 - 更改单元格高度后表格单元格显示错误内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5080943/

相关文章:

objective-c - 数字色度计逻辑 - 它是如何做到的?

ios - 如何按名称对 nsdocumentdirectory 中的文件进行排序?

objective-c - Obj-C 模块在工作区中不起作用

ios - 在uitableveiw中滑动,“删除”按钮不会出现。另外,当我快速滑动时,也会出现删除按钮

ios - 通过解析将时间戳添加到 MapView 注释?

iphone - 调整标签在 UITableViewCell 中的位置

iOS 应用无法运行 : unable to dequeue a cell with identifier Cell

swift - 点击 UICollectionViewCell 不会转换

iphone - 如何在 iOS 中获取应用内购买的区域设置货币价格?

swift - 自定义适用于 iOS 的 AWS Amplify 身份验证 UI