2011-09-12 51 views
1

我有一個問題,willDisplayCell它不會停止從URL中擷取XML 我的代碼:負載多種細胞「willDisplayCell」問題

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


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

static NSString *CellIdentifier = @"Cell"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

if (cell == nil) { 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
} 


if (indexPath.row == [itemsToDisplay count]) { 
    cell.textLabel.text = @"Load More..."; 


} else { 
    MWFeedItem *item = [itemsToDisplay objectAtIndex:indexPath.row]; 
    cell.textLabel.text = item.title; 
} 

    return cell; 
} 




- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
     if (indexPath.row == [itemsToDisplay count]) { 

    // Parse 
    NSURL *feedURL = [NSURL URLWithString:@"http://gdata.youtube.com/feeds/api/users/RayWilliamJohnson/uploads?v=2&alt=rss&sort_field=added&start-index=21&max-results=20"]; 
    feedParser = [[MWFeedParser alloc] initWithFeedURL:feedURL]; 
    feedParser.delegate = self; 
    feedParser.feedParseType = ParseTypeFull; // Parse feed info and all items 
    feedParser.connectionType = ConnectionTypeAsynchronously; 
    [feedParser parse]; 



    [self.tableView reloadData]; 
}  
} 

它的樣子,他是實物循環,這並不停止獲取xml

那麼我該如何解決這個問題,我只會打一次電話?

TNX

回答

1

的問題是,你叫的tableView內reloadData:willDisplayCell:reloadData將導致每個UITableViewCell再次繪製,意味着willDisplayCell將被調用每個將在屏幕上可見的單元格。對我來說就像你在那裏有一個無限循環。

tableView:willDisplayCell:絕對是做這個XML解析的錯誤地方。 willDisplayCell應該只是用來準備UI。我不能肯定地說,但也許更好的地方是在你的視圖控制器的init方法中進行XML解析。