遇到问题:prepareForReuse [英] Having problems with the method: prepareForReuse

查看:128
本文介绍了遇到问题:prepareForReuse的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义的UITableViewCell,当它被选中时,它会扩展并添加一个UILabel到我在storyBoard中添加的所选单元格UIView。



当我运行应用程序并选择一个单元格,标签将按预期添加到myView。问题是,当我向下滚动时,标签也会显示在另一个单元格上。



显然它的表现如此,是因为我正在重复使用单元格而且我不清理它们。我正在尝试调用prepareForReuse和'清理'单元格的方法,但是我很难做到这一点。这是我的代码:



I have a custom UITableViewCell, and when it's selected, it expands and adds a UILabel to the selected cells UIView that I added in the storyBoard.

When I run the app and select a cell, the label gets added to myView as expected. The problem is, when I scroll down, the label is also shown at another cell.

Apparently the reason its behaving like so, is because I'm reusing the cell and I don't clean them. I'm trying to call the method of prepareForReuse and 'cleaning' the cell, but I'm having trouble doing that. Here is my code:

- (void)prepareForReuse
{
    mainVC *c = [[mainVC alloc] init];
    if (c.info) {
        [c.info removeFromSuperview];
    }
}
- (void)viewDidLoad {
    self.sortedDictionary = [[NSArray alloc] initWithObjects:@"Californa", @"Alabama", @"Chicago", @"Texas", @"Colorado", @"New York", @"Philly", @"Utah", @"Nevadah", @"Oregon", @"Pensilvainia", @"South Dekoda", @"North Dekoda", @"Iowa", @"Misouri", @"New Mexico", @"Arizona", @"etc", nil];

    self.rowSelection = -1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CategorieCell *customCell = [tableView dequeueReusableCellWithIdentifier:@"cellID" forIndexPath:indexPath];
    customCell.title.text = [self.sortedDictionary objectAtIndex:indexPath.row];
    return customCell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    CategorieCell *customCell = (CategorieCell *)[tableView cellForRowAtIndexPath:indexPath];

    if (self.info) {
        [self.info removeFromSuperview];
    }

    self.info = [[UILabel alloc] init];
    [self.info setText:@"Hello"];
    [self.info setBackgroundColor:[UIColor brownColor]];

    CGRect labelFrame = CGRectMake(0, 0, 50, 100);
    [self.info setFrame:labelFrame];

    [customCell.infoView addSubview:self.info];

    NSLog(@"%ld", (long)indexPath.row);

    self.rowSelection = [indexPath row];
    [tableView beginUpdates];
    [tableView endUpdates];

}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([indexPath row] == self.rowSelection) {
        return 159;
    }
    return 59;
}

推荐答案

prepareForReuse仅用于清理内部资源,例如将图像设置为nil。例如,函数可以在你的CategoryCell中实现,而不是在View Controller中实现。



你在分配视图控制器和使用视频控制器时所做的事情是无关紧要和危险的。 (也说它在政治上一半)
prepareForReuse is only for cleaning up internal resources, like setting images to nil. For instance the function could be implemented in your CategoryCell AND REALLY NOT in the View Controller.

What you do in allocating a view controller and using it is nonsens and dangerous. (too say it halfways politly)


这篇关于遇到问题:prepareForReuse的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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