加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

objective-c – Objective C UITableView – 更改单元格高度后,

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读