【问题标题】:Displaying iOS badge number / Handling unread messages (xCode - OBJECTIVE-C)显示 iOS 徽章编号/处理未读消息 (xCode - OBJECTIVE-C)
【发布时间】:2015-12-06 13:04:09
【问题描述】:

我想更新收件箱徽章以显示未读邮件的数量(我的邮件不会自毁,应用程序类似于 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 findObjects] 行收到此警告,请使用方法 findObjectsInBackgroundWithBlock() 而不是 findObjects。
  • 如何设置那行代码?另外,我希望在所有消息加载的同时加载未读消息。我之前使用过两个“findobjectsInBackWithBlock”代码,只有一个运行,直到我刷新了表格视图

标签: objective-c xcode icons badge read-unread


【解决方案1】:
[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];
    }
}

【讨论】:

  • 刚刚添加了我的代码,用于根据已读或未读更新单元格。我也将不胜感激!现在 self.unreadmessages 从不包含消息
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-17
  • 2020-12-26
  • 1970-01-01
  • 2014-07-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多