【问题标题】:Function using to much memory使用大量内存的功能
【发布时间】:2011-09-05 15:03:21
【问题描述】:

我有一个由表格视图单元格中的按钮调用的函数,在调用该函数 15-20 次(单击按钮 15-20 次)后,我收到 2 级内存警告。如果没有这个功能,我似乎不会收到这个错误。

我已经阅读了为 iPhone 开发应用程序所附带的整个“内存责任”,但我的经验不足使我无法在我的功能上实现这一点,所以我希望那里的人可以看看它并看看是什么占用了所有内存。

函数(编辑:“(int)sender”->“(id)sender”)

- (void)addPoint:(id)sender {
if ([sender tag] == 0) {
    [playerOneScore replaceObjectAtIndex:holeNum withObject:[NSNumber numberWithInt:[[playerOneScore objectAtIndex:holeNum] intValue] + 1]];
} if ([sender tag] == 1) {
    [playerTwoScore replaceObjectAtIndex:holeNum withObject:[NSNumber numberWithInt:[[playerTwoScore objectAtIndex:holeNum] intValue] + 1]];
} if ([sender tag] == 2) {
    [playerThreeScore replaceObjectAtIndex:holeNum withObject:[NSNumber numberWithInt:[[playerThreeScore objectAtIndex:holeNum] intValue] + 1]];
} if ([sender tag] == 3) {
    [playerFourScore replaceObjectAtIndex:holeNum withObject:[NSNumber numberWithInt:[[playerFourScore objectAtIndex:holeNum] intValue] + 1]];
}
[tbl reloadData];
}

如果有任何关于如何从我的函数中释放一些内存的指示,我将不胜感激,该函数现在的行为方式毫无用处。

注意:我已经尝试过[button release],但这似乎只会造成错误,而且似乎对我的记忆状况没有帮助。

提前谢谢你,托拜厄斯·托维达尔

编辑: cellForRowAtIndex:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];


if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}

[cell.detailTextLabel setText:[NSString stringWithFormat:@"%@", [players objectAtIndex:indexPath.row]]];
[cell.textLabel setFont:[UIFont systemFontOfSize:50]];

UIButton *plusBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
plusBtn.frame = CGRectMake(255, 5, 60, 70);
[plusBtn setTitle:@"+" forState:UIControlStateNormal];
[plusBtn.titleLabel setFont:[UIFont systemFontOfSize:25]];
[plusBtn addTarget:self action:@selector(addPoint:) forControlEvents:UIControlEventTouchUpInside];

UIButton *minusBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
minusBtn.frame = CGRectMake(190, 5, 60, 70);
[minusBtn setTitle:@"-" forState:UIControlStateNormal];
[minusBtn.titleLabel setFont:[UIFont systemFontOfSize:25]];
[minusBtn addTarget:self action:@selector(removePoint:) forControlEvents:UIControlEventTouchUpInside];

plusBtn.tag = indexPath.row;
minusBtn.tag = indexPath.row;

if (indexPath.row == 0) {
    [cell.textLabel setText:[[playerOneScore objectAtIndex:holeNum] stringValue]];
} if (indexPath.row == 1) {
    [cell.textLabel setText:[[playerTwoScore objectAtIndex:holeNum] stringValue]];
} if (indexPath.row == 2) {
    [cell.textLabel setText:[[playerThreeScore objectAtIndex:holeNum] stringValue]];
} if (indexPath.row == 3) {
    [cell.textLabel setText:[[playerFourScore objectAtIndex:holeNum] stringValue]];
}

[cell.contentView addSubview:plusBtn];
[cell.contentView addSubview:minusBtn];

return cell;
}

【问题讨论】:

  • 为什么说是 (int)sender,而不是 (id)sender?然后你可以做 [sender tag]
  • 发布tableView:cellForRowAtIndexPath:的代码。我几乎可以肯定重新加载数据会导致泄漏。
  • @XenElement 你是对的,现在编辑。 (认为​​我之前因为某种原因有其他东西发送过 int)
  • @taskinoor 我已经添加了其余的代码。
  • 您是否尝试过通过 Instruments 运行它以查看什么类型的对象占用了这么多内存?

标签: iphone objective-c ios memory memory-management


【解决方案1】:

每次您访问cellForRowAtIndexPath: 中的表格单元格时,您都会添加minusBtnplusBtn 子视图。

您应该重新排列您的代码,以便这些子视图仅在创建单元格时添加到单元格中。因为它现在是新按钮,如果它正在被重用并且已经包含这些视图,也会添加。

【讨论】:

  • 你们太棒了,非常感谢你们解决了我的问题:)
【解决方案2】:

每次显示单元格时,您都在添加 plusBtn 和 minusBtn,而无需删除它们。 尝试移动

[cell.contentView addSubview:plusBtn];
[cell.contentView addSubview:minusBtn];

块内的代码:

if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}

编辑:克劳斯更快

【讨论】:

    【解决方案3】:

    每次调用有关单元格是否重用的方法时,您都将创建并添加为子视图plusBtnminusBtn。每次都添加为子视图而不删除前一个会导致泄漏。试试这个:

    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    
       UIButton *plusBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
       plusBtn.frame = CGRectMake(255, 5, 60, 70);
       [plusBtn setTitle:@"+" forState:UIControlStateNormal];
       [plusBtn.titleLabel setFont:[UIFont systemFontOfSize:25]];
       [plusBtn addTarget:self action:@selector(addPoint:) forControlEvents:UIControlEventTouchUpInside];
    
       [cell.contentView addSubview:plusBtn];
    
       // do the same for minusBtn here
    }
    

    【讨论】:

      猜你喜欢
      • 2015-06-15
      • 1970-01-01
      • 2014-04-02
      • 2016-12-31
      • 2012-01-13
      • 2023-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多