更改UITableViewCell框架但不起作用 [英] Change UITableViewCell frame but didn't work

查看:94
本文介绍了更改UITableViewCell框架但不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

UITableViewCell * cell = [tableViewController.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]];

[单元格setFrame:CGRectMake(cell.frame.origin.x,cell.frame.origin.y-34,cell.frame.size.width,cell.frame.size.height)]];

我想更改表格视图单元格的框架,该代码在某些ViewController中有效,但在某些情况下则无效。我想知道
这有什么问题。

I want to change the frame of table view cell, the code work in some ViewController, but some it doesn't work. I want to know is there something wrong with it.

非常感谢。

推荐答案

您无法完全控制单元格框架,尤其是原点。要求表视图委托提供表视图单元格,但是其位置取决于表视图的内部逻辑,以便将单元格正确放置在表视图(滚动视图子类)中。刷新表视图的显示部分时,将重新排列单元格位置并更新框架原点。
因此,尽管您的代码没有错,但它与您无法完全控制的某些表内部规则相冲突,因此结果可能无法预测。

You cannot have full control on the cell frame, especially on the origin. The table view delegate is asked for a table view cell, but then its position depends on the internal logic of the table view in order to correctly place the cell inside the table view (which is a scroll view subclass). When the displayed portion of the table view is refreshed, the cell position is rearranged and the frame origin is updated. So while your code is not wrong, it conflicts with some table internal rules you cannot fully control and so the results could unpredictable.

如果您想改为看到此更改的可见效果,您可以将帧转换器代码移动到代码的另一部分,例如在

If you want instead to see a visible effect of this change, you can move your frame changer code to another part of the code, e.g. inside the

tableView:didSelectRowAtIndexPath:


内的情况下,例如,您可以触摸一个单元格,调用此代码并更改单元格框架,您将立即进行更改。但是,一旦您将单元格滚动出iPhone屏幕,然后又将其移回屏幕中,您会注意到它会重新出现在其原始位置。

In such case you can for example touch a cell, call this code and change the cell frame and you will the change immediately. But as soon as you scroll the cell out of the iPhone screen, and then you move back this cell in the screen, you will notice that it will re-appear in its original position.

这篇关于更改UITableViewCell框架但不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