2012-08-26 87 views
0

由於某種原因,我的代碼拒絕工作。我在方法
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath,我嘗試從tableView和我的書籤數組中刪除對象的方法中出現sigbart錯誤? (你可以在我的代碼底部找到編譯指示標記 - 表視圖數據源的方法)。如何從NSMutableArray中刪除對象?

我是Objective-C和iPhone開發的新手,所以如果你知道什麼可能會導致這個錯誤,請嘗試用我沒有經歷的假設來解釋它。謝謝!

bookmarks.h

#import <UIKit/UIKit.h> 

#import "ShowTaskViewController.h" 

#import "Bookmark.h" 

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

} 

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

bookmarks.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])); 
} 
} 

有什麼想法?

+0

如果你能證明你有麻煩了你的代碼的一部分,這將是一個開端。很難通過一段代碼來找到你想要修改數組的地方。 – Abizern

+0

錯誤具體說明了什麼?並讓我們看看數組的初始化。 –

回答

4

[bookmarks removeObjectAtIndex:indexPath.row]; 

以前

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

編輯

我發現這條線的另一個問題行:

self.bookmarks = [storedBookmarks allValues]; 

這不保留數組的可變性。請更改它,就可以添加/刪除書籤對象:

self.bookmarks = [[storedBookmarks allValues] mutableCopy]; 
+0

+1擊敗了我。 – Abizern

+0

嗯,一些事情發生。但現在我得到另一個SIGBART,而不是在我的appdelegate.h文件中。看看我的新帖子 –

+0

#import #import「AppDelegate。H」 INT主(INT的argc,字符* argv的[]){ @autoreleasepool { 返回UIApplicationMain(的argc,argv的,零,NSStringFromClass([AppDelegate類]));} } 看到 –

相關問題