2017-08-14 6 views
1

私は、選択されたindexPathを、次のようにmutabledictionary `selectedRowsInSectionDictionaryの中に保存しています。TableViewでのindexPathの比較

例えば次の辞書では、最初のセクションがキーです。そして、このセクションでは、第1(1,0)、第2(1,1)および第3(1,2)の行が選択され、辞書内に格納されています。

enter image description here

は、私はこれらの indexPathcellForRowAtIndexPathデリゲートメソッドで辞書内に格納されているかどうかをチェックしようとしていますが、それは常にfalseを返します。私は何が間違っているのだろうと思っていますか?

if([selectedRowsInSectionDictionary objectForKey:@(indexPath.section)] == indexPath) 
{ 
    cell.accessoryType = UITableViewCellAccessoryCheckmark; 
} 
+0

のisEqualを試してみてください:条件付きのご比較に==を置き換えるのメソッド

if([[selectedRowsInSectionDictionary objectForKey:@(indexPath.section)] containsObject:indexPath] { cell.accessoryType = UITableViewCellAccessoryCheckmark; } 

私は、次のテストコードと、これが確認されています。また、辞書から返されたオブジェクトが実際にはNSIndexPathオブジェクトであることを確認してください。 – Bamsworld

+0

[2つのNSIndexPathsを比較する方法](https://stackoverflow.com/questions/6379101/how-to-compare-two-nsindexpaths)の可能な複製 –

+0

@ShamasS、実際に私の質問はsligthly異なっています、私は配列を持っていますチェックするindexPathesの – hotspring

答えて

3

[selectedRowsInSectionDictionary objectForKey:@(indexPath.section)]NSMutableArray参照、ないindexPathので、比較は真ではありません。

NSMutableIndexSetを配列ではなく辞書に格納することをお勧めします。あなたが使用する「トグル」を使用して辞書に項目を追加/削除するには

NSMutableIndexSet *selectedSet = selectedRowsInSectionDictionary[@(indexPath.section)]; 
if ([selectedSet containsIndex:indexPath.row] { 
    cell.accessoryType = UITableViewCellAccessoryCheckmark; 
} else { 
    cell.accessoryType = UITableViewCellAccessoryNone; 
} 

:あなたのコードは、ようなものになるだろう

NSMutableIndexSet *selectedSet = selectedRowsInSectionDictionary[@(indexPath.section)]; 

if (selectedSet == nil) { 
    selectedSet = [NSMutableIndexSet new]; 
    selectedRowsInSectionDictionary[@(indexPath.section)] = selectedSet; 
} 

if ([selectedSet containsIndex:indexPath.row]) { 
    [selectedSet remove:indexPath.row]; 
} else { 
    [selectedSet add:indexPath.row]; 
} 
+0

これは 'NSMutableIndexSet'が' contains'メソッドを持つための目に見えるインターフェースがないという次のエラーを示しています。 – hotspring

+0

申し訳ありませんが、SwiftとObjective-Cのメソッド名には違いがあります。私はそれを更新しました – Paulw11

+0

あなたは関連する問題について何か考えがありますかhttps://stackoverflow.com/questions/48483622/reload-section-does-not-handle-properly – hotspring

2

辞書の値があるとしてこれが失敗していますアレイ。

は、私の知る限り

[selectedRowsInSectionDictionary objectForKey:@(indexPath.section)] 

を言うことができるように3つの要素(NSIndexPaths)を含む配列を返します。あなたは、次のようにコードを修正することができるはず :

NSIndexPath *comparisonIndexPath = [NSIndexPath indexPathForRow:2 inSection:0]; 
NSDictionary *test = @{ @(1): @[[NSIndexPath indexPathForRow:1 inSection:0], 
           comparisonIndexPath, 
           [NSIndexPath indexPathForRow:3 inSection:0]]}; 
NSArray *indexPathArray = [test objectForKey:@(1)]; 
if ([indexPathArray containsObject:comparisonIndexPath]) { 
    NSLog(@"Yeeehawww, let's do some stuff"); 
} 
関連する問題