2011-02-22 162 views
0

只是一个快速的问题,真正做到:内存管理

我正在拉从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 

我的问题是双重的:

  1. 是我在做什么将数据分配给实例变量吗?我是否正确地管理内存?
  2. 如何管理tableview数据源中该模型项的内存?我似乎能够顺利运行的唯一方法是,如果我根本不释放*model对象,但是肯定会导致泄漏?

干杯。

回答

2

不,你不管理内存正确位置:

  • 你应该使用“可重复使用的” UITableViewCells,最UITableView的例子展示了如何做到这一点,

  • 不做[模型发布],你没有“自己”在这种情况下,对象,你刚刚提到它,所以你不能释放它

这里是典型的cellForRowAtIndexPath:

-(UITableViewCell *) tableView:(UITableView *)atableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *CellIdentifier = @"CellIdentifier"; 

    // Dequeue or create a cell of the appropriate type. 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) 
    { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease]; 
     // settings that do not change with every row 
     cell.selectionStyle = UITableViewCellSelectionStyleGray; 
    } 
    // settings that change with every row 
    cell.textLabel.text = @"fill in your label here"; 
    return cell; 
} 

此外,如果您使用的是DB为你的数据,你可能想寻找到核心数据,苹果的数据持久/管理框架,它包括挂钩数据实体方面直接向上的能力到UITableViews。

+0

谢谢,我实际上使用可重复使用的tableviewcells,我只是不能在编写问题时烦恼。 – 2011-02-22 22:15:27

+0

初始分配给实例变量的任何想法?即在哪里做最好的地方,viewWillAppear,viewDidLoad?等等。 – 2011-02-22 22:16:25

+0

除第二点外。拥有手段:你做了'保留','alloc/init','新'或'复制'。 – 2011-02-22 22:19:16

0

1)填充方法是正确的。不要忘记在dealloc中将实例变量设置为nil。 (我想你是在你使用'self'的时候添加了一个属性/合成。)。

2)不要释放模型对象。您没有保留,复制或分配该方法。另一方面,你的单元格初始化是错误的。使用以下内容:(更好的性能)

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *Identifier = @"CellIdentifier"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Identifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:Identifier] autorelease]; 
    } 

    //Other code 
} 
+0

是的,我使用可重复使用的表格单元,我只是从速度问题中省略了它们。 – 2011-02-22 22:18:18

+0

感谢您的帮助! :) – 2011-02-22 22:18:40

+0

没有问题,请自己清楚内存管理(释放模型对象失败),如果您不熟悉它,这是一个重要问题,稍后您将陷入陷阱。祝你好运。 – 2011-02-22 22:25:25