Obj-C - 如果过滤结果包含 x >则执行动画?



我正在过滤一个NSDictionary,过滤的项目显示在tableView中。我想执行一个动画(self.tableView.frame...(仅当过滤计数大于 - 但是,我无法让我的过滤计数在 animateTextView 中编写 if 语句。请参阅下面的代码 - 我这样做的最佳方法是什么?

.m

- (void)viewDidLoad {
    [super viewDidLoad];
    NSMutableDictionary *viewParams = [NSMutableDictionary new];
    [viewParams setValue:@"chat" forKey:@"view_name"];
    [DIOSView viewGet:viewParams success:^(AFHTTPRequestOperation *operation, id responseObject) {
        dispatch_async(dispatch_get_main_queue(), ^{
        self.messages = (NSMutableArray *)responseObject;
        [self.tableView reloadData];

            });
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Failure: %@", [error localizedDescription]);
    }];
}
    - (void) animateTextView:(BOOL) up
    {      
        const int movementDistance = self.keyboardHeight;
        const float movementDuration = 0.2f;
        int movement= movement = (up ? -movementDistance : movementDistance);

        [UIView beginAnimations: @"anim" context: nil];
        [UIView setAnimationBeginsFromCurrentState: YES];
        [UIView setAnimationDuration: movementDuration];
        self.upView.frame = CGRectOffset(self.upView.frame, 0, movement);
        [UIView setAnimationDidStopSelector:@selector(afterAnimationStops)];
        [UIView commitAnimations];
        self.tableView.frame = CGRectOffset(self.tableView.frame, 0, movement);
        [UIView setAnimationDidStopSelector:@selector(afterAnimationStops)];
        [UIView commitAnimations];

    }
    - (int)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        if ([self.messageDataFriends count] > 0) {
            NSDictionary *userDictInfo = (NSDictionary*) [NSKeyedUnarchiver unarchiveObjectWithData:[[NSUserDefaults standardUserDefaults] objectForKey:@"diosSession"]];
            DIOSSession *session = [DIOSSession sharedSession];
            [session setUser:userDictInfo];
            [session user];
            NSString *targetedUser = [self.messageDataFriends objectForKey:@"uid2"];
            NSString *myID = [session user][@"user"][@"uid"];

            NSPredicate *p1 = [NSPredicate predicateWithFormat:@"uid contains[cd] %@", targetedUser];
            NSPredicate *p2 = [NSPredicate predicateWithFormat:@"targetuser contains[cd] %@", myID];
            NSPredicate *p3 = [NSPredicate predicateWithFormat:@"uid contains[cd] %@", myID];
            NSPredicate *p4 = [NSPredicate predicateWithFormat:@"targetuser contains[cd] %@", targetedUser];
            NSCompoundPredicate *pIntermediary1 = [NSCompoundPredicate andPredicateWithSubpredicates:@[p1, p2,]];
            NSCompoundPredicate * pIntermediary2 = [NSCompoundPredicate andPredicateWithSubpredicates:@[p3, p4,]];
            NSCompoundPredicate *pFinal = [NSCompoundPredicate orPredicateWithSubpredicates:@[pIntermediary1, pIntermediary2]];
            NSArray *filtered = [self.messages filteredArrayUsingPredicate:pFinal];
            return [filtered count];
            }

首先,将过滤过程(MVC 的模型部分的功能(与tableView:numberOfRowsInSection:方法(控制器将数据发送到视图(分开可能是个好主意:

-(NSArray *)filterMessages {
    //returns the filtered array
}

然后,您可以将过滤后的数组保存在另一个实例变量中,并使用它:

- (int)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
   return self.filteredMessages.count;
}

最后,要在需要时制作动画:

 dispatch_async(dispatch_get_main_queue(), ^{
    self.messages = (NSMutableArray *)responseObject;
    self.filteredMessages = [self filterMessages];
    [self.tableView reloadData];
    if(self.filteredMessages.count > ANIMATE_THRESHHOLD){
        [self animateTextView:YES];
    }
});

顺便说一下,除非您进行 UI 调用来执行此操作,否则可以从主线程中提取过滤。只需将呼叫移动到filterMessages,并将呼叫移动到调度块之外。

最新更新