只是一个快速的问题,真正做到:内存管理
我正在拉从SQLite数据库记录的方法到一个数组,然后分配该数组的内容,以一个实例变量。
@interface {
NSArray *items;
}
@implementation
// The population method.
-(void)populateInstanceVariable
{
NSMutableArray *itemsFromDatabase = [[NSMutableArray alloc] init];
// Sqlite code here, instantiating a model class, assigning values to the instance variables, and adding this to the itemsFromDatabase Array.
self.items = itemsFromDatabase;
[itemsFromDatabase release];
}
// viewDidLoad is calling the method above
-(void)viewDidLoad
{
[self populateInstanceVariable];
[super viewDidLoad];
}
// TableViewDataSource method - cellforIndexPath
- (UITableViewCell *)tableView:(UITableView *)passedInTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault];
// Load in my model from the instance variable - ***1
MyDataModel *model = [items objectAtIndexPath:indexPath.row];
// Assign the title to the cell from the model data
cell.textLabel.text = model.title;
// This is the part i'm stuck on, releasing here causes a crash!
[model release];
return cell;
}
@end
我的问题是双重的:
- 是我在做什么将数据分配给实例变量吗?我是否正确地管理内存?
- 如何管理tableview数据源中该模型项的内存?我似乎能够顺利运行的唯一方法是,如果我根本不释放
*model
对象,但是肯定会导致泄漏?
干杯。
谢谢,我实际上使用可重复使用的tableviewcells,我只是不能在编写问题时烦恼。 – 2011-02-22 22:15:27
初始分配给实例变量的任何想法?即在哪里做最好的地方,viewWillAppear,viewDidLoad?等等。 – 2011-02-22 22:16:25
除第二点外。拥有手段:你做了'保留','alloc/init','新'或'复制'。 – 2011-02-22 22:19:16