ios - uitableview 中的滚动中断

标签 ios xcode uitableview

当我从服务器获取单元格的数据时,我在查看表格 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/

相关文章:

ios - 有没有办法遍历字典?

ios - UIPageviewcontroller 过渡样式滚动连续

ios - 在游戏结束过渡时为 spritekitgame 添加声音

xcodebuild (XCode 7.1) - 取消的命令出现意外的成功退出代码

ios - 设置uitableview背景 View

iphone - 如何从 iOS 中的照片中检测带框的图像

ios - 必须将自动调整掩码转换为约束以具有 _setHostsLayoutEngine :YES Xcode 13

xcode - 在 Xcode11 Beta 4 中使用带有 SwiftUI 的字符串(格式 : , args)时出错

ios - 通过在单个表格 View 中单击按钮逐个动态添加表格 View 单元格

ios - 如何在 Swift 中将标签和 UIButton 添加到 tableViewSection header ?