内存warrning tableviewcell

时间:2013-11-28 07:37:34

标签: ios objective-c memory-management uitableview

我在xib文件中有两个tableviewcell,我使用它们。

当我使用其中一个时,内存仍然是7.3MB,但是当我使用这两个内存时,内存增长非常快。

请帮帮我。我现在不会出现什么问题。

我的代码如下:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    NSUInteger row = [indexPath row];
    NSDictionary *rowData = [self.events objectAtIndex:row];

    static NSString *CellIdentifierL = @"LeftCell";
    static NSString *CellIdentifierR = @"RightCell";


    SampleTableCell *cellLeft = [tableView dequeueReusableCellWithIdentifier:CellIdentifierL];
    SampleTableCell *cellRight = [tableView dequeueReusableCellWithIdentifier:CellIdentifierR];

    if (cellLeft == nil) {
        NSArray* topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"LeftTableCell" owner:self options:nil];
        for (id currentObject in topLevelObjects) {
            if ([currentObject isKindOfClass:[SampleTableCell class]]) {
                cellLeft = (SampleTableCell *)currentObject;
                break;
            }
        }
    }

    if (cellRight == nil) {
        NSArray* topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"RightTableCell" owner:self options:nil];
        for (id currentObject in topLevelObjects) {
            if ([currentObject isKindOfClass:[SampleTableCell class]]) {
                cellRight = (SampleTableCell *)currentObject;
                break;
            }
        }
    }



    return (row%2==0)?cellRight:cellLeft;
}

3 个答案:

答案 0 :(得分:0)

这不是一个很好的方法。您正在为每一行创建两个单元格,但您只使用其中一个单元格。您还应该使用更新的方法来使用基于xib的单元格 - 也就是说,在viewDidLoad中使用registerNib:forCellWithReuseIdentifier来注册两个nib。首先检查奇数/偶数行,然后将正确的单元格出列(使用寄存器方法时无需检查nil)。你这样做:

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.tableView registerNib:[UINib nibWithNibName:@"SampleCell1" bundle:nil] forCellReuseIdentifier:@"SampleCell1"];
    [self.tableView registerNib:[UINib nibWithNibName:@"SampleCell2" bundle:nil] forCellReuseIdentifier:@"SampleCell2"];

}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    if (indexPath.row % 2 == 0) {
        SampleCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SampleCell1" forIndexPath:indexPath];
        // populate cell with data
        return cell;
    }else{
        SampleCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SampleCell2" forIndexPath:indexPath];
        // populate cell with data
        return cell;
    }
}

这就是你需要做的。没有必要检查nil单元格,因为如果你注册一个nib,或者在一个storyboard中创建单元格(在这种情况下你不需要注册任何东西),dequeueReusableCellWithIdentifier:forIndexPath:保证返回一个有效的单元格。

答案 1 :(得分:0)

您只需要创建所需的单元格。你的代码应该是这样的:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    NSUInteger row = [indexPath row];
    NSDictionary *rowData = [self.events objectAtIndex:row];

    static NSString *CellIdentifierL = @"LeftCell";
    static NSString *CellIdentifierR = @"RightCell";

    if (cellLeft == nil) {
        SampleTableCell *cellLeft = [tableView dequeueReusableCellWithIdentifier:CellIdentifierL];
        NSArray* topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"LeftTableCell" owner:self options:nil];
        for (id currentObject in topLevelObjects) {
            if ([currentObject isKindOfClass:[SampleTableCell class]]) {
                cellLeft = (SampleTableCell *)currentObject;
                break;
            }
        }
        return cellLeft;
    }

    if (cellRight == nil) {
        SampleTableCell *cellRight = [tableView dequeueReusableCellWithIdentifier:CellIdentifierR];
        NSArray* topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"RightTableCell" owner:self options:nil];
        for (id currentObject in topLevelObjects) {
            if ([currentObject isKindOfClass:[SampleTableCell class]]) {
                cellRight = (SampleTableCell *)currentObject;
                break;
            }
        }
        return cellRight;
    }



    return nil;
}

答案 2 :(得分:0)

我改为:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    NSUInteger row = [indexPath row];
    NSDictionary *rowData = [self.events objectAtIndex:row];

    SampleTableCell *cell;


    if(row%2==0){

        cell = [[[NSBundle mainBundle] loadNibNamed:@"RightTableCell" owner:self options:nil]objectAtIndex:0];

    }else{
        cell = [[[NSBundle mainBundle] loadNibNamed:@"LeftTableCell" owner:self options:nil]objectAtIndex:0];
    }

    cell.labelName.text = [rowData objectForKey:@"name"];

    return cell;
}

但我不知道这是个好主意?