【发布时间】:2016-08-09 01:10:22
【问题描述】:
我有两张表,一张是用户名表,一张是分数表。我用两个数组填充这些表。表格需要根据分数降序排列。我用其中的分数对数组进行排序,但我不确定如何安排用户名以保持他们的分数,它们在一个单独的数组中,并且在分数表中是一个单独的表。这是我的代码:
dictionary = [NSDictionary dictionaryWithObjects:matchesForUser forKeys:tableData];
sortedFirstArray = [dictionary allKeys];
sortedSecondArray = [dictionary objectsForKeys:sortedFirstArray notFoundMarker:[NSNull null]];
sortedSecondArray = [sortedSecondArray sortedArrayUsingSelector: @selector(compare:)];
我需要 sortedFirstArray 值按照它们在每个数组中的顺序来坚持它们各自的 sortedSecondArray 值。
更新
我的代码尝试进行排序:
PFQuery *query = [PFQuery queryWithClassName:@"_User"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
dispatch_async(dispatch_get_main_queue(), ^{
if (!error) {
entries = [NSMutableArray new];
for (PFObject *object in objects) {
NSLog(@"%@", object.objectId);
[tableData addObject:[object valueForKey:@"username"]];
[matchesForUser addObject:[object valueForKey:@"matches"]];
NSMutableDictionary* entry = [NSMutableDictionary new];
entry[@"username"] = [object valueForKey:@"username"];
entry[@"matches"] = [object valueForKey:@"matches"];
[entries addObject:entry];
//transfer = entries;
}
transfer = [entries sortedArrayUsingComparator:^NSComparisonResult(NSDictionary* a, NSDictionary* b) {
NSDate *first = [a objectForKey:@"matches"];
NSDate *second = [b objectForKey:@"matches"];
NSLog(first);
NSLog(second);
return [first compare:second];
}];
//dictionary = [NSDictionary dictionaryWithObjects:matchesForUser forKeys:tableData];
//sortedFirstArray = [dictionary allKeys];
//sortedSecondArray = [dictionary objectsForKeys:sortedFirstArray notFoundMarker:[NSNull null]];
//sortedSecondArray = [sortedSecondArray sortedArrayUsingSelector: @selector(compare:)];
[_tableView reloadData];
[_tableViewScore reloadData];
}else{
NSLog([error description]);
}
NSLog(@"***tabledata***");
NSLog([NSString stringWithFormat:@"%lu", (unsigned long)[tableData count]]);
NSLog(@"***matchesdata***");
NSLog([NSString stringWithFormat:@"%lu", (unsigned long)[matchesForUser count]]);
});
}];
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if(tableView.tag == 1) {
static NSString *simpleTableIdentifier = @"SimpleTableItem";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
cell.textLabel.font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:16.0];
cell.textLabel.textColor = [UIColor colorWithRed:218.0f/255.0f green:247.0f/255.0f blue:220.0f/255.0f alpha:1.0f];
cell.backgroundColor = [UIColor colorWithRed:153.0f/255.0f green:211.0f/255.0f blue:212.0f/255.0f alpha:1.0f];
cell.layoutMargins = UIEdgeInsetsZero;
cell.preservesSuperviewLayoutMargins = NO;
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
UILabel *contentV = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 230, 44)];
contentV.font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:16.0];
contentV.textColor = [UIColor colorWithRed:218.0f/255.0f green:247.0f/255.0f blue:220.0f/255.0f alpha:1.0f];
contentV.backgroundColor = [UIColor colorWithRed:153.0f/255.0f green:211.0f/255.0f blue:212.0f/255.0f alpha:1.0f];
cell.contentView.layoutMargins = UIEdgeInsetsZero;
NSString *username2 = [[transfer objectAtIndex:indexPath.row] valueForKey:@"username"];
NSLog(@"***username***");
NSLog(username2);
contentV.text = username2;
[cell.contentView addSubview:contentV];
return cell;
}
else {
static NSString *simpleTableIdentifier = @"SimpleTableItem";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
cell.textLabel.font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:16.0];
cell.textLabel.textColor = [UIColor colorWithRed:153.0f/255.0f green:211.0f/255.0f blue:212.0f/255.0f alpha:1.0f];
cell.backgroundColor = [UIColor colorWithRed:218.0f/255.0f green:247.0f/255.0f blue:220.0f/255.0f alpha:1.0f];
cell.textLabel.textAlignment = NSTextAlignmentCenter;
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
NSString *matchAmount = [[transfer objectAtIndex:indexPath.row] valueForKey:@"matches"];
NSLog(@"***matchamount***");
NSLog(matchAmount);
cell.textLabel.text = matchAmount;
return cell;
}
}
【问题讨论】:
-
我认为你最好使用一个 single 字典数组,每个字典都包含一个
username和一个score(或者可能是一个自定义类)。您可以使用自定义块对它们进行排序,该块比较每个字典的score条目,并且您可以使用相同的数组作为两个表的数据源(为每个表选择不同的条目)。 -
酷听起来不错,你能提供一个代码示例吗?我想我有一个粗略的想法,所以如果没有也不要担心
-
如果您知道如何使用
NSDictionary的-objectForKey:和-setObject:forKey:和NSArray的-sortedArrayUsingComparator:,那就很简单了。 -
非常感谢...是的..只是需要复习
-
如果你愿意,你可以发表你的评论作为答案,第一个
标签: ios objective-c arrays sorting indexing