显示以!开头的条目!当数据来自NSMutableDictionary时,首先在UITableViewCell中

时间:2013-12-17 15:55:35

标签: ios objective-c uitableview firebase

我正在与Firebase合作。我从Firebase获取包含名称的数据。如果名称以!开头,那么它很重要,因此它会获得另一种颜色并应首先显示。颜色变化没有问题,但我真的不知道如何进行排序。

使用Firebase,我会获取数据并将其添加到UITableView,但现在我还要首先显示!的数据。我的方法怎么样?这就是我展示UITableViewCell

的方式

数据仅包含以!开头的标题。

NSArray *tempKeys                   = [dictionaryFinishedProjects allKeys];
NSString *tempIdentifier            = [tempKeys objectAtIndex:indexPath.row];
static NSString *CellIdentifier     = @"Cell";

CustomButton *stateButton           = [CustomButton buttonWithType:UIButtonTypeRoundedRect];
stateButton.projectButtonIdentifier = tempIdentifier;
stateButton.frame                   = CGRectMake(5.0f, 10.0f, 24.0f, 24.0f);
[stateButton setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
[stateButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];
[stateButton addTarget:self action:@selector(updateState:) forControlEvents:UIControlEventTouchUpInside];

CustomCell *cell                    = [[CustomCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:tempIdentifier];

if (cell == nil) {
    cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

NSString *important                 = @"No";
NSString *projectName               = [dictionaryFinishedProjects objectForKey:[tempKeys objectAtIndex:indexPath.row]];
if ( [projectName rangeOfString:@"!"].location == NSNotFound ) {
} else {
    important                       = @"Yes";
    projectName                     = [projectName substringFromIndex:1];
    if ( [projectName rangeOfString:@":"].location == NSNotFound ) {}
    else {
        NSArray *splittedProjectName    = [projectName componentsSeparatedByString:@":"];
        NSString *temporaryString       = [splittedProjectName objectAtIndex:1];
        NSString *removedWhiteSpaces    = [temporaryString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
        projectName                     = [splittedProjectName objectAtIndex:0];
        cell.detailTextLabel.text       = removedWhiteSpaces;
    }

    cell.textLabel.text                 = projectName;

    cell.projectCellIdentifier          = tempIdentifier;
    cell.textLabel.frame                = CGRectMake(150.0f, 10.0f, 50.0f, 44.0f);
    UIFont *fontBoldCell                = [UIFont boldSystemFontOfSize:20];
    cell.textLabel.font                 = fontBoldCell;
    cell.detailTextLabel.textColor      = [UIColor grayColor];
    cell.indentationLevel               = 3;

    Firebase *tempRef = [[Firebase alloc] initWithUrl:[NSString stringWithFormat:@"%@/%@", stringRequestUrl, tempIdentifier]];
    [tempRef observeEventType:FEventTypeChildAdded withBlock:^(FDataSnapshot *snapshot) {
        if ( [snapshot.name isEqualToString:@"state"] ) {
            if ( [snapshot.value isEqualToString:@"canceled"]){
                if ( [important isEqualToString:@"No"] ){
                    [stateButton setBackgroundImage:imageStatusCancelled forState:UIControlStateNormal];
                } else {
                    [stateButton setBackgroundImage:imageStatusCancelledRed forState:UIControlStateNormal];
                }
            } else if ( [snapshot.value isEqualToString:@"done"]){
                if ( [important isEqualToString:@"No"] ){
                    [stateButton setBackgroundImage:imageStatusDone forState:UIControlStateNormal];
                } else {
                    [stateButton setBackgroundImage:imageStatusDoneRed forState:UIControlStateNormal];
                }
            }
        }
    }];
    [cell addSubview:stateButton];
    return cell;

2 个答案:

答案 0 :(得分:2)

根据对象对键进行排序......可以这样做:

NSArray *sortedKeys = [dictionaryFinishedProjects keysSortedByValueWithOptions:0 usingComparator:^NSComparisonResult(NSString *obj1, NSString *obj2) {
    BOOL firstImportant = [obj1 hasPrefix:@"!"];
    BOOL secondImportant = [obj2 hasPrefix:@"!"];
    if(firstImportant && !secondImportant) return NSOrderedAscending;
    if(secondImportant && !firstImportant) return NSOrderedDescending;
    return [obj1 caseInsensitiveCompare:obj2];
}];

此外,您不应该对cellForRow实现中的所有数据进行排序/迭代。只执行一次并将数据保存在实例变量中。不只是在cellForRow中访问它......

注意:此实现还按字母顺序对所有键进行排序..具有!和那些没有..

答案 1 :(得分:0)

您可以为您认为重要的优先级设置优先级,而不是添加“!”到关键名称。请参阅此处以获取示例:https://www.firebase.com/docs/ordered-data.html。这将导致Firebase按优先级顺序将它们返回给您。

如果你想进行更复杂的排序,设置优先级无法处理(可能将时间戳设置为优先级?),那么请使用jaydee3建议的内容,并使用自己的任何自定义排序方法进行一次排序。我喜欢。