0

次のように UITableView にデータを入力している NSMutableDictionary を含む .plist ファイルがあります。

- (void)viewDidLoad
{
[super viewDidLoad];

NSString *myFile = [[NSBundle mainBundle]
                    pathForResource:@"courses" ofType:@"plist"];
courses = [[NSMutableDictionary alloc] initWithContentsOfFile:myFile];
courseKeys = [courses allKeys];

}

と:

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

// Configure the cell...

NSString *currentCourseName = [courseKeys objectAtIndex:[indexPath row]];
[[cell textLabel] setText:currentCourseName];

return cell;
}

UiTableView セルを使用して、ユーザーがスワイプ + 削除し、新しいアイテムを plist に「追加」できるようにしようとしています。これは私がこれを行うために使用しているコードです:

- (void)tableView:(UITableView *)tableView commitEditingStyle:  (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
    // Delete the row from the data source
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]     withRowAnimation:UITableViewRowAnimationFade];
}   
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

シミュレーターを実行してこれを行うと、次のエラーが表示されます

「無効な更新: セクション 0 の行数が無効です。更新後の既存のセクションに含まれる行数 (7) は、更新前にそのセクションに含まれる行数 (7) に等しい必要があります。プラスまたはマイナスそのセクションから挿入または削除された行数 (0 挿入、1 削除) と、そのセクションに移動された、またはそのセクションから移動された行数 (0 移動、0 移動) をプラスまたはマイナスします。

誰か助けてくれませんか?

4

2 に答える 2

2

courseKeys 配列からテーブルにデータを入力しています。したがって、デリゲートメソッドで

- (void)tableView:(UITableView *)tableView commitEditingStyle:  (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
 {
     //whether it is an add or edit or delete modify your array from which you are populating the table and reload the table
 }
于 2012-05-20T04:22:34.940 に答える
1

テーブルから行を削除していますが、データ ソースからは削除していません。CourseKeys は、削除されたキーを削除する mutableArray である必要があります。

于 2012-05-20T04:34:28.107 に答える