2012-03-05 46 views
0

我正在製作大書呆子牧場書,iphone編程。我通過CHAPT 11個工作,你實現自己的setEditing方法:setEditing方法導致SIGABRT

- (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 

回答

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

這使表視圖重載數據..和檢查其數據源的數據保護方法..

我相信這是崩潰的

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

對於新的索引路徑,這裏的球隊陣列中可能沒有對象...

通過breakpointing檢查這裏..

+0

對!那段代碼應該被刪除......謝謝! – 2012-03-05 19:58:44