ios - 向后滚动时不要重新加载单元格

标签 ios objective-c uitableview reload

我有一个在网络上找不到的问题。我必须显示一些从 Nib 加载的自定义单元格。我在一个单独的线程上从我的数据库下载信息并将其分配到一个新的 MutableArray 上。 我还有分配在单独数组中并在必要时调用的图像,但不是从网上下载的。 向下滚动时我的表格 View “滞后”,那是(我猜)因为它必须将东西放在正确的单元格上,但是当我向后滚动时它再次滞后并再次重新加载所有信息。 我看到 Facebook 应用程序在向下滚动时加载单元格(但不是那么慢),而当向后滚动时它不会重新加载任何内容并且单元格已经加载(无论有多少)。我怎么能做这样的事情?我的 table 很慢,我(目前)只有 3 个单元格。但是当应用程序完成时,这些单元格将是 100 或 200。 谁能帮帮我?

这是我的代码:(在 viewDidLoad 上)

 NSString *strURL = [NSString stringWithFormat:@"..(myurl)..];
// to execute php code
NSData *dataURL = [NSData dataWithContentsOfURL:[NSURL URLWithString:[strURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];
// to receive the returend value
NSString *strResult = [[NSString alloc] initWithData:dataURL encoding:NSUTF8StringEncoding];

if(![strResult isEqualToString:@"0"]) {
    posts = [strResult componentsSeparatedByString:@"^"];
}

immProfilo = [NSMutableArray array];

for (int i = 0; i < [posts count]; i++) {
    NSArray *datiPost = [[posts objectAtIndex:i] componentsSeparatedByString:@"/"];
    FBProfilePictureView *fotoProfiloFB = [[FBProfilePictureView alloc] initWithFrame:CGRectMake(22, 22, 55, 55)];
    fotoProfiloFB.profileID = [datiPost objectAtIndex:1];
    [immProfilo addObject:fotoProfiloFB];
}

[self.postTab reloadData];

那是我的表格 View 代码:

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

return [posts count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *CellIdentifier = [NSString stringWithFormat:@"%ld_%ld",(long)indexPath.section,(long)indexPath.row];

    PostTabCell *cell = (PostTabCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {

        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"PostTabCell" owner:self options:nil];
        cell = [topLevelObjects objectAtIndex:0];
    }

    NSString *annuncio = [posts objectAtIndex:indexPath.section];
    datiAnnuncio = [annuncio componentsSeparatedByString:@"/"];

   [cell addSubview:[immProfilo objectAtIndex:indexPath.section]];

    cell.nome.text = [datiAnnuncio objectAtIndex:0];
    cell.location.text = [self getAddressFromLatLon:[[datiAnnuncio objectAtIndex:2] floatValue] withLongitude:[[datiAnnuncio objectAtIndex:3] floatValue]];
    cell.testoPost.text = [datiAnnuncio objectAtIndex:4];

    return cell;
}

最佳答案

您的 TableView 如此缓慢的原因是因为每次 TableView 向委托(delegate)请求一个单元格(您的 cellForRowAtIndexPath 方法)时,您使用 getAddressFromLatLon< 执行同步网络请求,阻塞主线程。

一个直接的解决方法是-至少-将这些文本存储在某种数组中,这样下次表格 View 请求相同的单元格时,您就不必执行网络再次请求。

这将解决当您向上滚动时 tableview 滞后的问题,但当您第一次向下滚动时则不会。一条您始终认为正确的通用规则是,您不应该因网络请求而阻塞主线程。

您现在有两个选择:在辅助线程的最开始加载所有这些文本,同时显示微调器(简单但会出现几个问题,例如它不会随着单元格的数量很好地扩展).或者您必须设计一个异步加载器,它会显示一个占位符字符串,例如 loading address...,直到该地址被实际加载。

Totumus 的回答也有一定的道理,但这不是导致你延迟的主要原因(尽管一旦你的细胞数量增加,这将是一个大问题)。

关于ios - 向后滚动时不要重新加载单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19267382/

相关文章:

iphone - UIAlertView 不显示完整消息

ios - KVO 通知间歇性崩溃

ios - 来自一个来源的多个 View 和 UITables,如何填充它们?

ios - UITableViewCell 复选标记在点击时打开和关闭

ios - 在 Xcode 中找不到社交框架

ios - "slicing"是否可以附加到 iOS 应用程序 Assets 的视频?

iOS POST 到 Google 表单(电子表格)

ios - 如何在 Objective C 的 Dynamic UITableViewCell 中隐藏特定的行按钮?

ios - 如何在 Swift 3 中使用 unsafeDowncast

ios - 子类化 NSInputStream,重写委托(delegate)?