从uitableview删除+写入plist不起作用

编程入门 行业动态 更新时间:2024-10-27 04:25:24
本文介绍了从uitableview删除+写入plist不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想从uitableview中删除并将其写入我的plist.对于这个Objective-C iOS编码,我还很陌生,所以请原谅我的错误

i want to delete from uitableview and make it write to my plist. i'm pretty new to this objective-c iOS coding, so forgive me for mistakes

现在使用我的代码,它崩溃了:

Right now with my code it crashed with this :

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (11) must be equal to the number of rows contained in that section before the update (11), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'

这是我的mainViewcontroller中的代码

Here's my code in my mainViewcontroller

- (void)viewWillAppear:(BOOL)animated{ // Property List.plist code //Gets paths from root direcory. NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES); //Get documents path. NSString *documentsPath = [paths objectAtIndex:0]; //Get the path to our PList file. plistPath = [documentsPath stringByAppendingPathComponent:@"Servers.plist"]; //Check to see if Property List.plist exists in documents. if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath]){ [[NSFileManager defaultManager]copyItemAtPath:[[NSBundle mainBundle]pathForResource:@"Servers" ofType:@"plist"] toPath:plistPath error:nil]; //If not in documents, get property list from main bundle. } arrayA = [[NSMutableArray alloc] initWithContentsOfFile:plistPath]; NSLog(@"%@\n%@", arrayA, [arrayA valueForKey:@"Hostname"]); tableData = [arrayA valueForKey:@"Hostname"]; [super viewWillAppear:animated]; [self.tableView reloadData]; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [tableData count]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *cellIdentifier = @"showServerAction"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; } cell.textLabel.text = [tableData objectAtIndex:indexPath.row]; return cell; } - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { if (editingStyle == UITableViewCellEditingStyleDelete) { [tableView beginUpdates]; [arrayA writeToFile:plistPath atomically: TRUE]; [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; [tableView endUpdates]; } }

更新 这是我的plist的样子.这是一个NSMutableArray

UPDATE here's how my plist looks. it's a NSMutableArray

推荐答案

尝试一下:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { if (editingStyle == UITableViewCellEditingStyleDelete) { [tableView beginUpdates]; // You are going to modify the table view, but you also need // to modify the data source of your table. Since tableData // is derived from the content of arrayA, the actual data // source is arrayA, so we start with that (also, arrayA is // a mutable array, so we are allowed to remove objects from // it). [arrayA removeObjectAtIndex:indexPath.row]; // Now we also need to modify tableData. If you don't do this, // tableData will report the wrong number of rows when // numberOfRowsInSection() is called the next time. This is the // direct source of the error that you mention in your question // (carefully read the error message, it's really meaningful!). // A small problem is that tableData is not a mutable array, // so we cannot use removeObjectAtIndex: to modify it (that's // probably the source of the error you got with Ilya's answer). // The solution is to simply recreate tableData from scratch // from the content of arrayA. tableData = [arrayA valueForKey:@"Hostname"]; [arrayA writeToFile:plistPath atomically: TRUE]; [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; [tableView endUpdates]; } }

更多推荐

从uitableview删除+写入plist不起作用

本文发布于:2023-07-16 07:23:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1119294.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不起作用   uitableview   plist

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!