在自定义单元格中更新UILabel文本

时间:2014-03-02 23:39:10

标签: ios json uitableview

在我的UITableViewController中,有从JSON填充的自定义单元格。每个单元格都有UILabel,显示文本值(@"valoracion")。当用户选择一行时,将显示来自单元对象的详细视图。在此详细视图中,用户可以更改valoracion的值。如果用户返回UITableViewController,则valoracion应显示新值,但实际上会显示旧值。

当用户从详细信息视图返回valoracion时,如何更新UITableViewController中的UITableViewController

修订* * *

- (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;
}

内部viewDidLoad方法:

//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];

现在JSON委托方法和didSelecRowAtIndexpath方法:

//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];
}

1 个答案:

答案 0 :(得分:2)

尝试调用[tableview reloadData]方法,并确保正确实现tableview委托方法。

如果您更改并保存JSON的位置不是tableViewController,那么请将reloadData方法放在viewDidAppear类的tableViewController中。如果您将JSON保存在同一tableViewController的其他方法中,请将reloadData放在此方法的最后一行。