UITableview慢滚动并在一些滚动后增加ram

时间:2014-11-30 15:18:31

标签: ios objective-c xcode uitableview

这是我的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"hourCell" forIndexPath:indexPath];

// Configure the cell...


    cell.textLabel.text=hoursarray[indexPath.row];

    if ([cell.contentView subviews]){
        for (UIView *subview in [cell.contentView subviews]) {
            [subview removeFromSuperview];
        }
    }

    UIButton *buttonOff = [UIButton buttonWithType:UIButtonTypeCustom];

    buttonOff.layer.cornerRadius = 5.0f;
    UIButton *buttonT1 = [UIButton buttonWithType:UIButtonTypeCustom];
    buttonT1.layer.cornerRadius = 5.0f;
    UIButton *buttonT2 = [UIButton buttonWithType:UIButtonTypeCustom];
    buttonT2.layer.cornerRadius = 5.0f;      
    buttonOff.frame = CGRectMake(130, 5, 40, 34);
    [buttonOff setTitle:@"OFF" forState:UIControlStateNormal];
    [buttonOff addTarget:self action:@selector(onButtonOFFTap:) forControlEvents:UIControlEventTouchUpInside];
    buttonT1.frame = CGRectMake(190, 5, 40, 34);
    [buttonT1 setTitle:@"T1" forState:UIControlStateNormal];
    [buttonT1 addTarget:self action:@selector(onButtonT1Tap:) forControlEvents:UIControlEventTouchUpInside];
    buttonT2.frame = CGRectMake(250, 5, 40, 34);
    [buttonT2 setTitle:@"T2" forState:UIControlStateNormal];
    [buttonT2 addTarget:self action:@selector(onButtonT2Tap:) forControlEvents:UIControlEventTouchUpInside];

    [cell addSubview:buttonOff];
    [cell.contentView bringSubviewToFront:buttonOff];
    buttonOff.tag=indexPath.row;

    [cell addSubview:buttonT1];
    [cell.contentView bringSubviewToFront:buttonT1];
    buttonT1.tag=indexPath.row;

    [cell.contentView bringSubviewToFront:buttonT2];
    [cell addSubview:buttonT2];
    buttonT2.tag=indexPath.row;

    if([[NSString stringWithFormat:@"%@",[datiProgrTemp objectAtIndex:indexPath.row+48*day]] isEqual:@"1"])
    {
        buttonOff.backgroundColor = [UIColor greenColor];
        buttonT1.backgroundColor = [UIColor lightGrayColor];
        buttonT2.backgroundColor = [UIColor lightGrayColor];
    }
    if([[NSString stringWithFormat:@"%@",[datiProgrTemp objectAtIndex:indexPath.row+48*day]] isEqual:@"2"])
    {
        buttonOff.backgroundColor = [UIColor lightGrayColor];
        buttonT1.backgroundColor = [UIColor greenColor];
        buttonT2.backgroundColor = [UIColor lightGrayColor];
    }
    if([[NSString stringWithFormat:@"%@",[datiProgrTemp objectAtIndex:indexPath.row+48*day]] isEqual:@"3"])
    {
        buttonOff.backgroundColor = [UIColor lightGrayColor];
        buttonT1.backgroundColor = [UIColor lightGrayColor];
        buttonT2.backgroundColor = [UIColor greenColor];
    }
return cell;

}

事实是,在两个或三个滚动(快速和平滑)之后,滚动变得缓慢且蹩脚,并且使用的ram增加。 我试着把if(cell == nill)但我正在使用故事板而且单元格永远不会出现

我错了什么?

非常感谢, Ñ

2 个答案:

答案 0 :(得分:1)

这里有几个问题。

使用dequeueReusableCellWithIdentifier:的重点是重用您的单元其内容。但是我看到你使用过这一行:

if ([cell.contentView subviews]){
    for (UIView *subview in [cell.contentView subviews]) {
        [subview removeFromSuperview];
    }
}

每次调用cellForRowAtIndexPath:时删除单元格内容。这是浪费使用dequeueReusableCellWithIdentifier:

尽管如此,你的逻辑几乎正确。您可以删除子视图并创建新子视图,而不会出现您所描述的增量内存问题。问题是你犯了一个小错误:

您将UIButton直接添加到单元格的视图中,例如

[cell addSubview:buttonOff];

但删除了单元格的内容视图的子视图:

