简体   繁体   English

iPhone:在UITableView中平滑滚动

[英]iPhone: smooth scroll through the UITableView

I have a UITableView, and custom cells on it. 我有一个UITableView,以及自定义单元格。 On cell I have a UILabel, but before I set text to UILabel I did really hard work on text...like find the text in another text, highlight some words on it, and only then I set it to label. 在单元格上,我有一个UILabel,但在将文本设置为UILabel之前,我确实在文本上做了艰苦的工作……就像在另一个文本中查找文本,突出显示其中的某些单词,然后才将其设置为标签。 So when I scroll my list, it has delay because of this hard work. 因此,当我滚动列表时,由于此辛苦工作,它会延迟。 Any idea how to improve performance ? 任何想法如何提高性能? Maybe to do all hard work in another thread ?? 也许在另一个线程中做所有艰苦的工作?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:
        (NSIndexPath *)indexPath {
static NSString *customCellIdentifier =
    @"CellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
    customCellIdentifier];
if (cell == nil) {
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomTableRow"
        owner:self options:nil];
    if (nib.count > 0) {
        cell = self.customTableRow;
    }
}

   self.myLabel.text = [self giveMeTheTextThatINeed];

return cell;
}

[self giveMeTheTextThatINeed] - did a hard work on text that takes some time. [self GiveMeTheTextThatINeed]-对需要花费一些时间的文本进行了艰苦的工作。

Make a new thread for every cell. 为每个单元格创建一个新线程。 this thread calls [self giveMeTheTextThatINeed:indexPath], and resets the label(s) in the cell. 此线程调用[self GiveMeTheTextThatINeed:indexPath],并重置单元格中的标签。 I'm assuming you can't get your data any faster, so you want to maintain the scrolling in the table and spin the hard work out to the thread. 我假设您无法更快地获取数据,因此您想保持表中的滚动并将辛苦的工作转交给线程。 When the thread is finished, update the cell. 线程完成后,更新单元格。 You see this a lot in cells with a thumbnail image where the thumbnail only gets uploaded after a while, and is blank or has a placeholder there first. 您会在带有缩略图图像的单元格中看到很多,其中缩略图仅在一段时间后才上传,并且为空白或先有占位符。

Any way for you to precompute the values you'll need? 有什么方法可以预先计算所需的值? In other words, start doing your "hard work" (in another thread) when the app starts, and store it somewhere so that, if it's ready, you can just grab it when the table view asks for it. 换句话说,在应用程序启动时开始进行“艰苦的工作”(在另一个线程中),并将其存储在某个位置,以便在就绪时可以在表视图要求时进行抓取。 It's hard to answer without more detail about what the hard work is and how much data we're talking about. 如果没有更多关于工作量以及我们正在谈论的数据的详细信息,很难回答。

I don't know about just doing the hard work on another thread as you suggested, since you still have to give something to tableView:cellForRowAtIndexPath: . 我不知道像您建议的那样仅在另一个线程上进行艰苦的工作,因为您仍然必须给tableView:cellForRowAtIndexPath: 一些东西 I suppose you could return some kind of template cell at first, and then update it with reloadRowsAtIndexPaths:withRowAnimation: when the hard work is done. 我想您可以先返回某种模板单元格,然后在完成艰苦的工作后,使用reloadRowsAtIndexPaths:withRowAnimation:对其进行更新。

I believe the method giveMeTheTextThatINeed needs to take the current cell as one of the parameters (or another parameter dependent on the cell content), eg: [self giveMeTheTextThatINeed:indexPath] . 我认为方法giveMeTheTextThatINeed需要将当前单元格作为参数之一(或另一个取决于单元格内容的参数),例如: [self giveMeTheTextThatINeed:indexPath] Otherwise you could store the text as an instance variable and set it in all cells from that variable. 否则,您可以将文本存储为实例变量,并在该变量的所有单元格中进行设置。

So, with that in mind, the easiest way is to store the result of the computation in an additional dictionary, where indexPath (or the other parameter) would be the key: 因此,考虑到这一点,最简单的方法是将计算结果存储在其他字典中,其中indexPath(或其他参数)将是关键:

self.myLabel.text = [self->myDictionary objectForKey:indexPath];

Now, you could either pre-populate that dictionary before the cells are drawn (eg in viewWillAppear), or cache them once they are calculated so that they are not recalculated when the cells are scrolled, eg: 现在,您可以在绘制单元格之前预先填充该字典(例如,在viewWillAppear中),或者一旦计算它们就对其进行缓存,以便在滚动单元格时不重新计算它们,例如:

NSString* calculatedText = [self->myDictionary objectForKey:indexPath];
if(calculatedText == nil)
{
    calculatedText = [self giveMeTheTextThatINeed:indexPath];
    [self->myDicationary setValue:calculatedText forKey:indexPath];
}
self.myLabel.text = calculatedText;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM