2011-08-24 126 views
1

我在向下滚动UITableView时遇到了iPhone应用崩溃的问题。我将NSZombieEnabled设置为YES,并发现我用来填充表格的NSArray以某种方式得到处理。NSArray早期发布

#import "RootViewController.h" 

@implementation RootViewController 
@synthesize flashsets; 

- (void)viewDidLoad 
{ 
     //Unrelated code removed from this post 
     NSString *listofsetsstring = [[NSString alloc] 
              initWithContentsOfFile:pathtosetsdata 
              encoding:NSUnicodeStringEncoding 
              error:&error]; 
     flashsets = [listofsetsstring componentsSeparatedByString:@"\n"]; 
     [super viewDidLoad]; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [flashsets count]; 
} 

// Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 

    } 

    // Configure the cell. 
    NSLog(@"IndexPath.row = %i", indexPath.row); 
    cell.textLabel.text = [flashsets objectAtIndex:indexPath.row]; <<<< CRASH HERE!! 

    return cell; 
} 

@end 

我在粗体行收到message sent to deallocated instance 0x4ebae20。在我的.h中,我使用了@property (nonatomic, retain) NSArray *flashsets;,我认为保留部分应该避免释放。

我该如何避免这样做?

回答

5

问题是:

flashsets = [listofsetsstring componentsSeparatedByString:@"\n"]; 

改变它

flashsets = [[listofsetsstring componentsSeparatedByString:@"\n"] retain]; 

编辑:在财产的保有,如果您使用的setter只使用,所以如果你使用它只会工作,以下行:

[self setFlashsets:[listofsetsstring componentsSeparatedByString:@"\n"]]; 
+0

谢谢!我将不得不记住将来使用[self variablename]。 – Cole

1

在你viewDidLoad它应该是self.flashsets =这将确保accessor方法我用于设置该值,因此您在属性定义中指定的“保留”行为将被执行。

0

我不知道这是否有帮助,但您可能想引用闪光灯时使用setter和getter方法 - 属性的保留部分不适用(我不认为)时设置直接变量。

1
flashsets = [listofsetsstring componentsSeparatedByString:@"\n"];//it returns autorealesed NsArray.So If you want Longer Use.you should get Owner ship from that array By Alloc or Copy Or Retain. 

flashsets = [[listofsetsstring componentsSeparatedByString:@"\n"] retain]; 
or 
flashsets = [[listofsetsstring componentsSeparatedByString:@"\n"]copy]; 
or 
flashsets = [[NsArry alloc ] initWithArray:[listofsetsstring componentsSeparatedByString:@"\n"]];