导致SIGABRT的setEditing方法

时间:2012-03-05 18:36:00

标签: ios

我正在为大书呆子牧场书,iphone编程工作。我正在通过第11章工作,你实现了自己的setEditing方法:

- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    if( editing) {

        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[teams count] inSection:0];

        NSArray *paths = [NSArray arrayWithObject:indexPath];

        [[self tableView] insertRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationLeft];

    }
    else {
        /*
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[teams count] inSection:0];

        NSArray *paths = [NSArray arrayWithObject:indexPath];

        [[self tableView] deleteRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationFade];
         */
    }
}

当我运行这个时,整个应用程序会执行一个Sigabort而不会有任何其他信息。似乎导致问题的一行就是这一行:

[[self tableView] insertRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationLeft];

我不确定我做错了什么。还有什么好看的?

这是整个文件:

//
//  TeamsViewController.m
//  TeamTrackerClient
//
//  Created by Mark Steudel on 3/4/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "TeamsViewController.h"
#import "Team.h"

@implementation TeamsViewController

-(id) init
{
    self = [super initWithStyle:UITableViewStyleGrouped];

    teams = [[NSMutableArray alloc] init];

    Team *team = [[Team alloc] init];
    team.teamName = [NSString stringWithFormat: @"Fighting Axons"];
    team.teamCode = [NSString stringWithFormat: @"FA1"];

    [teams addObject:team];


    Team *team2 = [[Team alloc] init];
    team2.teamName = [NSString stringWithFormat: @"Pipers Peddlers"];
    team2.teamCode = [NSString stringWithFormat: @"PP1"];

    [teams addObject:team2];

    return self;
}

- (id) initWithStyle:(UITableViewStyle)style
{
    return [self init];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

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

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

#pragma mark - View lifecycle

/*
 // Implement loadView to create a view hierarchy programmatically, without using a nib.
 - (void)loadView
 {
 }
 */

/*
 // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
 - (void)viewDidLoad
 {
 [super viewDidLoad];
 }
 */

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


- (UIView *) headerView
{
    if( headerView)
        return headerView;

    UIButton *editButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    [editButton setTitle: @"Edit" forState: UIControlStateNormal];

    float w = [[UIScreen mainScreen] bounds].size.width;

    CGRect editButtonFrame = CGRectMake(8.0, 8.0, w - 16.0, 30.0);
    [editButton setFrame:editButtonFrame];

    [editButton addTarget:self 
                   action:@selector(editingButtonPressed:) 
         forControlEvents:UIControlEventTouchUpInside];

    CGRect headerViewFrame = CGRectMake(0, 0, w, 48);
    headerView = [[UIView alloc] initWithFrame:headerViewFrame];

    [headerView addSubview:editButton];

    return headerView;
}


- (void) editingButtonPressed: (id) sender
{
    if( [self isEditing] ) {
        [sender setTitle:@"Edit" forState:UIControlStateNormal];

        [self setEditing:NO animated:YES];
    }
    else {
        [sender setTitle: @"Done" forState:UIControlStateNormal];

        [self setEditing:YES animated:YES];
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return [self headerView];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return [[self headerView] frame].size.height;
}

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
    Team *t = [teams objectAtIndex:[sourceIndexPath row]];

    [teams removeObjectAtIndex:[sourceIndexPath row]];

    [teams insertObject:t  atIndex:[destinationIndexPath row]];

}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if( editingStyle == UITableViewCellEditingStyleDelete ) {
        [teams removeObjectAtIndex:[indexPath row]];

        [tableView deleteRowsAtIndexPaths: [NSArray arrayWithObject:indexPath] withRowAnimation: UITableViewRowAnimationFade];
    }
}

- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    if( editing) {

        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[teams count] inSection:0];

        NSArray *paths = [NSArray arrayWithObject:indexPath];

        [[self tableView] insertRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationLeft];

    }
    else {
        /*
         NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[teams count] inSection:0];

         NSArray *paths = [NSArray arrayWithObject:indexPath];

         [[self tableView] deleteRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationFade];
         */
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    int numberOfRows = [teams count];

    if( [self isEditing] )
        numberOfRows++;

    return numberOfRows;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];

    if( !cell ) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UITableViewCell"];
    }

    if( [indexPath row] < [teams count] ) {
        Team *t = [teams objectAtIndex:[indexPath row]];
        [[cell textLabel] setText:[t teamName]];
    }
    else {
        [[cell textLabel] setText: @"Add New Item ... "];
    }

    Team *t = [teams objectAtIndex:[indexPath row]];
    [[cell textLabel] setText:[t teamName]];

    return cell;
}
@end

1 个答案:

答案 0 :(得分:0)

    [[self tableView] insertRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationLeft];

这使表视图重新加载数据..并检查其数据源方法中的数据..

我相信它正在崩溃

Team *t = [teams objectAtIndex:[indexPath row]];
[[cell textLabel] setText:[t teamName]];

这里的团队数组中可能没有对象用于新的索引路径...

通过破坏点检查..