为什么当我滚动时我的 tableview 会抖动
Why is my tableview jittery when I scroll
正在使用以下代码设置 tablview 单元格...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
MyTableviewcell *cell = [tableView dequeueReusableCellWithIdentifier:@"mytableviewcell" forIndexPath:indexPath];
cell.titleLabel.text = [self.data[indexPath.row] objectForKey:@"node_title"];
cell.taxonomy1Label.text = [self.data[indexPath.row] objectForKey:@"group"];
@try {
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:[self.data[indexPath.row] objectForKey:@"image"]]];
cell.thumbnailImageView.image = [UIImage imageWithData:imageData];
}
@catch (NSException * e) {
NSLog(@"Exception: %@", e);
}
return cell;
}
try/catch
只是因为它可能有也可能没有图像,但它甚至在我将其放入之前就已经发生了。似乎在使单元格出队时出现了某种问题。有什么想法吗?
您试图获取图像阻碍了主线程。将您的图像调用放在单独的线程中。
如果您熟悉使用第三方库或 CocoaPods,但这是一个常见问题,我推荐使用 https://github.com/rs/SDWebImage or https://github.com/AFNetworking/AFNetworking,它有 UIImageView 类别方法来处理从 URL 在后台,而不是在主线程中。
例如使用 SDWebImage:
[cell.thumbnailImageView sd_setImageWithURL:yourImageURL];
该方法将在后台获取图像,不会阻塞主线程,不会使您的 UITableView 抖动。
如前所述,此调用会阻塞主线程:
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:[self.data[indexPath.row] objectForKey:@"image"]]];
没有提到的是处理此类工作的正确方法。
您不能只将以上内容包装在 GCD 调用中并期望一切正常。
您需要延迟加载图像并在适当的时候将它们填充到 tableView 上。
处理这个问题最优雅的方法是编写您自己的 NSOperation
.
Here是教程,有问题可以私信我。
正在使用以下代码设置 tablview 单元格...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
MyTableviewcell *cell = [tableView dequeueReusableCellWithIdentifier:@"mytableviewcell" forIndexPath:indexPath];
cell.titleLabel.text = [self.data[indexPath.row] objectForKey:@"node_title"];
cell.taxonomy1Label.text = [self.data[indexPath.row] objectForKey:@"group"];
@try {
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:[self.data[indexPath.row] objectForKey:@"image"]]];
cell.thumbnailImageView.image = [UIImage imageWithData:imageData];
}
@catch (NSException * e) {
NSLog(@"Exception: %@", e);
}
return cell;
}
try/catch
只是因为它可能有也可能没有图像,但它甚至在我将其放入之前就已经发生了。似乎在使单元格出队时出现了某种问题。有什么想法吗?
您试图获取图像阻碍了主线程。将您的图像调用放在单独的线程中。
如果您熟悉使用第三方库或 CocoaPods,但这是一个常见问题,我推荐使用 https://github.com/rs/SDWebImage or https://github.com/AFNetworking/AFNetworking,它有 UIImageView 类别方法来处理从 URL 在后台,而不是在主线程中。
例如使用 SDWebImage:
[cell.thumbnailImageView sd_setImageWithURL:yourImageURL];
该方法将在后台获取图像,不会阻塞主线程,不会使您的 UITableView 抖动。
如前所述,此调用会阻塞主线程:
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:[self.data[indexPath.row] objectForKey:@"image"]]];
没有提到的是处理此类工作的正确方法。
您不能只将以上内容包装在 GCD 调用中并期望一切正常。
您需要延迟加载图像并在适当的时候将它们填充到 tableView 上。
处理这个问题最优雅的方法是编写您自己的 NSOperation
.
Here是教程,有问题可以私信我。