如何从 NSMutableArray 中删除对象?
Posted
技术标签:
【中文标题】如何从 NSMutableArray 中删除对象?【英文标题】:How to delete objects from NSMutableArray? 【发布时间】:2012-08-26 10:36:20 【问题描述】:由于某种原因,我的代码拒绝工作。我在方法- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
中遇到 sigbart 错误,我尝试从 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]));
有什么想法吗?
【问题讨论】:
如果您可以只显示您遇到问题的代码部分,这将是一个开始。很难通过一堵代码来找到您要修改数组的位置。 错误具体说明了什么?让我们看看数组的初始化。 【参考方案1】:放线
[bookmarks removeObjectAtIndex:indexPath.row];
之前
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
编辑
我发现这一行还有一个问题:
self.bookmarks = [storedBookmarks allValues];
这不会保留数组的可变性。请更改它,以便您可以添加/删除书签对象:
self.bookmarks = [[storedBookmarks allValues] mutableCopy];
【讨论】:
嗯,发生了一些事情。但是现在我在我的 appdelegate.h 文件中获得了另一个 SIGBART。看看我的新帖 #import以上是关于如何从 NSMutableArray 中删除对象?的主要内容,如果未能解决你的问题,请参考以下文章
如何从匹配 NSNumber 的 NSMutableArray 中删除对象
我怎样才能从NSMutableArray中删除[NSNull Null]对象?