在iOS中下载JSON并使用它来绘制UI



我对iOS开发相对较新,我目前的项目需要我通过Rest API连接到一个网站,并使用从网站接收的JSON对象来填充我已经实现的自定义UITableViewController。我已经能够下载信息并使其正确显示在表上,但这需要我进入开始下载的页面,然后离开页面并再次输入,在此基础上填充所有单元格。我如何让我的应用程序加载这些信息而不离开页面?

My Custom UITableViewClass包含以下方法:

- (void)viewDidLoad {
    [super viewDidLoad];
    NSString *chuckFranklinConnection = @"http://chuckfranklinlaw.com/wp-json/posts?type=tribe_events";
    NSURLSession *session = [NSURLSession sharedSession];
    [[session dataTaskWithURL: [NSURL URLWithString:chuckFranklinConnection]
            completionHandler:^(NSData *data,
                            NSURLResponse *response,
                            NSError *error){
                //Complete Data Handling from Chuckfranklinlaw.com Here
                NSString *responseData = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
                NSError *e = nil;
                jsonArray = [NSJSONSerialization JSONObjectWithData: data options:NSJSONReadingMutableContainers error: &e];
                if(!jsonArray) {
                    NSLog(@"Error parsing JSON: %@", e);
                }else {
                    NSLog(@"Begin Successful Parse Readout -----------");
                    for(NSDictionary *item in jsonArray) {
                        NSLog(@"Item: %@", item);
                    }
                }
            }]
    resume];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    //UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:<#@"reuseIdentifier"#> forIndexPath:indexPath];
    // Configure the cell...
    EventTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"EventsCell"];
    if(cell == nil) {
        cell = [[EventTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"EventsCell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    //Formatting for each Cell Title
    cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
    cell.textLabel.numberOfLines = 0;
    cell.textLabel.text = [[jsonArray objectAtIndex:indexPath.row] objectForKey: @"title"];
    //Formatting for each Cell Detail
    NSString *startDate = [[jsonArray objectAtIndex:indexPath.row] objectForKey: @"StartDate"];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *date = [dateFormatter dateFromString:startDate];
    [dateFormatter setDateStyle:NSDateFormatterMediumStyle];
    [dateFormatter setTimeStyle:NSDateFormatterShortStyle];
    cell.data = [[jsonArray objectAtIndex:indexPath.row] objectForKey:@"ID"];
    cell.detailTextLabel.text = [dateFormatter stringFromDate:date];
    //Work On Implementing Image View for App in this section
    NSDictionary *imageInfo = [[jsonArray objectAtIndex:indexPath.row] objectForKey:@"featured_image"];
    NSDictionary *attachmentMeta = [imageInfo objectForKey:@"attachment_meta"];
    NSDictionary *sizes = [attachmentMeta objectForKey:@"sizes"];
    NSDictionary *thumbnail = [sizes objectForKey:@"thumbnail"];
    NSString *url = [thumbnail objectForKey:@"url"];
    NSLog(url);
    UIImage* image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:url]]];
    cell.imageView.image = image;
    return cell;
    [cell setNeedsDisplay];
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if([segue.identifier isEqualToString:@"showEventDetail"])
    {
        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
        DetailViewController *detailView = segue.destinationViewController;
        NSString *contentString = [[jsonArray objectAtIndex:indexPath.row] objectForKey:@"content"];
        //The Following code mutates the HTML Strings recieved from the server into NSStrings for displaying
        contentString = [contentString stringByReplacingOccurrencesOfString:@"&#8217;" withString:@"'"];
        contentString = [contentString stringByReplacingOccurrencesOfString:@"<p>" withString:@""];
        contentString = [contentString stringByReplacingOccurrencesOfString:@"</p>" withString:@"n"];
        detailView.content = contentString;
        detailView.title = [[jsonArray objectAtIndex:indexPath.row] objectForKey:@"title"];
        NSString *startDate = [[jsonArray objectAtIndex:indexPath.row] objectForKey:@"StartDate"];
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
        NSDate *date = [dateFormatter dateFromString:startDate];
        [dateFormatter setDateStyle:NSDateFormatterMediumStyle];
        [dateFormatter setTimeStyle:NSDateFormatterShortStyle];
        detailView.eventTime = [dateFormatter stringFromDate:date];
        //Pull Image from web and serve to next page
        NSDictionary *imageInfo = [[jsonArray objectAtIndex:indexPath.row] objectForKey:@"featured_image"];
        NSDictionary *attachmentMeta = [imageInfo objectForKey:@"attachment_meta"];
        NSDictionary *sizes = [attachmentMeta objectForKey:@"sizes"];
        NSDictionary *blogFull = [sizes objectForKey:@"blog-full"];
        NSString *url = [blogFull objectForKey:@"url"];
        detailView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:url]]];
    }
}

在您的完成块:[self.tableview reloadData]。文档。

实际上,你需要创建一个弱引用到self,然后在你的完成块中使用弱引用。

最新更新