gpt4 book ai didi

ios - 从网络服务器(ASIHTTPRequest、PHP、mysql)更新 iOS TableView 单元格中的数据

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

我有表格 View ,其中名称和状态。苹果推送通知 (APNS) 出现时,状态会发生变化。但是我有这个问题。如果没有收到通知,我该怎么办?或者,如果用户点击此消息的关闭按钮。我尝试使用 ASIHTTPRequest 更新表:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
static NSString *CellIdentifier = @"Cell";
HomePageTableCell *cell = (HomePageTableCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

// Configure the cell...
NSManagedObject *device = [self.devices objectAtIndex:indexPath.row];
cell.nameLabel.text = [device valueForKey:@"name"];

if ([[device valueForKey:@"status"] isEqualToNumber:@1])
{
cell.status.text = @"Not configured";
cell.stav.image = [UIImage imageNamed:@"not_configured.png"];
}
if ([[device valueForKey:@"status"] isEqualToNumber:@2])
{
//some other states
}

return cell;
}

我尝试在单元格加载之前更改状态...

- (void) getStatus:(NSString *)serialNumber
{
NSURL *url = [NSURL URLWithString:@"link to my server"];

__block ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
__weak ASIHTTPRequest *request_b = request;
request.delegate = self;

[request setPostValue:@"updatedevice" forKey:@"cmd"];
[request setPostValue:serialNumber forKey:@"serial_number"]; //get status of this serial number

[request setCompletionBlock:^
{
if([self isViewLoaded])
{
[MBProgressHUD hideHUDForView:self.view animated:YES];
if([request_b responseStatusCode] != 200)
{
ShowErrorAlert(@"Comunication error", @"There was an error communicating with the server");
}
else
{
NSString *responseString = [request_b responseString];
SBJsonParser *parser = [[SBJsonParser alloc] init];
NSDictionary *result = [parser objectWithString:responseString error:nil];

status = [result objectForKey:@"status"];
NSInteger statusInt = [status intValue]; //change to int value

//here I want to change cell status in SQLite, but don't know how
//something with indexPath.row? valueForKey:@"status"???
}
}
}];
[request setFailedBlock:^
{
if ([self isViewLoaded])
{
[MBProgressHUD hideHUDForView:self.view animated:YES];
ShowErrorAlert(@"Error", [[request_b error] localizedDescription]);

}
}];

[request startAsynchronous];
}

或者如果没有收到苹果通知或用户没有点击通知消息,这是在我的表格 View 中更改状态的更好方法吗?谢谢

编辑:我不知道如何将数据存储到 NSManagedObject *设备。你能帮我吗?我尝试了这个,但它没有用:(在你写的地方)

NSInteger statusInt = [status intValue]; //change to int value
NSManagedObject *device = [self.devices objectAtIndex:indexPath.row];
[device setValue:statusInt forKey:@"status"];

编辑2:我明白了,但问题是重新加载表数据

 NSString *responseString = [request_b responseString];
SBJsonParser *parser = [[SBJsonParser alloc] init];
NSDictionary *result = [parser objectWithString:responseString error:nil];

NSString *status = [result objectForKey:@"status"];
NSInteger statusInt = [status intValue]; //change to int value
NSManagedObject *device = [self.devices objectAtIndex:indexPath.row];
[device setValue:statusInt forKey:@"status"]; //there is problem in save statusInt

//[设备设置值:@5 forKey:@"status"];//如果我这样做就可以了 status 是 integer16 类型

第二个问题是重新加载表数据。我把这个放在那里

[self.tableView reloadData]

但是它在循环中一次又一次地重新加载,有什么问题吗?我的事情是无限循环,如果我没有重新加载表数据更改将在下一个应用程序加载中可见。我认为问题是我调用

- (void) getStatus:(NSString *)serialNumber atIndexPath:(NSIndexPath *)indexPath
{}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
}

更好的应该在 viewDidLoad 或 viewDidApper 中,但我不知道如何为所有设备制作循环并调用

    [self getStatus:[device valueForKey:@"serialNumber"] atIndexPath:indexPath];

在那个地方。

编辑3:

如果我这样做会怎样:

- (void)viewDidLoad
{
[self updateData];
[self.tableView reloadData];
}
-(void)updateData
{
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Device"];
request.returnsDistinctResults = YES;
//request.resultType = NSDictionaryResultType;
request.propertiesToFetch = @[@"serialNumber"];

NSArray *fetchedObjects = [self.managedObjectContext
executeFetchRequest:request error:nil];

NSArray *result = [fetchedObjects valueForKeyPath:@"serialNumber"];

//there I get all serialNumbers of my devices and than I call method getStatus and get "new" status and than update it in Core Data.
}

这是解决这个问题的好方法吗?我认为如果我只调用一次 getStatus 方法并获得状态数组会更好。

