我想更新收件箱徽章以显示未读消息的数量(我的消息不会自毁,应用程序类似于 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
}