【发布时间】: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