Ios 内存warrning tableviewcell

Ios 内存warrning tableviewcell,ios,objective-c,memory-management,uitableview,Ios,Objective C,Memory Management,Uitableview,我在xib文件中有两个tableviewcell,我同时使用它们 当我使用其中一个时,内存仍然是7.3MB,但当我同时使用这两个时,内存增长非常快 请帮帮我。我现在不知道怎么了 我的代码如下: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ NSUInteger row = [indexPath row]; NSDict

我在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;
}

这不是一个好办法。您正在为每一行创建两个单元格,但只使用其中一个。您还应该使用更新的方法来使用基于xib的单元格——也就是说,在viewDidLoad中使用registerNB:forCellWithReuseIdentifier:来注册这两个NIB。首先检查奇偶行,并将正确的单元格出列(使用register方法时无需检查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;
    }
}
- (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;
}

这就是你需要做的一切。无需检查nil单元格,因为dequeueReusableCellWithIdentifier:forIndexPath:保证在注册nib或在故事板中创建单元格时返回有效单元格(在这种情况下,不需要注册任何内容)。

您只需要创建所需的单元格。您的代码应该如下所示:

- (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;
    }
}
- (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;
}
我改为:

- (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;
}

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

您是否检查了内存崩溃报告或设备日志,因为您的代码看起来很好,您正在使用cell做什么?您还剩下一些代码,我想我认为这段代码没有问题。内存占用何时增长?在滚动表格视图时?有两个理论:1-您确定您的
SampleTableCell
具有正确的可重用CellIdentifier吗?(在xib中检查)如果不是,则表示您创建的行太多。2-在创建两个单元格之前,应确定必须创建的单元格。不过,这只对第一个单元格的创建很重要。在使用了2个单元格之后,您是否在代码中启用了缩放功能?哈!然后,您的问题是强引用:属性(非原子,强)IBUILabel*labelDate;这些IBOutlet应该是弱的:属性(非原子,弱)IBOutlet UILabel*labelDate;你有什么例子吗?我应该在控制器的viewDidLoad中创建吗?我如何在CellForRowatindex中使用它创建两个单元格应该不是问题。当方法返回时,ARC应立即释放未使用的单元格。尽管在这种情况下,将self设置为nib的所有者是否会抵消ARC是一个有趣的问题。你能在loadNibNamed:?@Unmerciful中不使用owner:self就尝试一下吗?我已经更新了我的答案,告诉你怎么做。我为这个例子创建了一个类SampleCell和两个xib文件SampleCell1和SampleCell2。@TamásZahola,是的,你写的是真的——我应该在解释中更清楚一些。我不是有意暗示,制造两个细胞是导致记忆问题的原因。这不是好代码,所以不应该这样做。我不知道我的答案是否能解决这个问题,但它显示了苹果建议现在使用基于xib的单元格进行表视图的方式。