替换NSMutableArray中的一个NSMutableDictionary对象,替换所有对象

时间:2013-07-26 07:41:24

标签: ios nsmutablearray nsmutabledictionary

我对iOS开发非常了解,我认为我跳得太快了。

我正在尝试使用NSMutableDictionary替换NSMutableArray字典中的replaceObjectAtIndex:withObject:但是当我重新加载我的tableView时,数组中的所有对象都被替换为我的我试图取代,而不仅仅是具体的指数。

提前感谢您的帮助。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath    *)indexPath
  {
     static NSString *CellIdentifier = @"Cell";
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

     if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
     }


     if ((self.editing && indexPath.row == [detailsArray count])) {

         cell.textLabel.text = @"Add Detail";

         return cell;
     }

     [cell.textLabel setText:[[detailsArray objectAtIndex:indexPath.row] objectForKey:@"name"]];
     [cell.detailTextLabel setText:[[detailsArray objectAtIndex:indexPath.row] objectForKey:@"desc"]];

     return cell;

}

-(void)setEditing:(BOOL)editing animated:(BOOL)animated{

    [super setEditing:editing animated:animated];
    [self.tableView setEditing:editing animated:animated];

    if (!editing) {

        [dictionaryList setValue:name.text forKey:@"name"];
        [dictionaryList setValue:description.text forKey:@"desc"];

        [detailsArray replaceObjectAtIndex:IndexHelper.row withObject:dictionaryList];

    }

     [self.tableView reloadData];


 }

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

//覆盖以支持编辑表格视图。

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{

    if (editingStyle == UITableViewCellEditingStyleDelete) {
       // Delete the row from the data source
       if ([detailsArray count] > 0) {

           [detailsArray removeObjectAtIndex:indexPath.row];

           [tableView beginUpdates];
           [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationFade];
           [tableView endUpdates];
       }
    }
    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

       [dictionaryList setValue:@"Title" forKey:@"name"];
       [dictionaryList setValue:@"Details" forKey:@"desc"];

       [detailsArray addObject:dictionaryList];

       [[self tableView] endUpdates];
       [[self tableView] insertRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationFade];
       [[self tableView] endUpdates];

     }

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{


IndexHelper = indexPath;

UIAlertView *passwordAlert = [[UIAlertView alloc] initWithTitle:@"" message:@"\n\n\n\n"
                                                       delegate:self cancelButtonTitle:@"Άκυρο" otherButtonTitles:@"Αποθήκευση", nil];

UIImageView *passwordImage = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"passwordfield" ofType:@"png"]]];

passwordImage.frame = CGRectMake(11,20,262,33);
[passwordAlert addSubview:passwordImage];

name = [[UITextField alloc] initWithFrame:CGRectMake(16,25,252,21)];
name.font = [UIFont systemFontOfSize:18];
name.backgroundColor = [UIColor whiteColor];
name.secureTextEntry = NO;
name.keyboardAppearance = UIKeyboardAppearanceAlert;
name.delegate = self;
name.placeholder = @"Title";
[name becomeFirstResponder];
[passwordAlert addSubview:name];


UIImageView *passwordImage2 = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"passwordfield" ofType:@"png"]]];

passwordImage2.frame = CGRectMake(11,65,262,33);
[passwordAlert addSubview:passwordImage2];

description = [[UITextField alloc] initWithFrame:CGRectMake(16,70,252,21)];
description.font = [UIFont systemFontOfSize:18];
description.backgroundColor = [UIColor whiteColor];
description.secureTextEntry = NO;
description.keyboardAppearance = UIKeyboardAppearanceAlert;
description.delegate = self;
description.placeholder = @"Details";
[description becomeFirstResponder];
[passwordAlert addSubview:description];


[passwordAlert show];


}

--- 这是我替换数组中字典的地方 ---

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{

    NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];


    if (buttonIndex==1) {

        if ([name.text isEqualToString:@""] && [description.text isEqualToString:@""]) {
            return;
        }

        [dictionaryList setValue:name.text forKey:@"name"];
        [dictionaryList setValue:description.text forKey:@"desc"];

        [detailsArray replaceObjectAtIndex:indexPath.row withObject:dictionaryList];

    }

    [self.tableView reloadData];

    NSLog(@"%@",detailsArray);

}

2 个答案:

答案 0 :(得分:0)

我认为你在所有索引中添加相同的字典,需要检查并为每个索引创建字典的多个实例。 它可能会帮助你

答案 1 :(得分:0)

我认为问题在于下面第一个代码

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{

    //NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow]; //REMOVE THIS LINE

    NSIndexPath *indexPath = IndexHelper; //USE THIS LINE

    if (buttonIndex==1) {

        if ([name.text isEqualToString:@""] && [description.text isEqualToString:@""]) {
            return;
        }

        [dictionaryList setValue:name.text forKey:@"name"];
        [dictionaryList setValue:description.text forKey:@"desc"];

        [detailsArray replaceObjectAtIndex:indexPath.row withObject:dictionaryList];

    }

    [self.tableView reloadData];

    NSLog(@"%@",detailsArray);

}