I have a NSSet method checking for duplicates and the count of duplicates of each name. I want to place the names in the table view with the number of duplicates with it. I have taken 100 names and return 27 cells after I find duplicates but I can not seem to get the number of duplicates to be placed in the table view with the corresponding names. Here is a screen shot and method I have been trying to work with to get this working.
- (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];
}
I modified a simple UITableView example that lists country names to do what I think you want to do. The count is put in the detailTextLabel.text.
And then I used the following method to supply the data for the table: