1

我是相当新的 iPhone 开发,我有一个问题。经过几个小时的搜索和挖掘代码,我可以理解问题的根源(希望)但不知道如何解决它。问题是 tableView 单元在解析器完成之前加载,因此“[array count]”为 nil。当我手动设置“部分中的行数”时,表格确实出现了,但只是在几秒钟后上下滚动。

我有 2 个类,一个用于 XMLParser,一个用于显示 tableView。根据我在打开这个问题之前读到的内容,我需要重新加载 tavleview 数据,但因为我有 2 个不同的类,我不知道该怎么做。有任何想法吗?

非常感谢!

这是我的 XMLParser 代码:

- (void)parserDidStartDocument:(NSXMLParser *)parser
{ 
    self.titles = [[NSMutableArray alloc]init];
    self.descriptions = [[NSMutableArray alloc]init];
    self.links = [[NSMutableArray alloc]init];
    self.pubDate = [[NSMutableArray alloc]init]; 
}

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

}

BOOL isItem = NO;
BOOL isTitle = NO;
BOOL isDesription = NO;
BOOL isImg = NO;
BOOL isPubDate = NO;

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
    if ([elementName isEqualToString:@"item"]) {
        isItem = YES;
    }

    if ([elementName isEqualToString:@"title"]) {
        isTitle=YES;
        self.titlesString = [[NSMutableString alloc]init];
    }

    if ([elementName isEqualToString:@"description"]) {
        isDesription = YES;
        self.descriptionString = [[NSMutableString alloc]init];;
        self.data = [NSMutableData data];
    }
    if ([elementName isEqualToString:@"pubDate"]) {
        isPubDate = YES;
        self.pubDateString = [[NSMutableString alloc]init];
    }

}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
    if(isItem && isTitle){
        [self.titlesString appendString:string];
    }
    if (isItem && isDesription) {
        [self.descriptionString appendString:string];
    }
    if (isItem && isPubDate) {
        [self.pubDateString appendString:string];
    }


}

- (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock
{
    if (self.data)
        [self.data appendData:CDATABlock];

}


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



    if ([elementName isEqualToString:@"item"]) {
        isItem = NO;



        [self.titles addObject:self.titlesString];
        [self.descriptions addObject:self.descriptionString];
        [self.pubDate addObject:self.pubDateString];
        NSLog(@"%@,%@,%@,",self.titlesString,self.descriptionString,self.pubDate);

    }

    if ([elementName isEqualToString:@"title"]) {
        isTitle=NO;

    }
    if ([elementName isEqualToString:@"description"]) {
        isDesription = NO;


        if ([self.data length] > 0)
        {
            NSString *htmlSnippet = [[NSString alloc] initWithData:self.data encoding:NSUTF8StringEncoding];
            NSString *imageSrc = [self firstImgUrlString:htmlSnippet];
            [self.links addObject:imageSrc];
        }

        self.data = nil;
    }

    if([elementName isEqualToString:@"pubDate"])
        isPubDate = NO;
}

- (NSString *)firstImgUrlString:(NSString *)string
{
    NSError *error = NULL;
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"(<img\\s[\\s\\S]*?src\\s*?=\\s*?['\"](.*?)['\"][\\s\\S]*?>)+?"
                                                                           options:NSRegularExpressionCaseInsensitive
                                                                             error:&error];

    NSTextCheckingResult *result = [regex firstMatchInString:string
                                                     options:0
                                                       range:NSMakeRange(0, [string length])];

    if (result)
        return [string substringWithRange:[result rangeAtIndex:2]];

    return nil;

}

这是我的 show tableView 代码:

- (void)viewDidLoad
{
    [super viewDidLoad];

       self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"menuButton.png"] style:UIBarButtonItemStyleBordered target:self.viewDeckController action:@selector(toggleLeftView)];

    NSURL *url;

    if (!self.isGetLink)
        url = [NSURL URLWithString:@"http://www.ynet.co.il/Integration/StoryRss2.xml"];
    else
        url = [NSURL URLWithString:self.linkForParsingString];


    if (!self.xmlParser) {
        self.xmlParser = [[XMLparser alloc]init];

        [self.xmlParser LoadXMLWithURl:url];
    }

    self.isGetLink = NO;
}


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

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    // Return the number of sections.
    return 1;
}
- (void)registerNib:(UINib *)nib forCellReuseIdentifier:(NSString *)identifier
{

}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return [self.xmlParser.titles count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{


    static NSString *CellIdentifier = @"CustomCell";
    CustomCell *cell = (CustomCell*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];

    }

    cell.lblTitle.text = [self.xmlParser.titles objectAtIndex:indexPath.row];
    cell.lblTitle.font = [UIFont boldSystemFontOfSize:[UIFont systemFontSize]];
    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:[self.xmlParser.links objectAtIndex:indexPath.row]]];
    cell.imgView.image = [UIImage imageWithData:data];


    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 82;
}


#pragma mark - Table view delegate
NSString *curnentDes;
NSString *currentTitle;
NSString *currentPubDate;

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    ReadViewController *rvc = [[ReadViewController alloc]initWithNibName:@"ReadViewController" bundle:nil];
    curnentDes = [self.xmlParser.descriptions objectAtIndex:indexPath.row];
    currentTitle = [self.xmlParser.titles objectAtIndex:indexPath.row];

    currentPubDate = [self.xmlParser.pubDate objectAtIndex:indexPath.row];
    NSDateFormatter *inputFormatter = [[NSDateFormatter alloc]init];

    [inputFormatter setDateFormat:@"EEE, dd MMM yyyy HH:mm:ss Z"];
    NSDate *inputDate = [inputFormatter dateFromString:currentPubDate];
    NSDateFormatter *outputFormatter = [[NSDateFormatter alloc]init];
    [outputFormatter setDateFormat:@"dd/MM/yyyy HH:mm"];
    NSString *outputDate = [outputFormatter stringFromDate:inputDate];

    rvc.description = curnentDes;
    rvc.stitle = currentTitle;
    rvc.pubDate = outputDate;


    [self.navigationController pushViewController:rvc animated:YES];

}
4

2 回答 2

1

解析完成后非常简单的重新加载表,你很高兴。滚动后出现数据的原因是您的数组稍后会更新,然后调用 cellForRowAtIndex 并且数组具有内容,因此您可以看到行。

注意:每行都会调用 cellForRow(希望您知道这一点)。

使用通知:

在您的解析器类中

- (void)parserDidEndDocument:(NSXMLParser *)parser
{ 
  [[NSNotificationCenter defaultCenter ]postNotificationName:@"parsingComplete" object:nil];
}

在您的表视图类的 ViewDidLoad 中添加此代码并在您的类中的任何位置添加其他函数:

你的 viewDidLoad(已编辑)

- (void)viewDidLoad
{
[super viewDidLoad];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(parsingComplete) name:@"parsingComplete" object:nil];
 self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"menuButton.png"] style:UIBarButtonItemStyleBordered target:self.viewDeckController action:@selector(toggleLeftView)];

NSURL *url;

if (!self.isGetLink)
    url = [NSURL URLWithString:@"http://www.ynet.co.il/Integration/StoryRss2.xml"];
else
    url = [NSURL URLWithString:self.linkForParsingString];


if (!self.xmlParser) {
    self.xmlParser = [[XMLparser alloc]init];

    [self.xmlParser LoadXMLWithURl:url];
}

self.isGetLink = NO;
}

//通知处理函数:

-(void)parsingComplete
 {
  NSLog(@"parsing results: %@",self.xmlParser.titles); 

  //change your tableViewName

  [yourTableView reloadData];
 }

//删除通知:

 -(void)dealloc
 {
 [[NSNotificationCenter defaultCenter] removeObserver:self];
 }
于 2013-04-05T06:14:42.007 回答
0

解析完成后重新加载表。使用委托方法从 didEndElement 方法重新加载表视图。尝试一些这样的想法。

-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
    if([elementName isEqualToString:@"Final Element"])
    {

     // Reload your table View using delegates 

    }
}
于 2013-04-05T06:50:19.730 回答