0
votes

For some reason my code refuses to work. I get a sigbart error in the method
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath, where I try to delete objects from the tableView and my bookmarks Array? (you find the method at the bottom of my code under the pragma mark - Table view data source).

I am new to Objective-C and iPhone development, so if you know what may cause this error, please try to explain it with the assumption I am not experienced. Thanx !

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

**EDIT: After I changed positions on

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

to

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

I get a new SIGBART error in another file with the following code:

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

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

Any thoughts ?

1
If you could just show the part of your code that you are having trouble with, it would be a start. It's hard to read through a wall of code to find where you are trying to modify the array.Abizern
What does the error say specifically? And lets see the initialization of the array.Mick MacCallum

1 Answers

4
votes

Put the line

[bookmarks removeObjectAtIndex:indexPath.row];

before

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

EDIT

I found another problem with this line:

self.bookmarks = [storedBookmarks allValues];

This does not retain mutability of the array. Please change it so you can add/remove bookmark objects:

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