- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有表格 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 预期的
。来自 [NSManagedObject setValue:forKey:] 的文档,方法预期参数如下。NSObject
:forKey:]
- (void)setValue:(id)value forKey:(NSString *)key
因此,在本例中,您需要传递一个 NSNumber
。 NSInteger
的问题在于,它只是基于当前系统的最大 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/
我在 JavaScript 文件中运行 PHP,例如...... var = '';). 我需要使用 JavaScript 来扫描字符串中的 PHP 定界符(打开和关闭 PHP 的 )。 我已经知道使
我希望能够做这样的事情: php --determine-oldest-supported-php-version test.php 并得到这个输出: 7.2 也就是说,php 二进制检查 test.
我正在开发一个目前不使用任何框架的大型 php 站点。我的大问题是,随着时间的推移慢慢尝试将框架融入应用程序是否可取,例如在创建的新部件和更新的旧部件中? 比如所有的页面都是直接通过url服务的,有几
下面是我的源代码,我想在同一页面顶部的另一个 php 脚本中使用位于底部 php 脚本的变量 $r1。我需要一个简单的解决方案来解决这个问题。我想在代码中存在的更新查询中使用该变量。 $name)
我正在制作一个网站,根据不同的情况进行大量 PHP 重定向。就像这样...... header("Location: somesite.com/redirectedpage.php"); 为了安全起见
我有一个旧网站,我的 php 标签从 因为短标签已经显示出安全问题,并且在未来的版本中将不被支持。 关于php - 如何避免在 php 文件中写入
我有一个用 PHP 编写的配置文件,如下所示, 所以我想用PHP开发一个接口(interface),它可以编辑文件值,如$WEBPATH , $ACCOUNTPATH和 const值(value)观
我试图制作一个登录页面来学习基本的PHP,首先我希望我的独立PHP文件存储HTML文件的输入(带有表单),但是当我按下按钮时(触发POST到PHP脚本) )我一直收到令人不愉快的错误。 我已经搜索了S
我正在寻找一种让 PHP 以一种形式打印任意数组的方法,我可以将该数组作为赋值包含在我的(测试)代码中。 print_r 产生例如: Array ( [0] => qsr-part:1285 [1]
这个问题已经有答案了: 已关闭11 年前。 Possible Duplicate: What is the max key size for an array in PHP? 正如标题所说,我想知道
我正在寻找一种让 PHP 以一种形式打印任意数组的方法,我可以将该数组作为赋值包含在我的(测试)代码中。 print_r 产生例如: Array ( [0] => qsr-part:1285 [1]
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 9 年前。 Improve this ques
我在 MySQL 数据库中有一个表,其中存储餐厅在每个工作日和时段提供的菜单。 表结构如下: i_type i_name i_cost i_day i_start i_
我有两页。 test1.php 和 test2.php。 我想做的就是在 test1.php 上点击提交,并将 test2.php 显示在 div 中。这实际上工作正常,但我需要向 test2.php
我得到了这个代码。我想通过textarea更新mysql。我在textarea中回显我的MySQL,但我不知道如何更新它,我应该把所有东西都放进去吗,因为_GET模式没有给我任何东西,我也尝试_GET
首先,我是 php 的新手,所以我仍在努力学习。我在 Wordpress 上创建了一个表单,我想将值插入一个表(data_test 表,我已经管理了),然后从 data_test 表中获取所有列(id
我有以下函数可以清理用户或网址的输入: function SanitizeString($var) { $var=stripslashes($var); $va
我有一个 html 页面,它使用 php 文件查询数据库,然后让用户登录,否则拒绝访问。我遇到的问题是它只是重定向到 php 文件的 url,并且从不对发生的事情提供反馈。这是我第一次使用 html、
我有一个页面充满了指向 pdf 的链接,我想跟踪哪些链接被单击。我以为我可以做如下的事情,但遇到了问题: query($sql); if($result){
我正在使用 从外部文本文件加载 HTML/PHP 代码 $f = fopen($filename, "r"); while ($line = fgets($f, 4096)) { print $l
我是一名优秀的程序员,十分优秀!