简体   繁体   English

更改表视图单元格颜色如果消息是新的

[英]Change Table View Cell Color If message is New

I am new in iOS so please help me. 我是iOS新手所以请帮助我。 I am getting some messages from server with a key " messageId ". 我从服务器获得一些带有“ messageId ”键的消息。 I am showing those messages in tableview. 我在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. 现在我必须区分如果在该tableview中第一次打开新消息,我必须更改该单元格中该消息的颜色。

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. @AADi我看到你收到来自通知的消息。 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. 更好的方法是发送布尔变量,例如seenread ,以及来自服务器的消息的其他细节,并相应地显示单元格。 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. 要将消息标记为已读或已显示,您可以使用在后端反映它的messageID对服务器进行API调用。

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

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