1

NSMutableDictionary からの配列キーによって入力される UITableView があります。

これらの配列を削除できるようにするには、何らかの方法でキーを取得できる必要があります。私の考えは、行のラベルから取得するのが最も簡単でした。

このコードを使用して辞書をロードしています。

NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
// get documents path
NSString *documentsPath = [paths objectAtIndex:0];
// get the path to our Data/plist file
NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"Data.plist"];
NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithContentsOfFile:(NSString *)plistPath];

これにより、辞書から配列が削除されます。

[dictionary removeObjectForKey:];

しかし、プログラムでキーを取得する方法がわからないため、明らかにキーがありません。任意のヒント?

乾杯、

クリス

<plist version="1.0">
<dict>
    <key>(null)</key>
    <array>
        <string>Username</string>
        <string>Password</string>
        <string>http://www.google.com</string>
        <string>/whatever</string>
    </array>
    <key>Hello</key>
    <array>
        <string>admin</string>
        <string></string>
        <string>https://www.whatever.com</string>
        <string>/things</string>
    </array>
</dict>
</plist>

cellForRowAtIndexPath:

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

        UILabel *label = (UILabel *)[cell viewWithTag:1000];
        label.text = [viewerKeys objectAtIndex:indexPath.row];
        return cell;
}

コードを削除:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
    // get documents path
    NSString *documentsPath = [paths objectAtIndex:0];
    // get the path to our Data/plist file
    NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"Data.plist"];
    NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithContentsOfFile:(NSString *)plistPath];
    NSString *key = [viewerKeys objectAtIndex:indexPath.row];
    [dictionary removeObjectForKey:[NSString stringWithFormat:@"%@", key]];

    [self.tableView reloadData];

    NSArray *indexPaths = [NSArray arrayWithObject:indexPath];
    [tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationAutomatic];
}
4

1 に答える 1

1

表のセルからデータを取得しないでください。テーブル セルに入力するために使用したデータ構造が既にあり、同じデータ構造からデータを取得します。

各セルの適切なデータを取得できるように、何らかの配列を配置する必要があります。同じ配列を使用して、削除される行のデータを取得します。

メソッドのコードを表示するcellForRowAtIndexPath:と、より具体的な答えが得られる可能性があります。しかし、基本的には、に基づいてデータを取得するのと同じコードindexPathです。

これまでに投稿したコードは、辞書のみを示しています。ディクショナリは、テーブルのような行ベースの構造には適していません。

更新: のコードに基づいて、cellForRowAtIndexPath:必要なのは次のとおりです。

NSString *key = [viewerKeys objectAtIndex:indexPath.row];

更新 2commitEditingStyle :メソッドでファイルを再度読み込むのはなぜですか? テーブルに既にロードした既存のデータ構造を変更する必要があります。現状では、テーブルで使用されるデータ構造があります。次にcommitEditingStyle、データを再度ロードし、この一時データからエントリを削除してから、テーブルにリロードするように指示します。リロードでは、この一時データではなく、元のデータ構造が使用されます。

reloadDataまた、 と の両方を呼び出さないでくださいdeleteRowsAtIndexPaths:。どちらか一方に電話するだけです。

そして、この行:

[dictionary removeObjectForKey:[NSString stringWithFormat:@"%@", key]];

次のようにする必要があります。

[dictionary removeObjectForKey:key];

stringWithFormatフォーマットする文字列が実際にない限り、使用しないでください。

于 2013-02-14T17:03:02.953 に答える