【问题标题】:pull to refresh uitableview json parsing data拉动刷新uitableview json解析数据
【发布时间】:2014-08-17 10:39:18
【问题描述】:

我在我的 rss 应用程序中使用 json rss 提要解析,我有表格视图加载标题、缩略图并将标题链接发送到 Web 视图,每个都在它自己的 nsmutable 数组中,但是,我添加了一个拉动刷新在我的应用程序中,我遵循了很多教程,我添加了代码,当我拉动时,我得到了纺车,并且加载和停止,但是我的新提要没有加载,我如何确保解析方法被调用,不只是刷新表发生,下面是我的代码:

#import "APPMasterViewController.h"
#import "APPDetailViewController.h"
#import <SDWebImage/UIImageView+WebCache.h>

#define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]



@interface APPMasterViewController () {
    NSXMLParser *parser;
    NSMutableArray *feeds;
    NSMutableDictionary *item;
    NSMutableString *title;
    NSMutableString *link;
    NSMutableString *thumbnail;
    NSString *element;
    UIRefreshControl *refreshControl;
}


@end

@implementation APPMasterViewController

- (void)awakeFromNib
{
    [super awakeFromNib];
    [[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
    [[UINavigationBar appearance] setBarTintColor:UIColorFromRGB(0xcf1717)];
    [[UINavigationBar appearance] setBarStyle:(UIBarStyleBlackTranslucent)];

}


- (void)viewDidLoad {
    [super viewDidLoad];
    feeds = [[NSMutableArray alloc] init];
    NSURL *url = [NSURL URLWithString:@"http://icuore.ly/category/ipad/feed/"];
    parser = [[NSXMLParser alloc] initWithContentsOfURL:url];
    [parser setDelegate:self];
    [parser setShouldResolveExternalEntities:NO];
    [parser parse];


    refreshControl = [[UIRefreshControl alloc] init];

    [refreshControl addTarget:self action:@selector(refresh) forControlEvents:UIControlEventValueChanged];

    [self.tableView addSubview:refreshControl];
}

- (void)refresh {

    [self.tableView reloadData];
    [refreshControl endRefreshing];

    NSLog(@"fetching data from the server");
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table View

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return feeds.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    cell.textLabel.text = [[feeds objectAtIndex:indexPath.row] objectForKey: @"title"];


    //cell image set
    NSString *imageStr = [[feeds objectAtIndex:indexPath.row] objectForKey: @"thumbnail"];
    NSString *trimmedString = [imageStr stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    NSString *string1=[trimmedString stringByReplacingOccurrencesOfString:@"/n" withString:@""];
    NSURL *url = [NSURL URLWithString:string1];
//    NSData *data = [NSData dataWithContentsOfURL:url];
//    UIImage *newImage = [UIImage imageWithData:data];
    //cell.imageView.image = newImage;

    CALayer * roudning = [cell.imageView layer];
    [roudning setMasksToBounds:YES];
    [roudning setCornerRadius:30.0];

    [cell.imageView setImageWithURL:url
    placeholderImage:[UIImage imageNamed:@"icuore_logo.jpg"]];
    return cell;
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {

    element = elementName;

    if ([element isEqualToString:@"item"]) {

        item    = [[NSMutableDictionary alloc] init];
        title   = [[NSMutableString alloc] init];
        link    = [[NSMutableString alloc] init];
        thumbnail    = [[NSMutableString alloc] init];

    }

}


- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {

    if ([elementName isEqualToString:@"item"]) {

        [item setObject:title forKey:@"title"];
        [item setObject:link forKey:@"link"];
        [item setObject:thumbnail forKey:@"thumbnail"];

        [feeds addObject:[item copy]];

    }

}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {

    if ([element isEqualToString:@"title"]) {
        [title appendString:string];
    } else if ([element isEqualToString:@"link"]) {
        [link appendString:string];
    } else if ([element isEqualToString:@"thumbnail"]) {
        [thumbnail appendString:string];
    }

}

- (void)parserDidEndDocument:(NSXMLParser *)parser {

    [self.tableView reloadData];

}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
   if ([[segue identifier] isEqualToString:@"showDetail"]) {
//        
        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
        NSString *string = [feeds[indexPath.row] objectForKey: @"link"];

       NSString *trimmedString = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
       NSString *string1=[trimmedString stringByReplacingOccurrencesOfString:@"/n" withString:@""];

        NSLog(@"you clicked %@", [feeds[indexPath.row] objectForKey: @"link"]);
        [[segue destinationViewController] setUrl:string1];
          }

    //my modified passing


}

@end

【问题讨论】:

    标签: json uitableview pull-to-refresh


    【解决方案1】:

    我找到了答案,我必须清空表格视图,然后重新调用 nsurl,重新加载数据并结束刷新,现在它可以正常工作了 这是大家的代码:

    - (void)refresh {
        [feeds removeAllObjects];
    
        NSURL *url = [NSURL URLWithString:@"http://icuore.ly/category/ipad/feed/"];
        parser = [[NSXMLParser alloc] initWithContentsOfURL:url];
        [parser setDelegate:self];
        [parser setShouldResolveExternalEntities:NO];
        [parser parse];
        [self.tableView reloadData];
        [refreshControl endRefreshing];
    
        NSLog(@"fetching data from the server");
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-29
      • 2016-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多