当我从服务器获取单元格的数据时,我在查看表格 View 时遇到问题。如果我不使用照片,则滚动不会中断,但我也想使用图像。谁能知道我该如何解决这个问题?我正在从服务器中的 plist 获取数据。
这是使滚动中断的图像代码(我正在使用自定义样式单元格)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSURL *URL = [[NSURL alloc] initWithString:[[self.content objectAtIndex:indexPath.row] valueForKey:@"imageName"]];
NSData *URLData = [[NSData alloc] initWithContentsOfURL:URL];
UIImage *img = [[UIImage alloc] initWithData:URLData];
UIImageView *imgView = (UIImageView *)[cell viewWithTag:100];
imgView.image = img;
....
最佳答案
如果您的意思是滚动停止和开始,这可能是因为如果从服务器加载图像(这可能需要相当长的时间),在主线程上执行会导致卡住。
如果是这种情况,修复方法是在另一个线程中获取图像。幸运的是,iOS 有一个相当易于使用的多线程系统,称为 Grand Central Dispatch。
这是一些示例代码:
dispatch_queue_t q = dispatch_queue_create("FetchImage", NULL);
dispatch_async(q, ^{
/* Fetch the image from the server... */
dispatch_async(dispatch_get_main_queue(), ^{
/* This is the main thread again, where we set the tableView's image to
be what we just fetched. */
});
});
关于ios - uitableview 中的滚动中断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15671725/