0

出于某种原因,我的代码拒绝工作。我在方法中收到 sigbart 错误
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath,我尝试从 tableView 和我的书签数组中删除对象?(您可以在我的代码底部的 pragma 标记下找到该方法 - 表视图数据源)。

我是 Objective-C 和 iPhone 开发的新手,所以如果你知道什么可能导致这个错误,请尝试用我没有经验的假设来解释它。谢谢!

书签.h

#import <UIKit/UIKit.h>

#import "ShowTaskViewController.h"

#import "Bookmark.h"

@interface BookmarksViewController : UITableViewController  <UITableViewDelegate,UITableViewDataSource>
{
NSMutableArray *bookmarks;

}

@property (nonatomic, retain) NSMutableArray *bookmarks;
@end

书签.m

#import "BookmarksViewController.h"

@interface BookmarksViewController ()

@end

@implementation BookmarksViewController

@synthesize bookmarks;

- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
    // Custom initialization
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;

// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)viewWillAppear:(BOOL)animated
{
NSUserDefaults *storage = [NSUserDefaults standardUserDefaults];
NSDictionary *storedBookmarks = [NSKeyedUnarchiver unarchiveObjectWithData:[storage objectForKey:@"bookmarks"]];

if (storedBookmarks == nil)
{
    storedBookmarks = [[NSDictionary alloc] init];
}

self.bookmarks = [storedBookmarks allValues];

[self.tableView reloadData];    
[super viewWillAppear:animated];
}

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

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.bookmarks count];
}

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

if (cell == nil)
{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
Bookmark *item = [self.bookmarks objectAtIndex:indexPath.row];

NSArray *chunks = [item.name componentsSeparatedByString: @","];

NSString *name;
NSString *book;
NSString *chapter;

if ([chunks count] > 0)
{
    name = [chunks objectAtIndex:0];
    if ([chunks count] > 1)
    {
        book = [chunks objectAtIndex:1];
        if ([chunks count] > 2)
        {
            chapter = [chunks objectAtIndex:2];
        }
    }
}

cell.textLabel.text = name;
cell.detailTextLabel.text = book;

return cell;
}


- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"FromBookmarksToShowTaskSegue"])
{
    ShowTaskViewController *vc = [segue destinationViewController];

    Bookmark *item = [self.bookmarks objectAtIndex:[self.tableView indexPathForSelectedRow].row];

    vc.taskId =  item.id;
}
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}
*/


// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
    // Delete the row from the data source
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];

    [bookmarks removeObjectAtIndex:indexPath.row];

}   
else if (editingStyle == UITableViewCellEditingStyleInsert) {
    // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}   
}

* *编辑:在我改变位置之后

[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; [书签 removeObjectAtIndex:indexPath.row];

[bookmarks removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];

我在另一个文件中收到一个新的 SIGBART 错误,代码如下:

#import <UIKit/UIKit.h>
#import "AppDelegate.h"

int main(int argc, char *argv[])
{
@autoreleasepool {
    return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}
}

有什么想法吗 ?

4

1 回答 1

4

放线

[bookmarks removeObjectAtIndex:indexPath.row];

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

编辑

我发现这条线的另一个问题:

self.bookmarks = [storedBookmarks allValues];

这不会保留数组的可变性。请更改它,以便您可以添加/删除书签对象:

self.bookmarks = [[storedBookmarks allValues] mutableCopy];
于 2012-08-26T10:58:21.540 回答