2011-08-24 50 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"]];