2011-05-22 44 views
0

当我滚动我的表视图上下,约经6-8次我的应用程序崩溃,我得到在调试窗口中:为什么我的应用程序崩溃时出现以下错误?

myapp[250:207] *** -[NSIndexPath row]: message sent to deallocated instance 0xdd0eab0 

这里是我的代码:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    // Return the number of rows in the section. 
    return [remoteRecipientItems count]; 
} 


// Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"RemoteRecipientItem"; 

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




    NSUInteger row = [indexPath row]; 

    NSUInteger oldRow = [lastIndexPath row]; 

    // Configure the cell... 

    [[cell textLabel]setText:[remoteRecipientItems objectAtIndex:[indexPath row]]]; 

    cell.accessoryType = (row == oldRow && lastIndexPath !=nil)? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone; 

    return cell; 
} 





#pragma mark - 
#pragma mark Table view delegate 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 



    int newRow = [indexPath row]; 
    int oldRow = (lastIndexPath !=nil)?[lastIndexPath row]:-1; 

    if (newRow != oldRow) { 
     UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath]; 
     newCell.accessoryType = UITableViewCellAccessoryCheckmark; 

     UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:lastIndexPath]; 

     oldCell.accessoryType = UITableViewCellAccessoryNone; 

     lastIndexPath = indexPath; 


    } 

    [tableView deselectRowAtIndexPath:indexPath animated:YES]; 


} 


#pragma mark - 
#pragma mark Memory management 

- (void)didReceiveMemoryWarning { 
    // Releases the view if it doesn't have a superview. 
    [super didReceiveMemoryWarning]; 

    // Relinquish ownership any cached data, images, etc. that aren't in use. 
} 

- (void)viewDidUnload { 
    // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand. 
    // For example: self.myOutlet = nil; 


    remoteRecipientItems = nil; 
    remoteRecipientID = nil; 
    xmlData = nil; 
    lastIndexPath = nil; 

} 


- (void)dealloc { 

    [remoteRecipientItems release]; 
    [remoteRecipientID release]; 
    [xmlData release]; 

    [lastIndexPath release]; 

    [super dealloc]; 
} 

回答

1

你不拥有indexPath变量,所以你需要保留它。

尝试更换此:

lastIndexPath = indexPath; 

有了这个:

lastIndexPath = [indexPath retain]; 
+0

,这样意味着我不得不释放了吗?我会在哪里发布它? – jini 2011-05-22 18:28:24

+0

@jini在'dealloc'中。 – 2011-05-22 18:48:03

+1

是不是在每个表格视图中都保留点击?在dealloc中,我只会释放一次?这不会混淆保留/释放计数吗? – jini 2011-05-22 18:53:49

相关问题