XCODE 5.0,保持表值Mutable

时间:2014-08-14 15:09:51

标签: ios xcode uitableview nsmutabledictionary rowdeleting

我会通过提供代码来更清楚。目标是从plist中获取信息。 (标题和副标题)并将其添加到表格中。以下代码适用于我,但不允许我删除任何行。我想找到一种方法来做到这一点。

#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

2 个答案:

答案 0 :(得分:0)

如果我理解你的问题,那么这应该可以解决问题。

NSDictionary *dictionary = someInfo;

NSMutableDictionary *mutableDictionary = [[NSMutableDictionary alloc] initWithDictionary:dictionary];

如果你只是创建它,你应该通过alloc init将字典设置为可变字典。

答案 1 :(得分:0)

尝试制作字典键的可变副本:

NSMutableArray *mutablearray = moreDetails.allKeys.mutableCopy