我目前正在尝试使用存储在我的Parse服务器上的信息填充tableView(用户名,描述,个人资料图片)
目前......当我打开tableview时,我没有显示任何信息,但如果我点击后退并重新打开表格,我的所有信息都显示出来,我不确定为什么会这样。
首先从Parse获取所需的所有信息并将它们存储到数组中,如下所示:
let query = PFQuery(className: institutionTitle)
query.findObjectsInBackground {
(objects, error) in
if error == nil {
// The find succeeded.
// Do something with the found objects
if let objects = objects {
for object in objects {
self.postGrabber.append(object)
self.pSenderUsername.append(object["sender"] as! String)
self.pPostBody.append(object["textPost"] as! String)
}
}
} else {
// Log details of the failure
print("Error: \(error!)")
}
}
我目前正在使用viewDidLoad()
和viewWillAppear()
到reloadData()
然后我在viewDidLoad()
函数中设置表视图的委托和数据源。
然后我使用cellForRowAtIndexPath方法设置表格中的每个单元格:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: UniversityFeedCellTableViewCell = tableView.dequeueReusableCell(withIdentifier: "postCell", for: indexPath) as! UniversityFeedCellTableViewCell
cell.sender.text = pSenderUsername[indexPath.section]
cell.senderPost.text = pPostBody[indexPath.section]
cell.profilePicture.layer.cornerRadius = cell.profilePicture.frame.size.width / 2
cell.profilePicture.layer.borderColor = UIColor.orange.cgColor
cell.backgroundColor = UIColor.white
return cell
}
我正在寻找一些有关如何让用户等到所有数据都已成功加载并已填充的信息。会感激一些帮助。
提前致谢,随时向我索取更多信息。
答案 0 :(得分:0)
当你从服务器获取json时需要设置回调方法,然后在结果获取时你需要调用reload方法并且你可以设置指标。使用fetch完成的回调方法将重新加载数据
如果您需要一些代码示例,那么我可以提及代码听取
答案 1 :(得分:0)
您需要在从服务器获取所有内容后在表视图中重新加载数据......这样的事情可以解决这个问题:
let query = PFQuery(className: institutionTitle)
query.findObjectsInBackground {
(objects, error) in
if error == nil {
// The find succeeded.
// Do something with the found objects
if let objects = objects {
for object in objects {
self.postGrabber.append(object)
self.pSenderUsername.append(object["sender"] as! String)
self.pPostBody.append(object["textPost"] as! String)
}
dispatch_async(dispatch_get_main_queue(), {
self.tableView.reloadData() // Add this line and it should work
})
}
} else {
// Log details of the failure
print("Error: \(error!)")
}
}
如果您想在每次出现视图控制器时加载数据,请在viewWillAppear(),otherWise中进行加载,如果您需要首次加载数据,则在viewDidLoad()
中加载数据答案 2 :(得分:0)
首先你需要在你的.h文件中添加这个回调块//回调块
void (^successCallback)(id response);
void (^failCallback)(NSError *error);
+(BOOL)CallService3:(NSString*)serviceName postData:(NSString*)params qString:(NSString*)QueryStr callBackBlock:(void (^)(id response))responeBlock;
在您的实现文件中意味着.m文件创建方法用于调用web api或Web服务
+(BOOL)CallService3:(NSString*)serviceName postData:(NSString*)params qString:(NSString*)QueryStr callBackBlock:(void (^)(id response))responeBlock{
NSString *jsonString =params;//if you have dictionary then [params JSONRepresentation];
NSString *post = [NSString stringWithFormat:@"json=%@", jsonString];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:NO];
NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
sessionConfiguration.HTTPAdditionalHeaders = @{
@"api-key" : @"API_KEY"
};
NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfiguration];
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@",baseURLS,serviceName]];
NSLog(@"url %@",[url absoluteString]);
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
request.HTTPMethod = @"GET";
NSURLSessionDataTask *postDataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSDictionary* jsonObject = [NSJSONSerialization JSONObjectWithData:data
options:kNilOptions
error:&error];
responeBlock(jsonObject);
}];
[postDataTask resume];
return TRUE;
}
创建此方法后,您必须将其调用为回调函数
-(void)callWebApi{
//here you can call indicator if you need this class i can share with you
// [self startActivityIndicatorInView:self.view withMessage:@"please wait"];
[Connection CallService3:WebApi_Category postData:jsonstring qString:str callBackBlock:^(id response){
// NSLog(@"param .. %@",response);
// NSLog(@"%@",[response allKeys]);
//NSLog(@"param .. %@",response);
jsonArray=[[NSDictionary alloc]initWithDictionary:response];
dispatch_async(dispatch_get_main_queue(), ^(void){
//stop indicator here
// [self stopActivityIndicatorInView:self.view];
[self call_your_method_populating_table:[jsonArray objectForKey:kModel]];
//now call reload tableview
[_tableview reloadData];
});
}];
}