-1

indexPaths に依存せずに tableView の行をチェックしようとしています。これは私が以前に尋ねた質問に似ていますが、これは実際よりも簡単なはずです。

tableView のデータ ソースである静的な値の配列があり、それを fullArray と呼びます。行が選択されると、その値が別の配列に配置されます - それを partialArray と呼びましょう。indexPaths を使用してこれを行う前は、partialArray を次のように反復処理していました。

for(NSIndexPath * elem in [[SharedAppData sharedStore] selectedItemRows]) { 
    if ([indexPath compare:elem] == NSOrderedSame) { 
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }
}

魅力のように機能します。ただし、部分配列の値を使用してこれを実行しようとしていて、問題が発生しています。

sudo コードの cellForRowAtIndexPath メソッドでどのように機能するかを次に示します。

fullArray 内のすべての文字列について、partialArray 内にある場合は、その indexPath を取得して確認します。

私がまとめ始めたコード:

for(NSString *string in fullArray) {
    if (partialArray containsObject:string) {
//Need help here. Get the index of the string from full array
    fullArray indexOfObject:string];
//And check it.

        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }
}

それほど難しくないように思えますが、頭を包むことはできません。

4

1 に答える 1

0

インデックス パスの保存をやめた理由はわかりませんが、それはあなたの決断です。また、NSMutableSet配列の代わりに を使用して、チェックしたアイテムを格納することもできます。また、より適切な変数名は、たとえば、のcheckedItems代わりにpartialArray.

とにかく、要素をループしてfullArray各要素のインデックスを取得する必要がある場合は、2 つの方法のいずれかを使用できます。for1 つの方法は、ステートメントのように単純な古い C ループを使用することです。

for (int i = 0, l = fullArray.count; i < l; ++i) {
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (!cell)
        continue;
    NSString *item = [fullArray objectAtIndex:i];
    cell.accessoryType = [partialArray containsObject:item]
        ? UITableViewCellAccessoryCheckmark
        : UITableViewCellAccessoryNone;
    }
}

もう 1 つの方法は、次のenumerateObjectsWithBlock:メソッドを使用することです。

[fullArray enumerateObjectsUsingBlock:^(id item, NSUInteger index, BOOL *stop) {
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:0];
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (!cell)
        return;
    cell.accessoryType = [partialArray containsObject:item]
        ? UITableViewCellAccessoryCheckmark
        : UITableViewCellAccessoryNone;
}];
于 2012-11-12T20:03:20.897 に答える