首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >产生SIGABRT的setEditing方法

产生SIGABRT的setEditing方法
EN

Stack Overflow用户
提问于 2012-03-06 02:36:00
回答 1查看 318关注 0票数 0

我正在写一本大书呆子的书,iphone编程。我正在学习第11章,在这里你可以实现你自己的setEditing方法:

代码语言:javascript
复制
- (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。似乎导致问题的代码行是这样的:

代码语言:javascript
复制
[[self tableView] insertRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationLeft];

我不确定我做错了什么。还有什么值得一看的吗?

这是整个文件:

代码语言:javascript
复制
//
//  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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-03-06 02:43:13

代码语言:javascript
复制
    [[self tableView] insertRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationLeft];

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

我相信它正在崩溃

代码语言:javascript
复制
Team *t = [teams objectAtIndex:[indexPath row]];
[[cell textLabel] setText:[t teamName]];

新索引路径的teams数组中可能没有对象...

在此处通过断点检查..

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9571985

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档