繁体   English   中英

显示iOS徽章编号/处理未读消息(xCode-OBJECTIVE-C)

[英]Displaying iOS badge number / Handling unread messages (xCode - OBJECTIVE-C)

我想更新收件箱标志以显示未读消息的数量(我的消息不会自毁,应用程序类似于iOS消息)。

当用户单击消息并返回到“收件箱”选项卡时,徽章应进行更新。 另外,我想标记未读单元格的背景颜色与已读单元格的不同...这样用户可以知道已读和未读的内容。

我有一些工作代码,但现在我得到:

“警告:正在主线程上执行长时间运行的操作。中断warnBlockingOperationOnMainThread()进行调试。”

这是我用于更新未读消息的代码:

PFQuery *query = [PFQuery queryWithClassName:@"Messages"];
    [query whereKey:@"recipientIds" equalTo:[[PFUser currentUser] objectId]];
    [query orderByDescending:@"createdAt"];
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (error) {
            NSLog(@"Error: %@ %@", error, [error userInfo]);
        } else {
            // messages were found!
            self.messages = objects;

            //find unread messages
            [query whereKey:@"readBy" notEqualTo:[[PFUser currentUser] objectId]];
            self.unreadMessages = [query findObjects];

            // set badge # to number of msgs
            if (self.unreadMessages.count > 0) {

                [[self navigationController] tabBarItem].badgeValue = [NSString stringWithFormat:@"%lu", (unsigned long)self.unreadMessages.count];
            }

            [self.tableView reloadData];

用于更新单元格的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

PFObject *message = [self.messages objectAtIndex:indexPath.row];
cell.textLabel.text = [message objectForKey:@"senderEmail"];

if ([self.unreadMessages containsObject:message]) {
    // color background gray, bold the font
}else{
   // leave cell alone
}
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error)  
{
    if (!error) 
    {
        self.unreadMessages = [NSMutableArray arrayWithArray:objects];
       // set badge # to number of msgs
        if (self.unreadMessages.count > 0) 
        { 
            [[self navigationController] tabBarItem].badgeValue = [NSString stringWithFormat:@"%lu", (unsigned long)self.unreadMessages.count];
        }
        [self.tableView reloadData];
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM