我有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