Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/44.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 旋转UITableView时更新UITableViewCells时出现问题_Iphone_Uitableview_Rotation - Fatal编程技术网

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

Iphone 旋转UITableView时更新UITableViewCells时出现问题,iphone,uitableview,rotation,Iphone,Uitableview,Rotation,我在自定义UITableViewCell中有一个UILabel,当设备旋转时,它会调整大小。旋转后需要重新计算此标签中的文本,因为我正在将其缩小到合适的大小,并在末尾追加一些文本 例如,数据模型有:“这是一个需要停止的连续语句。” 在纵向模式下,它变为“这是一个运行已发送…更多” 在横向模式下,它变成“这是一个持续不断的句子……更多” From(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterface

我在自定义UITableViewCell中有一个UILabel,当设备旋转时,它会调整大小。旋转后需要重新计算此标签中的文本,因为我正在将其缩小到合适的大小,并在末尾追加一些文本

例如,数据模型有:“这是一个需要停止的连续语句。”

在纵向模式下,它变为“这是一个运行已发送…更多”

在横向模式下,它变成“这是一个持续不断的句子……更多”

From
(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];
}
for(int index=max+1;index
(其中max是VisibleCell返回的最大行),但cell始终为零

是否有任何方法可以刷新UITableViewCells的缓存,使其不会被重复使用?或者访问它们以便我可以更新它们


谢谢

cellforrowatinexpath:
中创建单元格时,将其添加到数组中。然后,循环遍历数组,根据需要更新文本

希望这有帮助,
jrtc27

编辑:


您说它们是自定义单元格-您不能更新
UITableViewCell
子类中的文本吗?

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

其次,在
(UITableViewCell*)tableView:(UITableView*)tableView cell for rowatinexpath:(nsindepath*)indepath
方法中添加负责收缩的代码行

例如:

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

    if (UIInterfaceOrientationIsPortrait(self.interfaceOrientation)) {
        //Make labels smaller
    }
    else {
        //Make them bigger
    }
}
或者,您可以在创建它们时调用
updateCellForRotate:forRow:
方法。但我不确定该函数是如何工作的,所以我不能说得太具体。

两件事。 第一。在您的
didRotateFromInterfaceOrientation
方法中,您可以像这样简单地重新加载可见行:

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

    NSLog(@"didRotateFromInterfaceOrientation:%d",fromInterfaceOrientation);
    [tableView reloadRowsAtIndexPaths:[tableView indexPathsForVisibleRows] withRowAnimation:UITableViewRowAnimationNone];
}
然后,我建议您将
interface-orientation
编号或仅将表格宽度添加到出列单元格名称中,这样
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];
    }
}

您可以在shouldAutorotateToInterfaceOrientation方法中使用此简单行:

self.view.autoresizesSubviews = YES;
对我来说,它总是成功的

因此,我最近遇到了(我认为是)一个非常类似的问题,我很抱歉地说,贴出的答案没有一个对我有帮助

我的问题是,我故意在旋转时调整UITableView的大小并重新定位,我是通过编程实现的。纵向图中的表格单元格占据了视图的宽度,横向图中的表格单元格略高,但宽度较小。然后,我根据我们得出的方向重新定位单元的元素

应用程序启动后,第一次查看表就可以了。然后我旋转,发现我似乎有两个元素的实例,它们似乎是第一个表格中显示的单元格。然后向后旋转会损坏带有上一个表中元素的初始方向表

我尝试了上面所有适用的答案,直到我仔细查看cellForRowAtIndexPath代码:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
我知道细胞重复使用是一个很好的主意,但我真的不需要保留(就像保留)任何细胞,我希望它们在每次旋转后都是明亮、亮丽、崭新的

编辑:在我自己的应用程序中,我最多会有20-30行,因为我个人不喜欢太长的桌子。如果一个特定的查询返回了很多行,我会为用户提供一些过滤器,帮助他们分类他们想要的行。如果要显示大量的行,那么将它们排在队列中可能会对性能造成不希望的影响

我所做的只是注释掉if和以下括号,我的表格单元格完全按照我的要求更新:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
//if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
//}
为华夫饼道歉,对一个老问题的迟来回答

本。
华夫饼、奶油或糖浆。

在方向改变时重新创建所有单元肯定会导致性能下降吗?虽然我可能错了(这是众所周知的);)是的,“CellforRowatineXpath”似乎不起作用。也没有重新加载数据-应该在我的原始文件中发布。将调查updateCellForRotate-它们是来自xibs的自定义单元格。如果您正在回收单元格(这是管理表内存的最佳方式!),则重新加载数据是您想要做的,因为这会按照您自己的方式“刷新缓存”。如果你不回收细胞,这是一个更大的野兽,所以你必须手动重新绘制每一个。我在回收细胞。[tableview reloadData]似乎确实更新了所有可见的单元格,这很好,但当我向下滚动时,仍有一两个单元格出现问题。我刚刚将实现更改为[self.tableview reloadData],这使我只在一行中找到了确切的位置。直到用户滚动到单元格,离开,然后再返回,问题仍然存在。希望他们不会注意到-s