2012-04-25 11 views
1

私はNSSetメソッドを使用して重複をチェックし、各名前の重複数を確認しています。テーブルビューに名前を重複して配置したいと思います。私は重複を見つけた後、100の名前を取って27のセルを返すが、重複の数を対応する名前のテーブルビューに配置することはできないようだ。ここで私はこれを動作させるために協力しようとしているスクリーンショットとメソッドです。NSCountedSet UITableViewで重複してカウントする

enter image description here

- (NSSet *)checkForDuplicateIDs { 

//CHECK FOR DUPLICATES IN ARRAY 
NSArray *allIDs = [tweetArray valueForKeyPath:@"sender_screen_name"]; 
NSArray *sortedIDs = [allIDs sortedArrayUsingSelector:@selector(compare:)]; 

NSString *previousID = nil; 
duplicateIDs = [NSMutableSet set]; 
for (NSString *anID in sortedIDs) { 
    if ([previousID isEqualToString:anID]) { 
     [duplicateIDs addObject:anID]; 

    } 
    previousID = anID; 
} 


//THEN REMOVE ANY DUPLICATES IN NEW ARRAY 
NSCountedSet *countedSet = [NSCountedSet setWithArray:sortedIDs]; 
NSMutableArray *oneMess = [NSMutableArray arrayWithCapacity:[sortedIDs count]]; 
NSMutableArray *multipleMess = [NSMutableArray arrayWithCapacity:[sortedIDs count]]; 


for(id obj in countedSet) { 
    if([countedSet countForObject:obj] == 1) { 
     [oneMess addObject:obj]; 
    } 
    for(id duplicatenames in countedSet) { 
     if ([countedSet countForObject:duplicatenames]>1) { 
      [multipleMess addObject:duplicatenames]; 
      [countedSet countForObject:duplicatenames]; 

     } 


    } 


} 


//other method.... much easier and cleaner but how do I put them into an array? 
NSCountedSet *set = [[NSCountedSet alloc] initWithArray:allIDs]; 

for (id item in set) 
{ 
    // NSLog(@"Name=%@, Count=%lu", item, (unsigned long)[set countForObject:item]); 
}  





// NSLog(@"NSCountedSet ALL Objects = %@",[countedSet allObjects]); 
// NSLog(@"NSCountedSet = %@",countedSet); 

[tweetArray removeAllObjects]; 
[tweetArray addObjectsFromArray:[countedSet allObjects]]; 
tweetArrayCount = [[NSMutableArray alloc] initWithObjects:countedSet, nil]; 


//NSLog(@"One Message = %@",oneMess); 
// NSLog(@"Multiple Messages = %@",multipleMess); 


//HERE I HAVE ORIGINAL ARRAY IN ALBETICAL ORDER 
// NSLog(@"Messages Containing more then 1 = %@",sortedIDs);  

//HERE I HAVE DUPLICATES IN ALBETICAL ORDER 
// NSLog(@"Messages Containing more then 1 = %@",duplicateIDs); 

//HERE I HAVE THE MESSAGES ONLY CONTAING 1 
//NSLog(@"Messages Containing only 1 = %@",oneMess); 


//NSLog(@"Duplicates count = %i",[duplicateIDs count]); 
// NSLog(@"Messages Containing only count = %i",[oneMess count]); 



[mainTableView reloadData]; 


return [[duplicateIDs copy] autorelease]; 

} 
+0

私は問題が何であるかを理解していない - 掲載コードのどれもあなたが得ることを試みた方法を示していませんテーブルに数えます。どうやってそれをしようとしているのですか?テーブルのどこに番号を入れたいですか?カウントされたセットメソッドは、確かに各オブジェクトのカウントとともに一意のセットを取得する方法です。 – rdelmar

答えて

2

私は私はあなたがしたいと思う何をすべきか国の名前が一覧表示されます簡単なのUITableViewの例を変更しました。このカウントはdetailTextLabel.textに格納されます。

countedList = [[NSCountedSet alloc] initWithArray:listOfItems];//listOfItems is an array of country name strings with duplicates 
    listOfItems2 = [NSMutableArray array]; // array without duplicates that's the data source for the UITableView 

    for (NSString *aCountry in countedList) { 
     NSMutableDictionary *dict = [NSMutableDictionary dictionary]; 
     [dict setValue:aCountry forKey:@"name"]; 
     [dict setValue:[NSString stringWithFormat:@"%lu",[countedList countForObject:aCountry]] forKey:@"count"]; 
     [listOfItems2 addObject:dict]; 
    } 

そして、私はテーブルのためのデータを提供するために、以下の方法を使用:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier]; // 
    } 

    // Set up the cell... 
    cell.textLabel.text = [[listOfItems2 objectAtIndex:indexPath.row]valueForKey:@"name"]; 
    cell.detailTextLabel.text = [[listOfItems2 objectAtIndex:indexPath.row]valueForKey:@"count"]; 
    return cell; 
} 
+0

ありがとう!これはまさに私が達成しようとしていたものです... valueForKeyを与えることによって、テーブルビューに文字列を置いて、そこに配列やセットを入れてクラッシュさせることができます。非常に高く評価! – FreeAppl3

関連する問題