ios - UITableViewCell 子类在重新显示时重新加载 subview

标签 ios objective-c uitableview

我有一个 UITableView,其中填充了属于 RightArrowTableViewCell 类的单元格,该类是 UITableViewCell 的子类。子类单元格包含一个类似附件的箭头图标 View (arrowDecoratorView)。选择主类别后,箭头图标会向下旋转以在同一表格 View 中显示子类别列表。

然而,当滚动表格 View 并且主类别单元格“移出 View ”时,事情就不会那么顺利了。一旦主类别单元格移回视线,箭头图标就会恢复其初始水平位置。

为了解决这个问题,我在 RightArrowTableViewCell 类中添加了一个标志来检查单元格是否展开。然后在 tableView: willDisplayCell: forRowAtIndexPath: 方法中,我执行以下操作:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    RightArrowTableViewCell *arrowCell = (RightArrowTableViewCell *)cell;
    if (arrowCell.iconExpanded) {
        [arrowCell updateIconStatus];
    }
}

updateIconStatus 是这样的:

- (void)updateIconStatus{
    self.arrowDecoratorView.transform =  CGAffineTransformMakeRotation(M_PI * 2.5);
    [self layoutIfNeeded];
}

上述解决方案似乎有效,但不会持续很长时间。连续滚动 3-4 次隐藏/显示主类别单元格后,箭头图标恢复其原始水平位置。

关于如何解决这个问题有什么想法吗?

谢谢。

最佳答案

您应该在 cellForRowAtIndexPath 中执行此操作,并且标志不能是您的子类 UITableViewCell 的属性,因为单元格正在被重用,因此该属性应该属于您尝试从中填充表格的数据结构。示例:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: NSIndexPath *)indexPath
{
    CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:@"customCell" forIndexPath:indexPath];

    CustomDataClass *data = [_arData objectAtIndex:indexPath.row];

    if(data.expand){
        [cell ShowArrowInThisPos];
    }
    else{
        [cell ShowArrowInThatPos];
    }
    ... some code

    return cell;
}

关于ios - UITableViewCell 子类在重新显示时重新加载 subview ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24611686/

相关文章:

ios - Instagram - 如何在 Objective C 中使用 API 关注用户

ios - 如何使用 swift 在 ios 9 中以编程方式将应用程序带到前台?

ios - 有没有办法使用 AVAudioEngine 和 AVAudioPlayerNode 显示锁屏控件?

objective-c - 将字节顺序交换为 double 值

ios - UITableView - 将数组分配给 UITableView 中的右侧部分

ios - 在 swift spritekit 场景中淡化视频

iphone - setContentOffset 失去精度?

ios - 如何导航到 navigationStack 中不存在的 UIViewController

swift - 为什么我会收到 SIGABRT 信号?

swift - 当 CustomCell 标签值更改时如何更新 UITableView?