[英]Objective C: How to check for unread messages (number to be displayed as badge)
[英]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.