[cell.contentView subviews]

(出于同样的原因,你也无效地使用bringSubviewToFront:。)

由于这个错误,你实际上并没有按照你想要的那样在每次调用cellForRowAtIndexPath:时删除按钮,所以当你来回滚动时,按钮会被添加到另一个上面。另一个,因此增加了内存使用量。

要解决此问题,无论您是在cell还是cell.contentView添加和删除,都必须保持一致。

除此之外,由于您的单元格内容非常相似,我强烈建议您通过实际重用单元格内容的方式使用dequeueReusableCellWithIdentifier:

答案 1 :(得分:0)

感谢所有人。这是代码运作良好。我添加了一个TableViewCell类,它只包含按钮的插座(在故事板中以图形方式添加按钮):

    #import <UIKit/UIKit.h>

@interface CHRTableViewCell : UITableViewCell
@property (weak, nonatomic) IBOutlet UIButton *buttonOFF;
@property (weak, nonatomic) IBOutlet UIButton *buttonT1;
@property (weak, nonatomic) IBOutlet UIButton *buttonT2;

@end

然后在tableview控制器上实例化单元格并指向插座改变了按钮的方面:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
CHRTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"hourCell" forIndexPath:indexPath];

// Configure the cell...


    cell.textLabel.text=hoursarray[indexPath.row];


    cell.buttonOFF.layer.borderColor = [UIColor lightGrayColor].CGColor;
    cell.buttonOFF.layer.borderWidth = 0.5f;
    cell.buttonOFF.layer.cornerRadius = 5.0f;

    cell.buttonT1.layer.borderColor = [UIColor lightGrayColor].CGColor;
    cell.buttonT1.layer.borderWidth = 0.5f;
    cell.buttonT1.layer.cornerRadius = 5.0f;

    cell.buttonT2.layer.borderColor = [UIColor lightGrayColor].CGColor;
    cell.buttonT2.layer.borderWidth = 0.5f;
    cell.buttonT2.layer.cornerRadius = 5.0f;

    [cell.buttonOFF setTitle:@"OFF" forState:UIControlStateNormal];
    [cell.buttonOFF addTarget:self action:@selector(onButtonOFFTap:) forControlEvents:UIControlEventTouchUpInside];
    [cell.buttonT1 setTitle:@"T1" forState:UIControlStateNormal];
    [cell.buttonT1 addTarget:self action:@selector(onButtonT1Tap:) forControlEvents:UIControlEventTouchUpInside];
    [cell.buttonT2 setTitle:@"T2" forState:UIControlStateNormal];
    [cell.buttonT2 addTarget:self action:@selector(onButtonT2Tap:) forControlEvents:UIControlEventTouchUpInside];

    cell.buttonOFF.tag=indexPath.row;
    cell.buttonT1.tag=indexPath.row;
    cell.buttonT2.tag=indexPath.row;


    if([[NSString stringWithFormat:@"%@",[datiProgrTemp objectAtIndex:indexPath.row+48*day]] isEqual:@"1"])
    {

        cell.buttonOFF.backgroundColor = [UIColor colorWithRed:0.0f/255.0f green:200.0f/255.0f blue:60.0f/255.0f alpha:1.0f];
        cell.buttonT1.backgroundColor = [UIColor whiteColor];
        cell.buttonT2.backgroundColor = [UIColor whiteColor];

    }
    if([[NSString stringWithFormat:@"%@",[datiProgrTemp objectAtIndex:indexPath.row+48*day]] isEqual:@"2"])
    {
        cell.buttonOFF.backgroundColor = [UIColor whiteColor];
        cell.buttonT1.backgroundColor = [UIColor colorWithRed:0.0f/255.0f green:200.0f/255.0f blue:60.0f/255.0f alpha:1.0f];;
        cell.buttonT2.backgroundColor = [UIColor whiteColor];
    }
    if([[NSString stringWithFormat:@"%@",[datiProgrTemp objectAtIndex:indexPath.row+48*day]] isEqual:@"3"])
    {
        cell.buttonOFF.backgroundColor = [UIColor whiteColor];
        cell.buttonT1.backgroundColor = [UIColor whiteColor];
        cell.buttonT2.backgroundColor = [UIColor colorWithRed:0.0f/255.0f green:200.0f/255.0f blue:60.0f/255.0f alpha:1.0f];;
    }

return cell;

}