为什么我的应用程序崩溃并出现以下错误?

发布于 2024-11-08 16:12:47 字数 2597 浏览 0 评论 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];
}

When I scroll my table view up and down, after about 6-8 times my application crashes and I get the following in debug window:

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

Here is my code:

- (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];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

独木成林 2024-11-15 16:12:47

您不拥有 indexPath 变量,因此您需要保留它。

替换:

lastIndexPath = indexPath;

尝试用这个

lastIndexPath = [indexPath retain];

You don't own the indexPath variable, so you need to retain it.

Try replacing this:

lastIndexPath = indexPath;

With this:

lastIndexPath = [indexPath retain];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文