gpt4 book ai didi

ios - 更新自定义单元格中的 UILabel 文本

转载 作者:行者123 更新时间:2023-11-28 22:14:14 24 4
gpt4 key购买 nike

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

我应该怎么做才能更新valoracionUITableViewController ,当用户从详细信息 View 返回到 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 委托(delegate)方法和 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 委托(delegate)方法。

如果你更改和保存 JSON 的地方不是 tableViewController,那么将 reloadData 方法放在 tableViewController 的 viewDidAppear 类。如果您在同一 tableViewController 的其他方法中保存 JSON,则将 reloadData 放在该方法的最后一行。

关于ios - 更新自定义单元格中的 UILabel 文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22135236/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com