简体   繁体   中英

Change Table View Cell Color If message is New

I am new in iOS so please help me. I am getting some messages from server with a key " messageId ". I am showing those messages in tableview. Now I have to differentiate that if there is new message which is being open first time in that tableview, I have to change the colour of that message in the cell.

cell.notificationMessageDate.text = [[self.notifications objectAtIndex:indexPath.row] valueForKey:@"msgCreatedDate"];

cell.notificationMessageText.text = [self stringByStrippingHTML:[[self.notifications objectAtIndex:indexPath.row] valueForKey:@"message"]];

NSString *str = [[self.notifications objectAtIndex:indexPath.row] valueForKey:@"messageId"];

self.isAlreadyExist = false;


if (str ) {
    cell.backgroundColor = [UIColor whiteColor];
} else {

}

@AADi i see that you are getting message from notifications. I dont know what structure you have implemented.

To implement what you want, what i feel is you must implement a dictionary or array where you store your data from wherever you are fetching it. Then pass the count of the array or dict. to table. Now maintain a variable which stores the previous count of the array or dict. In cell for row, compare the index path with the previous count of the array or dict. If the index path is greater then previous count simply change the background for the cell at that index path and update the value of previous count with current count.

If you don't understand or have any doubts then ask here.

Hope it will help you.

A word of advice, its not a good approach to check if message is new locally. If a user uninstalls the app and reinstall it then all the messages would appear new to the user even thought she may have read them.

A better approach is to send boolean variable, something like seen or read , along with other details of message from the server and display the cells accordingly. To mark a message as read or seen you can make an API call to the server with the messageID reflecting it in the backend.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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