【问题标题】:UITableView only shows data from JSON when I scroll up and downUITableView 仅在我上下滚动时显示来自 JSON 的数据
【发布时间】:2012-03-22 21:57:22
【问题描述】:

UITableView 仅在我上下滚动时显示来自 JSON 数组的数据。当我加载表格视图时,它显示单元格,但它们是空白的,当我上下滚动时,它开始显示数据。

如何在不滚动的情况下显示带有数据的单元格?

- (void)requestFinished:(ASIHTTPRequest *)request
{    
    if (request.responseStatusCode == 400) {
        NSLog( @"Code already used");

    } else if (request.responseStatusCode == 403) {
        NSLog( @"Code already used");

    } else if (request.responseStatusCode == 200) {
        NSLog(@"%@",[request responseString]);
        NSString *response = [request responseString];
        const char *convert = [response UTF8String];
        NSString *responseString = [NSString stringWithUTF8String:convert];
        responseArray  = [responseString JSONValue];
        Nrows = [responseArray count];

    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

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

    NSDictionary *dict = [responseArray objectAtIndex:indexPath.row];
    cell.detailTextLabel.text = [dict objectForKey:@"allfeeds"];
    cell.textLabel.adjustsFontSizeToFitWidth = YES;
    cell.textLabel.font = [UIFont systemFontOfSize:12];
    cell.textLabel.minimumFontSize = 10;
    cell.textLabel.numberOfLines = 4;
    cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
    cell.textLabel.text = [dict objectForKey:@"allfeeds2"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    return cell;
}   

【问题讨论】:

    标签: iphone ios uitableview


    【解决方案1】:

    完成请求并设置数组后,请务必致电 [self.tableView reloadData];

    出于性能和动画目的,表视图不会在源更改时重新加载其数据。

    【讨论】:

      【解决方案2】:

      而不是异步启动请求(非阻塞)

      [request startAsynchronously];
      

      尝试同步(阻塞)

      [request startSynchronously];
      

      【讨论】:

      • 神奇地完成了! :) 非常感谢它的工作.. 现在 [requeststartSynchronously];已经解决了
      【解决方案3】:

      我有一个类似的问题,不久前在 Apple 的开发者论坛上得到了答案,这解决了我的问题:

      根据您的描述,我猜问题更可能出在 cellForRowAtIndexPath 或您对单元格进行实际配置的任何地方,而不是您发布的代码中。当 dequeueReusableCellWithIdentifier 给你一个现有的单元格,但你没有完全重置它的现有状态时,可能会发生像你这样的问题。

      完整的问题和答案可以在这里找到(需要登录):https://devforums.apple.com/message/502483#502483

      【讨论】:

      • 你能告诉我如何重置它的状态吗?
      • 您必须更改单元格中的每一点信息。假设您有一个标题、一个副标题和一个 accessoryType.UITableViewCellAccessoryCheckmark,因此对于您绘制的每个单元格,您都必须修改这三个单元格,即使其中一个对于所有单元格都相同,您只需重写它。
      • 但在我的情况下,我从 json 获取 NSArray,并且每个单元格中有两个文本字段,例如文本标签和详细信息文本。
      • NSString* CellIdentifier = [NSString stringWithFormat:@"Cell%d",indexPath.row]; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if(cell == nil){ cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; }
      【解决方案4】:

      这些问题通常是由 UITableView 缓存引起的,它使用相同的 CellIdentifier(在您的情况下 - @“Cell”)将他可用的缓存 UITableViewCell 出列。

      尝试为表格中的每个单元格创建一个单元格标识符。您可以通过以下方式使用 NSIndexPath 参数创建此差异:

      NSString* cellIdentifier = [NSString stringWithFormat:@"Cell%d",indexPath.row];
      

      当然,假设您在加载数据后没有更改单元格的内容。

      【讨论】:

      • 我做到了,但没有运气,当我加载表格视图时,它显示为空白,并且随着滚动它开始向我显示值,如果我再次上升,它开始向我显示其余的值,我我正在异步加载它。请推荐一些东西
      • 当我向下滚动它的 statrd 向我显示值时,它加载为空白。
      猜你喜欢
      • 1970-01-01
      • 2015-05-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-11
      • 2021-03-24
      • 1970-01-01
      相关资源
      最近更新 更多