删除行i uitableview正在工作,但无法在iphone上始终反映
我可以成功地从tableview中删除数据,但当我转到主菜单并返回时,我会看到所有旧数据 如何使数据保持一致,即一旦删除,数据将永远消失删除行i uitableview正在工作,但无法在iphone上始终反映,iphone,Iphone,我可以成功地从tableview中删除数据,但当我转到主菜单并返回时,我会看到所有旧数据 如何使数据保持一致,即一旦删除,数据将永远消失 - (void)viewDidLoad { [super viewDidLoad]; appDelegate = (DatabaseTestAppDelegate *)[[UIApplication sharedApplication] delegate]; { appDelegate.favDetail = [[N
- (void)viewDidLoad
{
[super viewDidLoad];
appDelegate = (DatabaseTestAppDelegate *)[[UIApplication sharedApplication] delegate];
{
appDelegate.favDetail = [[NSMutableArray alloc] initWithObjects:@"1", @"2",@"4",nil];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
appDelegate = (DatabaseTestAppDelegate *)[[UIApplication sharedApplication] delegate];
NSString *cc=[appDelegate.favDetail objectAtIndex:indexPath.row];
NSLog(@" vale is %d",indexPath.row);
// Configure the cell...
NSArray *theParameters = [cc componentsSeparatedByString:@","];
NSLog(@"array is %@",cc);
NSLog(@"arraytheParameters is %@",theParameters);
cell.text=cc;//theParameters;
return cell;
}
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{ NSLog(@"matrix is her%@",appDelegate.favDetail);
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[appDelegate.favDetail removeObjectAtIndex: indexPath.row];
[tableView beginUpdates];
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject: indexPath] 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
} */
}
- (void)viewWillAppear:(BOOL)animated
{ [tableView reloadData];
[super viewWillAppear:animated]; // no use , it shows old data
}
谢谢您必须删除视图和模型中的数据如果您希望在表视图控制器的使用寿命之外保存模型的状态,您可以做的事情很少。一种是将数组写入documents目录中的文件,并在重新加载时将其读回。这将很好地应用于小数据集。但如果要使用大型数据集,则必须使用 对于这种特定情况,您正在重新初始化每个viewDidLoad上的数据。我建议您在viewDidLoad中执行此操作-
这将确保您不会重新初始化它。查看appDelegate.favDetail从中获取数据的位置 删除时,您正在从appDelegate.favDetail中删除,但当您转到主菜单并返回时,您正在将值重新分配给appDelegate.favDetail 签出appDelegate.favDetail只获取一次值。。或者另一个选项是从favDetail获取值的主源中删除数据
Happy iCoding…在viewDidLoad中设置一个断点,并检查在您第二次回来时是否有人呼叫它。我想你正在用旧值重新加载数组是的,它的旧数据,如何加载新数据?你能解释一下你的确切意思吗?还有一个问题,在导航栏中我有后退按钮和编辑按钮,现在把添加按钮放在哪里,这样用户也可以添加数据,您可以在结尾添加一个附加的按钮,标题为“添加”,或者您可以考虑向安装程序添加工具栏。然而,仅仅为了一个按钮而添加它可能不是最好的选择。。。但是获得投票是表达感谢的最好方式:P
...
if ( appDelegate.favDetail == nil ) {
appDelegate.favDetail = [[NSMutableArray alloc] initWithObjects:@"1", @"2",@"4",nil];
}
...