Iphone CellForRowAtIndexPath导致问题,我什么时候可以释放其中的本地文件?
我有一个UITableView,它显示一个名为“Transaction”的自定义对象中的各种NSString。在CellForRowAtIndexPath中,它分配一个事务的新实例,然后复制位于委托数组中的特定事务的值,以便我可以从不同的视图访问它。然后,它使用新事务“copy”及其属性对表单元格进行popluate。我遇到的问题是,如果在重新显示应用程序崩溃的表时释放复制的事务对象。如果我注释掉这个版本,程序运行得很好,但我显然担心内存管理。我的问题是我该怎么做,有没有其他地方可以发布这个 代码如下:Iphone CellForRowAtIndexPath导致问题,我什么时候可以释放其中的本地文件?,iphone,memory-management,memory-leaks,uitableview,Iphone,Memory Management,Memory Leaks,Uitableview,我有一个UITableView,它显示一个名为“Transaction”的自定义对象中的各种NSString。在CellForRowAtIndexPath中,它分配一个事务的新实例,然后复制位于委托数组中的特定事务的值,以便我可以从不同的视图访问它。然后,它使用新事务“copy”及其属性对表单元格进行popluate。我遇到的问题是,如果在重新显示应用程序崩溃的表时释放复制的事务对象。如果我注释掉这个版本,程序运行得很好,但我显然担心内存管理。我的问题是我该怎么做,有没有其他地方可以发布这个 代
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"TransCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Set up the cell...
NSUInteger row = [indexPath row];
CashAppDelegate *mainDelegate = [(CashAppDelegate *) [UIApplication sharedApplication] delegate];
Transaction *cellTrans = [[Transaction alloc] init];
cellTrans = [mainDelegate.transactionArray objectAtIndex:row];
NSString *string = [NSString stringWithFormat:@"$%1.2f %@ | %@", cellTrans.amount, cellTrans.category, cellTrans.descriptionString];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.text = string;
//[cellTrans release];
return cell;
}
你想要:
Transaction *cellTrans = [mainDelegate.transactionArray objectAtIndex:row];
而不是:
Transaction *cellTrans = [[Transaction alloc] init];
cellTrans = [mainDelegate.transactionArray objectAtIndex:row];
Transaction *cellTrans = [[Transaction alloc] init];
cellTrans = [mainDelegate.transactionArray objectAtIndex:row];
那么你就不需要释放了。问题是cellTrans是一个指针。您所做的是创建一个新对象,指向它,然后指向其他对象,忽略您刚才创建的对象。然后,尝试删除数组中的对象,而不是刚刚创建的对象。您需要:
Transaction *cellTrans = [mainDelegate.transactionArray objectAtIndex:row];
而不是:
Transaction *cellTrans = [[Transaction alloc] init];
cellTrans = [mainDelegate.transactionArray objectAtIndex:row];
Transaction *cellTrans = [[Transaction alloc] init];
cellTrans = [mainDelegate.transactionArray objectAtIndex:row];
那么你就不需要释放了。问题是cellTrans是一个指针。您所做的是创建一个新对象,指向它,然后指向其他对象,忽略您刚才创建的对象。然后,您尝试删除数组中的对象,而不是刚刚创建的对象。您实际上不需要分配新的事务对象,因为您只希望/需要对代理事务数组中已存在的对象的引用。因此,不是:
Transaction *cellTrans = [[Transaction alloc] init];
cellTrans = [mainDelegate.transactionArray objectAtIndex:row];
Transaction *cellTrans = [[Transaction alloc] init];
cellTrans = [mainDelegate.transactionArray objectAtIndex:row];
整理
[cellTrans release];
只需使用这一行获取参考:
Transaction *cellTrans = (Transaction *)[mainDelegate.transactionArray objectAtIndex:row];
实际上,您不需要分配新的事务对象,因为您只需要/需要对委托事务数组中已经存在的事务对象的引用。因此,不是:
Transaction *cellTrans = [[Transaction alloc] init];
cellTrans = [mainDelegate.transactionArray objectAtIndex:row];
Transaction *cellTrans = [[Transaction alloc] init];
cellTrans = [mainDelegate.transactionArray objectAtIndex:row];
整理
[cellTrans release];
只需使用这一行获取参考:
Transaction *cellTrans = (Transaction *)[mainDelegate.transactionArray objectAtIndex:row];