Reputation: 1248
First of I like to start with I am new to Objective C and this is my first time developing in it. For some reason I got stuck on how to delete objects from my NSArray
through my tableview
(with objects in it). Tryed some diffrent things but seems I am a bit stuck... What shall I pu in the code below?
bookmarks.m
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]withRowAnimation:YES];
[tableView reloadData];
}
Bookmarks.h
#import <UIKit/UIKit.h>
#import "ShowTaskViewController.h"
#import "Bookmark.h"
@interface BookmarksViewController : UITableViewController <UITableViewDelegate,UITableViewDataSource>
{
NSArray *bookmarks;
}
@property (nonatomic, retain) NSArray *bookmarks;
@end
Upvotes: 0
Views: 1967
Reputation: 40211
The tableView doesn't manage your content. You have to do that yourself. When the user taps delete on a row, you have to remove that item from your array and notifiy the table view to delete the cell (with an animation).
I recommend you change your data array into an NSMutableArray. Then you can do this:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
[bookmarks removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:YES];
}
Alternatively you could temporarily create an NSMutableArray.
NSMutableArray *mutableBookmarks = [NSMutableArray arrayWithArray:bookmarks];
[mutableBookmarks removeObjectAtIndex:indexPath.row];
self.bookmarks = [NSArray arrayWithArray:mutableBookmarks];
Upvotes: 1