Ios XCODE 5.0,保持表值可变
我将通过提供代码来更清楚地说明这一点。目标是从plist中获取信息。标题和副标题,并将其添加到表中。以下代码适用于我,但不允许我删除任何行。我正在想办法做到这一点Ios XCODE 5.0,保持表值可变,ios,xcode,uitableview,nsmutabledictionary,rowdeleting,Ios,Xcode,Uitableview,Nsmutabledictionary,Rowdeleting,我将通过提供代码来更清楚地说明这一点。目标是从plist中获取信息。标题和副标题,并将其添加到表中。以下代码适用于我,但不允许我删除任何行。我正在想办法做到这一点 #import "RWTViewController.h" #import "details.h" @interface RWTViewController () { NSMutableArray *titleSubject; NSMutableDictionary *subTitleContent; } @end
#import "RWTViewController.h"
#import "details.h"
@interface RWTViewController () {
NSMutableArray *titleSubject;
NSMutableDictionary *subTitleContent;
}
@end
@implementation RWTViewController
NSInteger counter;
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
counter = subTitleContent.count;
return counter;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath (NSIndexPath *) indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
// retreive image
UIImage *myImage = [UIImage imageNamed:@"unreadMessage"];
[cell.imageView setImage:myImage];
cell.textLabel.text = titleSubject[indexPath.row];
// This could possibly be changed?
cell.detailTextLabel.text = subTitleContent[titleSubject[indexPath.row]];
return cell;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle: (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
// Remove the row from data model
[titleSubject removeObjectAtIndex:indexPath.row];
// Request table view to reload
[tableView reloadData];
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSURL *urlWeb = [[NSBundle mainBundle] URLForResource:@"messageinfo" withExtension:@"plist"];
subTitleContent = [NSDictionary dictionaryWithContentsOfURL:urlWeb];
titleSubject = subTitleContent.allKeys;
//this tells me incompatible pointer assigning nsmutablearray from array.. not sure if there is another way to implement.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
如果我正确理解了你的问题,那么这应该可以解决问题
NSDictionary *dictionary = someInfo;
NSMutableDictionary *mutableDictionary = [[NSMutableDictionary alloc] initWithDictionary:dictionary];
如果只是创建字典,则应通过alloc init将其设置为可变字典。尝试创建字典键的可变副本:
NSMutableArray *mutablearray = moreDetails.allKeys.mutableCopy
allKeys返回一个NSArray而不是NSDictionary。mutableCopy部分仍然适用。@David我的错,我刚刚编辑了我的答案。谢谢你指出。现在还不清楚你在问什么,因为-[NSDictionary allKeys]返回的是一个NSArray而不是NSDictionary,但是你说你的字典变得不可变了。如果jbouaziz的答案不正确,即使是,你也应该澄清你的问题。