旋转UITableView时更新UITableViewCells时出现问题

Mic*_*han 5 iphone rotation uitableview

我在自定义UITableViewCell中有一个UILabel,它可以在旋转设备时调整大小.旋转后需要重新计算此标签中的文本,因为我将其缩小到最小尺寸并在末尾添加一些文本.

例如,数据模型具有:"这是一个需要停止的连续句子."

在纵向模式下,它变为"这是一个连续发送...更多"

在横向模式中它变成"这是一个连续的句子...更多"

(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation 我能够访问可见的UITableViewCells并更新说明.

问题似乎是有UITableViewCells缓存但我无法达到.当我在旋转后滚动UITableView时,旋转后位于可见区域下方的一个或两个单元格在标签中没有正确的文本.所以它们没有被渲染(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath- 但是它们没有被[tableView visibleCells]返回(或者通过循环遍历通过[tableView子视图]返回的所有视图).

我试图通过这种方法访问"额外"单元格:

for (int index=max + 1; index < max + 3 && index < [cellTypes count]; index++) {
    NSIndexPath *updatedPath = [NSIndexPath indexPathForRow:index inSection:0];
    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:updatedPath];
    if (cell == nil) { continue; }
    [self updateCellForRotate:cell forRow:index];
}
Run Code Online (Sandbox Code Playgroud)

(其中max是从visibleCells返回的最大行)但是单元格总是为零.

反正有没有刷新UITableViewCells的缓存,以便它们不被重用?或者访问它们以便我可以更新它们?

谢谢!

rag*_*fin 15

两件事情.第一.在您的didRotateFromInterfaceOrientation方法中,您可以简单地重新加载可见行,如下所示:

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation) fromInterfaceOrientation
{
    [super didRotateFromInterfaceOrientation:fromInterfaceOrientation];

    NSLog(@"didRotateFromInterfaceOrientation:%d",fromInterfaceOrientation);
    [tableView reloadRowsAtIndexPaths:[tableView indexPathsForVisibleRows] withRowAnimation:UITableViewRowAnimationNone];
}
Run Code Online (Sandbox Code Playgroud)

然后我建议您将interfaceOrientation数字或表格宽度添加到出列单元格名称,这样就tableView知道一次旋转中的单元格与另一次旋转中的单元格不同.像这样:

- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath withType:(NSString *)s_type
{
    UITableViewCell *cell = nil;
    // add width of table to the name so that rotations will change the cell dequeue names
    s_cell = [s_cell stringByAppendingString:[NSString stringWithFormat:@"%@%d",@"Width",(int)tv.bounds.size.width]];

    NSLog(@"%@",s_cell);
    cell = [tv dequeueReusableCellWithIdentifier:s_cell];
    if( cell == nil ) { 
        cell = [[[UITableViewCell alloc];
        initWithFrame:CGRectZero reuseIdentifier:s_cell] autorelease];
    }
}
Run Code Online (Sandbox Code Playgroud)

  • FWIW,执行didRotateFromInterfaceOrientation:应该调用super. (3认同)

Mis*_*Moo 1

首先,要重新加载所有表格单元格,请使用[self.tableView reloadData]

其次,在方法内部添加负责收缩的代码行(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

例子:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    //Some identifier and recycling stuff

    if (UIInterfaceOrientationIsPortrait(self.interfaceOrientation)) {
        //Make labels smaller
    }
    else {
        //Make them bigger
    }
}
Run Code Online (Sandbox Code Playgroud)

updateCellForRotate:forRow:或者你可以在制作它们时调用你的方法。但我不确定该功能是如何工作的,所以我不能太具体。