ios - UIPageViewController 滑动延迟

标签 ios objective-c xcode uitableview uipageviewcontroller

我的 iOS 应用程序使用了大约 18 个页面的 UIPageViewcontroller。每个页面都有一个带有 4 个动态单元格的 UITableView。每个单元格有大约 10 张不同的图像。

问题是每次我滑动 UIPageViewcontroller 转到下一页/上一页时,都会有半秒的延迟,这让用户体验非常糟糕。

似乎下一页的 UITableView 在滑动加载其所有重数据单元格后显示,这会导致此延迟。

我读过可以使此加载异步,但我不知道如何执行此操作。

你们中的任何人都知道如何避免这种延迟以便顺利过渡到页面吗?

这是 UITableView 的一些代码:

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return 4;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([cellsStatus[indexPath.row] integerValue] == CLOSED_CELL)
        return [GrintScoreTrackPlayerDynamicCell getHeightForCell];
    else
        return [GrintScoreTrackPlayerDynamicCell getFullHeightForCell];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if ([mainController respondsToSelector:@selector(tableViewDidScroll:)] &&
        [mainController isKindOfClass:[GrintScoreTrackViewController class]])
    {
        [mainController tableViewDidScroll:scrollView];
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier;
    GrintScoreTrackPlayerDynamicCell *cell, *cellOwner;

    // Cell ID
    CellIdentifier = @"GrintScoreTrackPlayerDynamicCell-ID";

    // Creating the cell
    cell = (GrintScoreTrackPlayerDynamicCell*)[self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    // Creating the cell owner
    cellOwner = [[GrintScoreTrackPlayerDynamicCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

    // Getting cell from Nib, if possible.
    if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"GrintScoreTrackPlayerDynamicCell" owner:cellOwner options:nil];
        cell = [nib objectAtIndex:0];
    }

    // Filling cell with the corresponding user info
    [cell fillCellWithContent];

    // Changing appearance based on the new status
    [self setStatusOfCell:cell withStatus:[cellsStatus[indexPath.row] integerValue]];

    // Setting action for cell's "collapseButton" button
    [cell.collapseButton addTarget:self action:@selector(changeCellStatus:) forControlEvents:UIControlEventTouchUpInside];

    // Setting action for cell's "editScoresButton" button
    [cell.editScoresButton addTarget:self action:@selector(editScoresButtonWasPressed:) forControlEvents:UIControlEventTouchUpInside];

    // Setting action for cell's "pickerEnterButton" button
    [cell.pickerEnterButton addTarget:self action:@selector(pickerEnterButtonWasPressed:) forControlEvents:UIControlEventTouchUpInside];

    // Setting cell selection style
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.layoutMargins = UIEdgeInsetsZero;

    // Setting picker view delegate and data source.
    [cell.pickerView setDelegate:self];
    [cell.pickerView setDataSource:self];

    return cell;
}

还有一些来自 GrintScoreTrackPlayerDynamicCell 单元格的代码:

- (void) fillCellWithContent
{
    // Doing some processing fixes
    [self postProcessCellContent];
}

- (void) postProcessCellContent
{
    CGSize textSize;
    CGFloat scoreLabelTextWidth, newHazzardLabelXValue, newPuttsLabelXValue, offset;

    // Setting an offset for the hazzard and putts label. This offset represents the distance between the score label text and the hazzard and putts labels.
    offset = 1;

    // Getting the width of the text that's inside the Score Label.
    textSize = [[self.scoreLabel text] sizeWithAttributes:@{NSFontAttributeName:[self.scoreLabel font]}];
    scoreLabelTextWidth = textSize.width;

    // Getting new x position value for the Hazzards Label.
    newHazzardLabelXValue = self.scoreLabel.frame.origin.x +
                            (self.scoreLabel.frame.size.width - scoreLabelTextWidth) * 0.5 -
                            self.hazardsLabel.frame.size.width - offset;

    // Setting the new x position value for the Hazzards label
    [GrintUtils moveView:self.hazardsLabel inX:newHazzardLabelXValue andY:self.hazardsLabel.frame.origin.y];

    // Getting new x position value for the Putts Label.
    newPuttsLabelXValue = self.scoreLabel.frame.origin.x +
                         (self.scoreLabel.frame.size.width - scoreLabelTextWidth) * 0.5 +
                          scoreLabelTextWidth + offset;

    // Setting the new x position value for the Putts label
    [GrintUtils moveView:self.puttsLabel inX:newPuttsLabelXValue andY:self.puttsLabel.frame.origin.y];
}

这是一些关于我的 UIPageViewController 的代码,页面预加载到 pageContent 数组中:

#pragma mark - UIPageViewController Data Course

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
    // Getting the previous of the next controller
    int index;
    index = currentPageBeingDisplayed - 1;
    if (index < 0)
        index = [pageContent count] - 1;

    return pageContent[index];
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
    // Getting the index of the next controller
    int index;
    index = currentPageBeingDisplayed + 1;
    index = index % [pageContent count];

    return pageContent[index];
}

关注延迟的 Instruments Tool Counter 屏幕截图:

enter image description here

单元格的最后 2 张图片(它有 2 个状态):

enter image description here

enter image description here

最佳答案

这将是一个大胆的猜测 - 我还没有尝试过。但是从你如此出色的推论,以及你在评论中所说的,我认为这就是图像进入细胞的方式:它们在细胞定义 .xib.

因此,对于表格的每一行,.xib 都会被加载,并且所有图像都必须从 .xib 中重新加载。我们认为这就是让您放慢速度的原因。

所以这里有一个建议:不要那样做。相反,单元格中只有空 ImageView ,并用 cellForRowAtIndexPath: 中的图像填充它们。

为什么这会有帮助?因为如果您通过调用 imageNamed: 获取图像,图像将被系统缓存。因此,每张图片将被获取一次,之后将永远缓存。

还有一个建议 - 确保图像,即与显示的尺寸相同。从大图像开始并在小 ImageView 中显示它是一种巨大的时间浪费,因为每次都必须计算要显示的图像版本。

关于ios - UIPageViewController 滑动延迟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28121004/

相关文章:

ios - 如何刷新Kingfisher缓存的一系列图片?

ios - 使用 UIPopoverBackgroundView 类

ios - Xcode 8.x 在 XIB 文件中挂起太多

c++ - 运行英特尔线程构建模块的 XCode 8 环境变量是什么

ios - UITableViewCell 不显示

ios - Firebase 动态链接的问题

ios - 在 iOS 中为本地应用程序内容创建图库

ios - PopoverPresentationController不会在触摸外部时被解雇

objective-c - cocos2d绘制方法

iphone - 为 iphone 评级星级