我有表格视图,名称和状态在哪里。当苹果推送通知 (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];
}
或者,如果没有苹果通知或用户没有点击通知消息,那么在我的表格视图中更改状态的更好方法是什么?谢谢
编辑:我不知道如何将数据存储到 NSManagedObject *device。你能帮我解决这个问题吗?我试试这个,但它没有用:(在你写的地方)
NSInteger statusInt = [status intValue]; //change to int value
NSManagedObject *device = [self.devices objectAtIndex:indexPath.row];
[device setValue:statusInt forKey:@"status"];
EDIT2:我明白了,但问题在于重新加载表数据
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
// [设备 setValue:@5 forKey:@"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'....)