我正在使用NSURL和NSData从我的JSON文件下载信息,然后将其放入我的应用程序的表视图中。信息包括用户的一些文本和图像。这一切都很好,除了视图需要大约15秒加载并且在出现之前似乎被冻结。我已经尝试了一切来尝试修复此问题,包括使用NSURL会话提到的解决方案,但我似乎无法为我正常工作。我已经发布了以下代码的重要部分。
有人有任何建议吗?
感谢。
- (void)viewDidLoad {
[super viewDidLoad];
[self.tableViewObject setTranslatesAutoresizingMaskIntoConstraints:NO];
//Do any additional setup after loading the view.
UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
[refreshControl addTarget:self action:@selector(handleRefresh:) forControlEvents:UIControlEventValueChanged];
[self.tableViewObject addSubview:refreshControl];
self.tableViewObject.estimatedRowHeight = 70.0;
self.tableViewObject.rowHeight = UITableViewAutomaticDimension;
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center addObserver:self selector:@selector(didShow:) name:UIKeyboardWillShowNotification object:nil];
[center addObserver:self selector:@selector(didHide) name:UIKeyboardWillHideNotification object:nil];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:@selector(dismissKeyboard)];
[self.view addGestureRecognizer:tap];
[self downloadmessages];
timer = [NSTimer scheduledTimerWithTimeInterval:10.0
target:self
selector:@selector(downloadmessages)
userInfo:nil
repeats:YES];
}
- (void)viewWillDisappear:(BOOL)animated {
[timer invalidate];
timer = nil;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [trackin count];
}
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath { return UITableViewAutomaticDimension; }
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"ChatCellTest";
static NSString *simpleTableIdentifier2 = @"ChatCellTest2";
if([[trackin[indexPath.row] valueForKey:@"senderID"] isEqual: @"me"]){
ChatCellTest *cell = (ChatCellTest *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nibArray = [[NSBundle mainBundle] loadNibNamed:@"ChatCellTest" owner:self options:nil];
cell = [nibArray objectAtIndex:0];
}
cell.background.layer.cornerRadius = 5;
cell.background.layer.masksToBounds = YES;
cell.message.text = [trackin[indexPath.row] valueForKey:@"message"];
cell.time.text = [trackin[indexPath.row] valueForKey:@"time"];
cell.avatar.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[[NSURL alloc]initWithString:mypic]]];
cell.avatar.layer.cornerRadius = cell.avatar.frame.size.height /2;
cell.avatar.layer.masksToBounds = YES;
return cell;
}
else{
ChatCellTest *cell = (ChatCellTest *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier2];
if (cell == nil)
{
NSArray *nibArray = [[NSBundle mainBundle] loadNibNamed:@"ChatCellTest2" owner:self options:nil];
cell = [nibArray objectAtIndex:0];
}
cell.background.layer.cornerRadius = 5;
cell.background.layer.masksToBounds = YES;
cell.message.text = [reversedArray[indexPath.row] valueForKey:@"message"];
cell.time.text = [reversedArray[indexPath.row] valueForKey:@"time"];
cell.avatar.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[[NSURL alloc]initWithString:otherpic]]];
cell.avatar.layer.cornerRadius = cell.avatar.frame.size.height /2;
cell.avatar.layer.masksToBounds = YES;
return cell;
}
}
-(void)downloadmessages
{
NSURL *blogURL = [NSURL URLWithString:[NSString stringWithFormat:@"https://tutific.udropoff.com/api/getmessages.php?conID=%@", subjects2]];
NSLog(@"URL = %@", blogURL);
NSData *jsonData = [NSData dataWithContentsOfURL:blogURL];
if(jsonData == nil)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Connection Error" message:@"Please check your internet connection and try again." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
return;
}
else{
NSError *error = nil;
dict = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
NSMutableArray *temp = [[dict objectForKey:@"tutific"]mutableCopy];
trackin = [temp copy];
NSUInteger *elements = [trackin count];
NSLog(@"Number of Shows : %d", elements);
if (elements == 0)
{
//[udropofftrack removeAllObjects];
trackin=nil;
[_tableViewObject reloadData];
// self.noorder.hidden=FALSE;
}
else{
NSString *success = [trackin[0] valueForKey:@"type"];
if ([success isEqual: @"error"])
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Connection Error" message:@"Your application failed to connect to uDropOff servers. Please update your application in the App Store." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
return;
}
}
}
[_tableViewObject reloadData];
}
@end