微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

删除行,但不能在iPhone上始终反映出来

如何解决删除行,但不能在iPhone上始终反映出来

| 我可以成功地从tableview中删除数据,但是当我进入主菜单并返回时,我会看到所有旧数据, 如何使数据一致,即数据一旦删除将永远消失
- (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目录中的文件,然后在重新加载时将其读回。这将适用于少量数据。但是,如果要使用大数据集,则必须使用
Core Data
。 对于这种特定情况,您将在每个“ 2”上重新初始化数据。我建议您在
viewDidLoad
中执行此操作–
...
if ( appDelegate.favDetail == nil ) {
    appDelegate.favDetail = [[NSMutableArray alloc] initWithObjects:@\"1\",@\"2\",@\"4\",nil];
}
...
这将确保您不重新初始化它。     ,您必须同时删除视图和模型中的数据     ,找出appDelegate.favDetail从中获取数据的地方... 删除时,您将从appDelegate.favDetail中删除,但是当您转到主菜单并回来时,您正在将值重新分配给appDelegate.favDetail...。 检出appDelegate.favDetail仅获取一次值..或另一种选择是您从favDetail获取值的主要来源中删除数据 快乐的iCoding ...     

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。