iphone - 自定义 UITableViewCell 宽度在 tableview reloadData 上调整大小

标签 iphone objective-c ios uitableview

我有一个 UITableView,我通过 UIView 动画 block 增加它的宽度。

[UIView animateWithDuration:.5 
                     animations:^{
                         CGRect frame = myTableView.frame;
                         frame.size.height += 190;
                         myTableViewView.frame = frame;
                         [[myTableViewView visibleCells] makeObjectsPerformSelector:@selector(setNeedsDisplay)];
                     } 
                     completion:nil];

此调用完成后,我的子类 UITableViewCells 将被重绘并调用此委托(delegate)方法。

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell        forRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGRect frame = cell.frame;
    frame.size.width = tableView.frame.size.height;
    cell.frame = frame;
}

没有这个,我的 UITableViewCells 就不会绘制到 UITableView 的整个宽度。我遇到的问题是当我调用 [tableView reloadData] 时。重新加载数据正在调用上面的 willDisplayCell 调用来设置正确的帧,但是当我观察 UITableViewCell setFrame 的堆栈跟踪时:另一个调用正在从私有(private) UITableView 调用对我的所有单元格调用:

 -[UITableView(_UITableViewPrivate) _updateVisibleCellsNow:]

此调用将我所有可见的 UITableViewCells 的框架设置为 UITableView 的原始宽度和更小的宽度。有谁知道如何防止这种情况发生,以便我的单元格保持表格 View 的宽度?

最佳答案

我认为最好的办法是不要对 UITableViewCell 宽度施展魔法。

更好的解决方案是向单元格添加 subview 并更改该 subview 的宽度。

关于iphone - 自定义 UITableViewCell 宽度在 tableview reloadData 上调整大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10450286/

相关文章:

iphone - iOS:如何在应用程序中进行I18N/本地化?

ios - 与 xib 中隐藏的导航栏相关的自动布局无效

ios - iPhone应用程序在退出事件状态后崩溃(核心数据删除后)

ios - NSPredicate 正则表达式错误

ios - 如何使用 UIActivityItemProvider 设置邮件主题

objective-c - 更改 View 时如何保存数据

ios - 使用 Watch OS2 打电话

ios - 调整嵌入 ScrollView 内的网页 View 的大小

iphone - 如何在 ios 7 中阻止旋转

c - 了解字节顺序和函数,如 CFSwapInt32HostToBig