我对 iOS 开发相当了解,我认为我跳入深渊的速度太快了。
我正在尝试通过使用替换字典中的NSMutableDictionary
一个,但是当我重新加载我的 tableView 时,数组中的所有对象都已被我要替换的对象替换,而不仅仅是特定的索引。NSMutableArray
replaceObjectAtIndex:withObject:
在此先感谢您的帮助。
- (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);
}