简体   繁体   中英

read and unread messages in xcode

I am developing an iOS application that acts as a kind of email client. One of the views in my story board is an inbox in which displayed for each email: The sender of the message, the title of the conversation, the begining of the message body and the date & time at which the message was sent. (in tableView) How can I put these text in bold if the message isn't read yet, and in normal style after the user clic on the message ? Thank you !

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
messageTableViewCell *cell = (messageTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellidentifier];

// Configure Cell

NSDictionary *currentMessage = [self.unreadmessagesArray objectAtIndex:indexPath.row];

cell.nom.text = [currentMessage valueForKey:@"sentBy"];
cell.titre.text = [currentMessage valueForKey:@"messageThreadTitle"];
cell.resume.text = [currentMessage valueForKey:@"messageBody"];

cell.date.text = [currentMessage valueForKey:@"sentOn"];

return cell;

}

Your table is loaded from a datasource in your app, this means that each email / message / or whatever you have contains its own properties (where you are reading the title from). Add a flag there to indicate if it was already read or not, if it hasn't been read simply change the UILabel of the cell to show the title in bold.
You have a method where you are returning a "UITableViewCell", it goes there.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 

2 things need to do : 1. save read / unread state on server side, if you want to save the result for all time. 2. you have to set a key-value pair of isSelected (TRUE OR FALSE) at your end.

- (void) viewDidLoad {

// data coming from server keep in array having a dictionary 

    unreadmessagesArray  = [[NSMutableArray alloc] init];
    [unreadmessagesArray addObject:[NSDictionary dictionaryWithObjectsAndKeys:@"value", @“someOtherkey", [NSNumber numberWithBool:FALSE], @"isSelected", nil]];
    [unreadmessagesArray addObject:[NSDictionary dictionaryWithObjectsAndKeys:@"value", @"someOtherkey", [NSNumber numberWithBool:FALSE], @"isSelected", nil]];
    [unreadmessagesArray addObject:[NSDictionary dictionaryWithObjectsAndKeys:@"value", @"someOtherkey", [NSNumber numberWithBool:FALSE], @"isSelected", nil]];
    [unreadmessagesArray addObject:[NSDictionary dictionaryWithObjectsAndKeys:@"value", @"someOtherkey", [NSNumber numberWithBool:FALSE], @"isSelected", nil]];
]

}  

In cellForRowAtIndexPath :

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
messageTableViewCell *cell = (messageTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellidentifier];



NSDictionary *currentMessage = [self.unreadmessagesArray objectAtIndex:indexPath.row];

cell.nom.text = [currentMessage valueForKey:@"sentBy”];//someOtherkey
cell.titre.text = [currentMessage valueForKey:@"messageThreadTitle”];//someOtherkey
cell.resume.text = [currentMessage valueForKey:@"messageBody”];//someOtherkey

cell.date.text = [currentMessage valueForKey:@"sentOn”];//someOtherkey

if([currentMessage valueForKey:@“isSelected”]) {
    [cell.titre setFont:[UIFont boldSystemFontOfSize:10]];

}
else {
    [cell.titre setFont:[UIFont systemFontOfSize:10]];
}

return cell;
}

Now in didSelectRowAtIndexPath :

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    NSDictionary *currentMessage = [self.unreadmessagesArray objectAtIndex:indexPath.row];

    [currentMessage setValue:[NSNumber numberWithBool:YES] ForKey:@“isSelected” ];

    }

You can use didSelectRowAtIndexPath method of tableView.

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //Mark that selected row as read...
    //you can change the elements in array
    //after change in array element plz reload tableView...
}

Hope this helps...

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