也许我可以在一个变量 ('xxx','yyyy','zzz') 中设置所有 serialNubers,然后在服务器上执行 SELECT * FROM Devices WHERE serialNumber in (serialNuber)。

你认为这可行吗?我没有经验如何将数据从数组获取到字符串,例如 ('array_part1','array_part2'....)

最佳答案

您在代码中的什么地方调用了[UITableView reloadData]

从服务器检索到新数据后,您应该在 tableview 上调用 reloadData。由于您的服务器调用是异步的,因此服务器调用将在主线程继续运行时在单独的线程上运行,因此我认为您有以下问题...

- (void) ...
{
[self getStatus:@"SERIAL_NUMBER"];
[self reloadData]; // This will be called before the async server call above has finished
}

因此您正在重新加载原始数据,因此不会显示可能在几秒后加载的新数据。

要解决此问题,请调整 [getStatus:] 方法以在服务器响应时调用 [UITableView reloadData] 方法。

[request setCompletionBlock:^
{
if([self isViewLoaded])
{
[MBProgressHUD hideHUDForView:self.view animated:YES];
if([request_b responseStatusCode] != 200)
{
ShowErrorAlert(@"Comunication error", @"There was an error communicating with the server");

}
else
{

NSString *responseString = [request_b responseString];
SBJsonParser *parser = [[SBJsonParser alloc] init];
NSDictionary *result = [parser objectWithString:responseString error:nil];

status = [result objectForKey:@"status"];
NSInteger statusInt = [status intValue]; //change to int value

// Store the server response in NSManagedObject *device,
// which will be used as the data source in the tableView:cellForRowAtIndexPath: method

// Once stored, check the tableview isn't NULL and therefore can be accessed
// As this call is async the tableview may have been removed and therefore
// a call to it will crash
if(tableView != NULL)
{
[tableView reloadData];
}
}
}
}];

ASIHTTPRequest 也不再被开发者支持,我建议你看看AFNetworking .


更新

针对您现在在设备 NSManagedObject 中设置 statusInt 时遇到的问题

 NSManagedObject *device = [self.devices objectAtIndex:indexPath.row];
[device setValue:statusInt forKey:@"status"]; //there is problem in save statusInt

这是因为 statusInt 是一个 NSInteger,它是主要数据类型,而不是 [NSManagedObject setValue 预期的 NSObject :forKey:]。来自 [NSManagedObject setValue:forKey:] 的文档,方法预期参数如下。

- (void)setValue:(id)value forKey:(NSString *)key

因此,在本例中,您需要传递一个 NSNumberNSInteger 的问题在于,它只是基于当前系统的最大 int 数据类型的 dynamic typedef。从 NSInteger 的实现中,您可以看到抽象。

#if __LP64__
typedef long NSInteger;
#else
typedef int NSInteger;
#endif

如果您当前的系统是 64 位系统,它将使用更大的 long 数据类型。

现在,从技术上讲,从服务器返回的 status 值可以原样存储,无需任何转换为​​ NSString。当您需要检索和使用 int 的主要数据类型时,您可以使用您已经使用过的 [NSString intValue] 方法。

尽管最佳做法是使用 NSNumberFormatter,这对于基于locale 的数字调整很有用,并确保不存在无效字符。

NSString *status = [result objectForKey:@"status"];
NSNumberFormatter * f = [[NSNumberFormatter alloc] init];
NSNumber * statusNumber = [f numberFromString:status];
NSManagedObject *device = [self.devices objectAtIndex:indexPath.row];
[device setValue:statusNumber forKey:@"status"];

当您希望在代码中使用 int 时要检索主要数据类型,只需调用 [NSNumber intValue]

NSNumber *statusNumber = [device objectForKey:@"status"];
int statusInt = [statusNumber intValue];

至于无限循环的问题,这是由调用 [... getStatus:atIndexPath:] 引起的,其中包含方法调用 reloadData ,来自 [UITableView tableView:cellForRowAtIndexPath:]

这是因为reloadData实际上调用了[UITableView tableView:cellForRowAtIndexPath:]。因此,您的代码不断如下......

Initial UITableView data load -> tableView:cellForRowAtIndexPath: -> getStatus:atIndexPath: -> Server Response -> reloadData -> tableView:cellForRowAtIndexPath: -> getStatus:atIndexPath: -> Server Response -> reloadData -> ...

不幸的是,您不能只强制更新一个单元格,您必须请求 UITableView 使用 reloadData 重新加载所有数据。因此,如果可能,您需要调整服务器以返回设备的唯一 ID,以便您可以仅在 NSManagedObject 中调整更新的设备。

getStatus 方法的建议更改可能只是使用 serialNumber,如果它存储在 NSManagedObject 中作为键。

- (void) getStatus:(NSString*)serialNumber

关于ios - 从网络服务器(ASIHTTPRequest、PHP、mysql)更新 iOS TableView 单元格中的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17421090/

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