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

0

How do I create a UITableView , where cells keep the text bold, until they are touched, and save this change?

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

Example, Image Below:

Below is the code I tried, but it did not 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;
    }

How to do this process?

    
asked by anonymous 30.10.2014 / 21:31

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 cellForRowAtIndexPath: method 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]];
}

Considering that object is your database object and isSaved indicates whether the object was saved or not (whether visited or not).

And in the didSelectRowAtIndexPath: method you have the schema 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 caters to you.

    
31.10.2014 / 12:24
0

The problem is the TYPE of the data of LIDO in Core Data . Since the type in%% of the field for the LIDO field is Core Data and I was doing a direct comparison with a STRING in INT ;

Wrong code in IF()

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 is no way for Xcode to know the type of data that comes from cellForRowAtIndexPath: , and at runtime there were no problems since type Core Data to STRING , is equivalent to numeric value, so to check I had to use the method if()

    
02.11.2014 / 00:43