我有tableview,其中是名称和状态。苹果推送通知(APNS)时状态发生变化。 但我有这个问题。如果通知未到,我该怎么办?或者如果用户点击此消息的关闭按钮。 我尝试使用ASIHTTPRequest更新表:从webserver(ASIHTTPRequest,PHP,mysql)更新iOS TableView单元格中的数据
- (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 *设备。你能帮助我吗? 我试试这个,但它没有工作:(你在哪里写的地方)
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];
在那个地方。
EDIT3:
,如果我不喜欢这样写道:
- (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方法并获取状态数组,我认为会更好。
也许我可以将所有serialNubers设置为一个变量('xxx','yyyy','zzz'),然后在服务器上执行SELECT * FROM设备WHERE serialNumber(serialNuber)。
您认为这可行吗?我没有经验如何将数据从数组转换为字符串,例如('array_part1','array_part2'....)
请正确标记您的问题;例如,了解平台(OSX或iOS)比了解用于开发程序的IDE更重要。 – trojanfoe
好的,对不起没有问题。我会做的。 – Miras