簡體   English   中英

在自定義單元格中更新UILabel文本

[英]Updating UILabel text in custom cell

在我的UITableViewController中,有一些從JSON填充的自定義單元格。 每個單元格都有一個顯示文本值的UILabel@"valoracion" )。 當用戶選擇一行時,將顯示來自單元對象的詳細視圖。 在此詳細視圖中,用戶可以更改valoracion的值。 如果用戶返回UITableViewControllervaloracion應顯示新值,但實際上它顯示舊值。

我應該怎么做更新valoracionUITableViewController當用戶從細節着眼於蝸居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];
}

嘗試調用[tableview reloadData]方法並確保正確實現tableview委托方法。

如果您更改並保存JSON的位置是另一個,而不是tableViewController ,則將reloadData方法放在tableViewController類的viewDidAppear中。 如果將JSON保存在同一tableViewController其他方法中,則將reloadData放在此方法的最后一行中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM