How to make a Uitableviewcell work like Facebook Messenger for iOS?

Asked

Viewed 244 times

0

How to create a UITableView, where cells keep the text bold until touched, and save this change?

For example, when we receive a message on Facebook the text on UITableViewCell is in bold, after touching the cell and see and return to UITableView, cell text no longer in bold.

Example, Image Below:

Below the code I tried but it didn’t work:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
.
.
.
//Implementation
.
.
.
// Atualiza o estado de NÃO LIDO para LIDO no banco de dados

 [[fetchedObjects objectAtIndex:indexPath.row ] setValue:@"1" forKey:@"lido"];

    NSError*error;

    // Escreve no banco a alteração
    [context save:&error];

    for (int index = 0; index<[fetchedObjects count]; index++) {

        NSLog(@"%@",[[fetchedObjects objectAtIndex:index]valueForKey:@"lido"]);

.
.
.
// Implementation

}




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

//Implmentation

.
.
.

 //Verifica o estado e faz a alteração na fonte da célula

if ([[arrayProcessosLocal objectAtIndex:indexPath.row ]  valueForKey:@"lido"] == 1) {

        //Torna fonte bold
       cell.textLabel.font = [UIFont boldSystemFontOfSize:17];

      [cell.textLabel setText:[NSString stringWithFormat:@"%@ - %@", [processosLocal valueForKey:@"processo"],[processosLocal valueForKey:@"data_pdf"]]];

        return cell;
    }else{

        // Exibe a fonte normal caso o valor de "lido" seja 0 (zero)        
         [cell.textLabel setText:[NSString stringWithFormat:@"%@ - %@", [processosLocal valueForKey:@"processo"], [processosLocal valueForKey:@"data_pdf"]]];
        return cell;
    }

Antes de selecionar a célula

Após selecionar a célula, ver a conversa e voltar à UITableView

How to do this process?

2 answers

1


In a slightly simpler way, considering that you have some way to persist this data, you can do something like this in the method cellForRowAtIndexPath: of your table:

if ([objPessoa isSaved]) {
    [cell.textLabel setFont:[UIFont systemFontOfSize:16.0f]];
    [cell.detailTextLabel setFont:[UIFont systemFontOfSize:11.0f]];
} else {
    [cell.textLabel setFont:[UIFont boldSystemFontOfSize:16.0f]];
    [cell.detailTextLabel setFont:[UIFont boldSystemFontOfSize:11.0f]];
}

whereas objPessoa is the object of the database and isSaved indicates whether the object was saved or not (whether it was visited or not).

And in the method didSelectRowAtIndexPath: you have the scheme that performs the visit and then you can update the line to remove the bold:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

    [cell.textLabel setFont:[UIFont systemFontOfSize:16.0f]];
    [cell.detailTextLabel setFont:[UIFont systemFontOfSize:11.0f]];
}

See if it suits you.

  • I already have a persistence in core data, and I did a logic similar to yours, but it didn’t work... I’ll try with your code and I’ll tell you if it worked or not! Thank you!

  • I put the excerpt of the code I did, take a look and see what you think... I haven’t run the test with the code you gave me, but I’m going to do Aja!

  • it seems that when it detects if, it enters that pattern and determines the value of the source for all of the following cells :/. The same result I had with your code I had with my... did you see my code? When I put an exclamation (reverse the logical appraisal of if) if(![myObject isSaved]) it puts everything in bold, even reading from the bank that some are already READ it puts them back in bold.

  • I found the problem, I was making a wrong comparison between the types and the Xcode can not check this error because it is only possible to know the TYPE in run time! I will post the answer!

0

The problem is the LIDO data TYPE in the Core Data. For the guy in Core Data to the LIDO field is STRING and I was making a direct comparison with a INT in the IF();

Wrong code in cellForRowAtIndexPath:

if ([[arrayProcessosLocal objectAtIndex:indexPath.row ]  valueForKey:@"lido"] == 1) {
.
.
.
// Demais implementação

Correct Code!

if ([[[arrayProcessosLocal objectAtIndex:indexPath.row ]  valueForKey:@"lido"] isEqual:@"1"]) {
.
.
.
// Demais implementação

There’s no way the Xcode can tell what kind of data comes from Core Data, and at runtime there were no problems as the type STRING to the if(), is equivalent to numerical value, so to make the correct verification I had to use the method isEqual:

Browser other questions tagged

You are not signed in. Login or sign up in order to post.