Ios 引起SIGABRT的SET编辑方法

Ios 引起SIGABRT的SET编辑方法,ios,Ios,我正在写一本书,叫《iphone编程》。我正在阅读第11章,在这里您实现了自己的设置编辑方法: - (void)setEditing:(BOOL)editing animated:(BOOL)animated { [super setEditing:editing animated:animated]; if( editing) { NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[teams coun

我正在写一本书,叫《iphone编程》。我正在阅读第11章,在这里您实现了自己的设置编辑方法:

- (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
//
//TeamsViewController.m
//TeamTrackerClient
//
//马克·斯图德尔于2012年3月4日创作。
//版权所有(c)2012年\uuuu MyCompanyName\uuuuu。版权所有。
//
#导入“TeamsViewController.h”
#导入“Team.h”
@实施团队VIEWCONTROLLER
-(id)init
{
self=[super initWithStyle:UITableViewStyleGrouped];
teams=[[NSMutableArray alloc]init];
团队*Team=[[Team alloc]init];
team.teamName=[NSString stringWithFormat:@“战斗轴突”];
team.teamCode=[NSString stringWithFormat:@“FA1”];
[团队添加对象:团队];
团队*team2=[[Team alloc]init];
team2.teamName=[NSString stringWithFormat:@“Pipers-Peddler”];
team2.teamCode=[NSString stringWithFormat:@“PP1”];
[团队添加对象:团队2];
回归自我;
}
-(id)initWithStyle:(UITableViewStyle)样式
{
返回[自初始化];
}
-(id)initWithNibName:(NSString*)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil
{
self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
如果(自我){
//自定义初始化
}
回归自我;
}
-(无效)未收到记忆警告
{
//如果视图没有superview,则释放该视图。
[超级记忆警告];
//释放所有未使用的缓存数据、图像等。
}
#pragma标记-视图生命周期
/*
//实现loadView以编程方式创建视图层次结构,而不使用nib。
-(void)负荷视图
{
}
*/
/*
//实现viewDidLoad以在加载视图(通常从nib)后执行附加设置。
-(无效)viewDidLoad
{
[超级视图下载];
}
*/
-(无效)视图卸载
{
[超级视频下载];
//释放主视图的所有保留子视图。
//例如,self.myOutlet=nil;
}
-(布尔)应自动旋转指针面定向:(UIInterfaceOrientation)interfaceOrientation
{
//对于支持的方向返回YES
返回(interfaceOrientation==UIInterfaceOrientationGraphic);
}
-(UIView*)头视图
{
if(headerView)
返回headerView;
UIButton*editButton=[UIButton Button类型为:UIButtonyPeroundRect];
[编辑按钮设置标题:@“编辑”状态:UIControlStateNormal];
浮动w=[[UIScreen mainScreen]边界].size.width;
CGRect editButtonFrame=CGRectMake(8.0,8.0,w-16.0,30.0);
[编辑按钮设置框架:编辑按钮框架];
[编辑按钮添加目标:自我
操作:@选择器(编辑按钮按下:)
forControlEvents:UIControlEventTouchUpInside];
CGRect headerViewFrame=CGRectMake(0,0,w,48);
headerView=[[UIView alloc]initWithFrame:headerViewFrame];
[headerView addSubview:editButton];
返回headerView;
}
-(无效)编辑按钮按下:(id)发件人
{
如果([自编辑]){
[发件人设置标题:@“编辑”状态:UIControlStateNormal];
[自设置编辑:无动画:是];
}
否则{
[发送方设置标题:@“完成”状态:uicontrol状态正常];
[自设置编辑:是动画:是];
}
}
-(UIView*)表格视图:(UITableView*)表格视图用于标题部分:(NSInteger)部分
{
返回[自头视图];
}
-(CGFloat)表格视图:(UITableView*)表格视图头部高度部分:(NSInteger)部分
{
返回[[self-headerView]帧].size.height;
}
-(void)tableView:(UITableView*)tableView MoveRowatineXpath:(NSIndexPath*)sourceIndexPath到IndeXpath:(NSIndexPath*)destinationIndexPath
{
Team*t=[teams objectAtIndex:[sourceindexath行]];
[团队移除ObjectAtIndex:[sourceIndexPath行]];
[teams insertObject:t atIndex:[DestinationIndepath行]];
}
-(void)tableView:(UITableView*)tableView提交的编辑样式:(UITableViewCellEditingStyle)行的编辑样式索引路径:(NSIndexPath*)索引路径
{
如果(editingStyle==UITableViewCellEditingStyleDelete){
[团队移除ObjectAtIndex:[indexPath行]];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]和RowAnimation:UITableViewRowAnimationFade];
}
}
-(void)设置编辑:(BOOL)编辑动画:(BOOL)动画
{
[超级设置编辑:编辑动画:动画];
如果(编辑){
NSIndexPath*indexPath=[NSIndexPath indexPathForRow:[Team count]第1节:0];
NSArray*路径=[NSArray arrayWithObject:indexPath];
[[self tableView]InsertRowsAndExpaths:带有RowAnimation的路径:UITableViewRowAnimationLeft];
}
否则{
/*
NSIndexPath*indexPath=[NSIndexPath indexPathForRow:[Team count]第1节:0];
NSArray*路径=[NSArray arrayWithObject:indexPath];
[[self tableView]删除RowsAndExpaths:带有RowAnimation:UITableViewRowAnimationFade]的路径;
*/
}
}
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节
{
int numberOfRows=[团队计数];
如果([自编辑])
numberOfRows++;
返回numberOfRows;
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:@“UITableViewCell”];
如果(!单元格){
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault重用标识符:@“UITableViewCell”];
}
如果([indexPath行]<[Team count]){
Team*t=[teams objectAtIndex:[indexPath行]];
[[cell textLabel]setText:[t teamName]];
}
否则{
[[cell textLabel]setText:@“添加新项…”;
}
Team*t=[teams objectAtIndex:[indexPath行]];
[[cell textLabel]setText:[t teamName]];
返回单元;
}
@结束
这将使表视图重新加载da
    [[self tableView] insertRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationLeft];
Team *t = [teams objectAtIndex:[indexPath row]];
[[cell textLabel] setText:[t teamName]];