【问题标题】:Load more cells "willDisplayCell" Problem加载更多单元格“willDisplayCell”问题
【发布时间】:2011-09-12 01:04:51
【问题描述】:

我的 willDisplayCell 有问题,它不会停止从 url 获取 Xml 我的代码:

- (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

【问题讨论】:

    标签: iphone xml uitableview load


    【解决方案1】:

    问题是你在tableView:willDisplayCell:中调用了reloadDatareloadData 将导致每个 UITableViewCell 再次被绘制,这意味着 willDisplayCell 将为屏幕上可见的每个单元格调用。在我看来,你在那里有一个无限循环。

    tableView:willDisplayCell: 绝对是进行 XML 解析的错误位置。 willDisplayCell 应该只用于准备 UI。我不能肯定地说,但也许对 XML 解析更好的地方是在您的视图控制器的 init 方法中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多