NSCountedSet 在 UITableView 中重复和计数
Posted
技术标签:
【中文标题】NSCountedSet 在 UITableView 中重复和计数【英文标题】:NSCountedSet Duplicates and count in UITableView 【发布时间】:2012-04-25 02:17:38 【问题描述】:我有一个NSSet
方法检查重复项和每个名称的重复项计数。我想将名称与重复数一起放在表格视图中。在找到重复项后,我取了 100 个名称并返回了 27 个单元格,但我似乎无法获得要放置在具有相应名称的表视图中的重复项的数量。这是我一直在尝试使用的屏幕截图和方法。
- (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];
【问题讨论】:
我不明白问题出在哪里——发布的代码都没有显示您如何尝试将计数放入表中。你是怎么做到的?您希望号码在表中的哪个位置? counted set 方法当然是获取唯一集合以及每个对象的计数的方法。 【参考方案1】:我修改了一个简单的 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;
【讨论】:
谢谢!这正是我想要完成的......通过提供 valueForKey 它允许在表视图中出现一个字符串,我一直在那里放置数组或集合并且当然会崩溃。非常感谢!以上是关于NSCountedSet 在 UITableView 中重复和计数的主要内容,如果未能解决你的问题,请参考以下文章