【问题标题】:Reloading content using NSXMLParser and UITableView halts app for seconds使用 NSXMLParser 和 UITableView 重新加载内容会使应用暂停几秒钟
【发布时间】:2012-07-13 22:10:52
【问题描述】:

我正在开发我的第一个应用程序,它从网络获取 XML 提要。

XML 第一次加载到TableView 时,一切正常,但是当我想通过点击按钮重新加载数据时,它会暂停几秒钟,然后重新加载 XML 和表格视图。我怎样才能避免这个暂停时间?

当我点击表格单元格以转到下一个 TableViewController 时,它也会在加载 XML 提要以显示列表时暂停几秒钟。

我的应用类似于 iPhone 的原生博客应用,有类别和子类别。

【问题讨论】:

  • 您的表格视图是否包含图像?
  • 是的,我也在将图像加载到表格视图中

标签: iphone objective-c uitableview nsxmlparser


【解决方案1】:

参考以下流程。


@interface YourViewController : UITableViewController<NSURLConnectionDelegate,NSXMLParserDelegate>
//some declartion property...
@end

@implementation YourViewController

- (void)viewDidLoad
  {
    [super viewDidLoad];

    isParsingStart = NO;
    webData = [[NSMutableData alloc] init];
    connection = [[NSURLConnection alloc] initWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:YOUR_XML_URL] cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:20] delegate:self];
  }

 - (void)tappingButton:(UIButton *)sender
  {   
    if(isParsingStart)
    {
      //show a alert view... or other do stuff 
      return;
    }
     webData = [[NSMutableData alloc] init];
     connection = [[[NSURLConnection alloc] initWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:YOUR_XML_URL] cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:20] delegate:self] autorelease];
  }

  - (void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
  {
     [webData appendData:data];
  }

 - (void) connectionDidFinishLoading:(NSURLConnection *)connection
  {

     parser = [[NSXMLParser alloc] initWithData:webData];
     parser.delegate = self;
     if([parser parse])
     {
       isParsingStart = NO;
       NSLog(@"The XML is Parsed.");
       [[self tableView] reloadData];
     }
     else 
     {
        isParsingStart = NO;
        NSLog(@"Failed to parse the XML");
     }

       [webData setData:nil];
  }

- (void)parserDidStartDocument:(NSXMLParser *)parser
  {
      isParsingStart = YES;
  }

  - (void)parserDidEndDocument:(NSXMLParser *)parser
  {
      //do stuff
  }

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

  - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
  {
     // do stuff
  }

           .
           .
           .

【讨论】:

  • 感谢它的真正帮助,工作得很好,现在只需要做一点微调。
  • 嗨位图代码现在工作得很好,如果我从管理员发布一些东西,它不会重新加载刷新,对于新的更改,我必须再次加载应用程序。
  • 惊人的答案!无法相信这是一个多么出色的解决方案:)非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-03
  • 2015-08-31
  • 2023-01-17
  • 1970-01-01
相关资源
最近更新 更多