mvasco
mvasco

Reputation: 5101

Updating UILabel text in custom cell

In my UITableViewController there are custom cells populated from JSON. Each cell has a UILabel showing a text value (@"valoracion"). When the user selects a row, a detail view from the cell object is shown. At this detail view, the user can change the value of valoracion. If the user goes back to the UITableViewController, valoracion should show the new value, but actually it shows the old value.

What should I do to update valoracion in the UITableViewController, when the user goes back from the detail view to the UITableViewController.

UPDATED***

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CustomCell *cell =[tableView dequeueReusableCellWithIdentifier:@"Cell"];
    if (cell ==nil){
        cell = [[CustomCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"Cell"];
    }

    //cell.backgroundColor = [UIColor clearColor];

    cell.nombreEmpresaLabel.text = [[categorias objectAtIndex:indexPath.row] objectForKey:@"nombreEmpresa"];



    //[[cell contentView] setBackgroundColor:[UIColor clearColor]];


    //cell.textLabel.backgroundColor = nil;
    // cell.detailTextLabel.backgroundColor=nil;
    cell.textLabel.textColor = [UIColor whiteColor];

  cell.direccionEmpresaLabel.text= [[categorias objectAtIndex:indexPath.row] objectForKey:@"direccionEmpresa"];


    NSMutableString *logo = [[NSMutableString alloc]initWithString:@"http://mujercanariasigloxxi.appgestion.eu/logos/"];
    NSString *imageURL = [[categorias objectAtIndex:indexPath.row] objectForKey:@"strImagen"];


    cell.meGustaHits.text = [[categorias objectAtIndex:indexPath.row] objectForKey:@"valoracionEmpresa"];




    if(imageURL != nil && ![imageURL isEqual:[NSNull null]])
    {
        [logo appendString:imageURL];
        NSURL *logoURL = [NSURL URLWithString:logo];
        NSData *logoData = [NSData dataWithContentsOfURL:logoURL];
        cell.logoImage.image = [UIImage imageWithData:logoData];
    }
    else{
        cell.logoImage.image = [UIImage imageNamed:@"icono80"];
    }



    return cell;
}

Inside viewDidLoad method:

//URL definition where php file is hosted

    int categoriaID = [[categoriaDescription objectForKey:@"idCategoria"] intValue];

    NSString *string = [NSString stringWithFormat:@"%d", categoriaID];
    NSLog(@"CATEGORIA ID STGRING %@",string);
    NSMutableString *ms = [[NSMutableString alloc] initWithString:@"http://mujercanariasigloxxi.appgestion.eu/app_php_files/empresaslist.php?id="];
    [ms appendString:string];
    // URL request
    NSLog(@"URL = %@",ms);
   NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:ms]];
    //URL connection to the internet
    [[NSURLConnection alloc]initWithRequest:request delegate:self];

And now the JSON delegate methods and didSelecRowAtIndexpath method:

//methods to perform the connection and population of data

-(void)connection: (NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{

    data = [[NSMutableData alloc]init];

}

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)thedata
{
    [data appendData:thedata];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    //if data received network indicator not visible
    [UIApplication sharedApplication].networkActivityIndicatorVisible=NO;

    //array waterfalls populated via JSON from database
    categorias = [NSJSONSerialization JSONObjectWithData:data options:nil error:nil];
    NSLog(@"THE DA TA &@",categorias);
    [self.tableView reloadData];
}

//only in case of error
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    UIAlertView *errorView = [[UIAlertView alloc]initWithTitle:@"Error" message:@"The download could not complete - please make sure you are connected to eithre 3G or WiFi" delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
    [errorView show];
    //if no connection network indicator not visible
    [UIApplication sharedApplication].networkActivityIndicatorVisible=NO;

}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DetalleEmpresaViewController *detailViewController =[self.storyboard instantiateViewControllerWithIdentifier:@"detailEmpresaViewController"];
    detailViewController.title =[[categorias objectAtIndex:indexPath.row]objectForKey:@"idCategoria"];
    detailViewController.detalleDescription = [categorias objectAtIndex:indexPath.row];
    [self.navigationController pushViewController:detailViewController animated:YES];
}

Upvotes: 0

Views: 281

Answers (1)

Oleg Sobolev
Oleg Sobolev

Reputation: 3376

Try call [tableview reloadData] method and make sure that you have correct implementation of tableview delegate methods.

If place where you change and save your JSON is another, than tableViewController, then put reloadData method in viewDidAppear of tableViewController class. If you saving JSON in other method of same tableViewController, then place reloadData in last line of this method.

Upvotes: 2

Related Questions