如何以分组样式设置 UITableView 中单元格的宽度
我已经为此工作了大约两天,所以我想与您分享我的经验。
问题是:是否可以使分组的 UITableView 中单元格的宽度变小?
答案是:不。
但是有两种方法可以解决这个问题。
解决方案#1:更薄的桌子 可以更改tableView的框架,使表格变小。这将导致 UITableView 以减小的宽度渲染内部单元格。
解决方案如下所示:
-(void)viewWillAppear:(BOOL)animated
{
CGFloat tableBorderLeft = 20;
CGFloat tableBorderRight = 20;
CGRect tableRect = self.view.frame;
tableRect.origin.x += tableBorderLeft; // make the table begin a few pixels right from its origin
tableRect.size.width -= tableBorderLeft + tableBorderRight; // reduce the width of the table
tableView.frame = tableRect;
}
解决方案#2:由图像渲染单元格
此解决方案如下所述:http://cocoawithlove.com/2009/04/easy-custom-uitableview-drawing.html
我希望这些信息对您有所帮助。我花了大约2天的时间尝试了很多可能性。这就是剩下的。
I have been working on this for about 2 days, so i thought i share my learnings with you.
The question is: Is it possible to make the width of a cell in a grouped UITableView smaller?
The answer is: No.
But there are two ways you can get around this problem.
Solution #1: A thinner table
It is possible to change the frame of the tableView, so that the table will be smaller. This will result in UITableView rendering the cell inside with the reduced width.
A solution for this can look like this:
-(void)viewWillAppear:(BOOL)animated
{
CGFloat tableBorderLeft = 20;
CGFloat tableBorderRight = 20;
CGRect tableRect = self.view.frame;
tableRect.origin.x += tableBorderLeft; // make the table begin a few pixels right from its origin
tableRect.size.width -= tableBorderLeft + tableBorderRight; // reduce the width of the table
tableView.frame = tableRect;
}
Solution #2: Having cells rendered by images
This solution is described here: http://cocoawithlove.com/2009/04/easy-custom-uitableview-drawing.html
I hope this information is helpful to you. It took me about 2 days to try a lot of possibilities. This is what was left.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
实现此目的的一种更好、更简洁的方法是子类化 UITableViewCell 并重写其
-setFrame:
方法,如下所示:为什么它更好?因为另外两个更糟糕。
在
-viewWillAppear中调整表格视图宽度:
首先,这是不可靠的,超级视图或父视图控制器可能会在调用
-viewWillAppear:
后进一步调整表格视图框架。当然,您可以为 UITableView 子类化并重写-setFrame:
,就像我在这里为 UITableViewCells 所做的那样。然而,子类化 UITableViewCells 是一种非常常见、简单且 Apple 的方式。其次,如果你的UITableView有backgroundView,你不希望它的backgroundView一起缩小。在缩小 UITableView 宽度的同时保持 backgroundView 宽度并不是一件简单的工作,更不用说将子视图扩展到其超级视图之外并不是一件非常优雅的事情。
自定义单元格渲染以伪造更窄的宽度
为此,您必须准备带有水平边距的特殊背景图像,并且您必须自己布局单元格的子视图以适应边距。
相比之下,如果您只是调整整个单元格的宽度,自动调整大小将为您完成所有工作。
A better and cleaner way to achieve this is subclassing UITableViewCell and overriding its
-setFrame:
method like this:Why is it better? Because the other two are worse.
Adjust table view width in
-viewWillAppear:
First of all, this is unreliable, the superview or parent view controller may adjust table view frame further after
-viewWillAppear:
is called. Of course, you can subclass and override-setFrame:
for your UITableView just like what I do here for UITableViewCells. However, subclassing UITableViewCells is a much common, light, and Apple way.Secondly, if your UITableView have backgroundView, you don't want its backgroundView be narrowed down together. Keeping backgroundView width while narrow down UITableView width is not trivial work, not to mention that expanding subviews beyond its superview is not a very elegant thing to do in the first place.
Custom cell rendering to fake a narrower width
To do this, you have to prepare special background images with horizontal margins, and you have to layout subviews of cells yourself to accommodate the margins.
In comparison, if you simply adjust the width of the whole cell, autoresizing will do all the works for you.
要在 Swift 中执行此操作(它不提供设置变量的方法),您必须重写
frame
的 setter。最初发布(至少在我找到它的地方)此处To do this in Swift, which does not provide methods to set variables, you'll have to override the setter for
frame
. Originally posted (at least where I found it) here如果没有任何效果,您可以尝试
将单元格的背景颜色设置为透明颜色,然后放置所需尺寸的单元格图像。如果您想在该单元格上显示一些文本,请在图像上方放置一个标签。不要忘记将标签的背景颜色也设置为透明颜色。
If nothing works you can try this
Make the background colour of the cell as clear color and then put an image of the cell with required size. If you want to display some text on that cell put a label above the image. Don't forget to set the background color of the label also to clear color.
我发现接受的解决方案在轮换时不起作用。实现具有固定宽度和固定宽度的 UITableViewCells灵活的边距我刚刚将上述解决方案调整为以下内容:
每当设备旋转时就会调用该方法,因此单元格将始终居中。
I found the accepted solution didn't work upon rotation. To achieve UITableViewCells with fixed widths & flexible margins I just adapted the above solution to the following:
The method gets called whenever the device rotates, so the cells will always be centered.
当屏幕旋转时会调用一个方法:viewWillTransitionToSize
这是您应该调整框架大小的地方。参见示例。根据需要更改框架坐标。
There is a method that is called when the screen is rotated : viewWillTransitionToSize
This is where you should resize the frame. See example. Change the frame coords as you need to.
我这样做,
这对我有用
i do it in
And this worked for me
在 .h 文件中添加委托“UITableViewDataSource”
In .h file add the delegate 'UITableViewDataSource'