如何在分组样式的 UITableView 中设置单元格的宽度 [英] How to set the width of a cell in a UITableView in grouped style

查看:23
本文介绍了如何在分组样式的 UITableView 中设置单元格的宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经为此工作了大约 2 天,所以我想与您分享我的学习.

I have been working on this for about 2 days, so i thought i share my learnings with you.

问题是:是否可以将分组后的 UITableView 中单元格的宽度变小?

The question is: Is it possible to make the width of a cell in a grouped UITableView smaller?

答案是:不会.

但是有两种方法可以解决这个问题.

But there are two ways you can get around this problem.

解决方案 1:更薄的桌子可以改变tableView的frame,让table变小.这将导致 UITableView 以减小的宽度呈现内部的单元格.

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;
}

解决方案#2:通过图像渲染单元格

此处描述了此解决方案:http://cocoawithlove.com/2009/04/easy-custom-uitableview-drawing.html

This solution is described here: http://cocoawithlove.com/2009/04/easy-custom-uitableview-drawing.html

希望这些信息对您有所帮助.我花了大约 2 天的时间尝试了很多可能性.剩下的就这些了.<​​/p>

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.

推荐答案

一个更好更简洁的方法是继承 UITableViewCell 并重写它的 -setFrame: 方法,如下所示:

A better and cleaner way to achieve this is subclassing UITableViewCell and overriding its -setFrame: method like this:

- (void)setFrame:(CGRect)frame {
    frame.origin.x += inset;
    frame.size.width -= 2 * inset;
    [super setFrame:frame];
}

为什么更好?因为另外两个更糟.

Why is it better? Because the other two are worse.

  1. -viewWillAppear:

首先,这是不可靠的,在调用-viewWillAppear:后,父视图控制器可能会进一步调整表视图框架.当然,你可以为你的 UITableView 子类化和覆盖 -setFrame: 就像我在这里为 UITableViewCells 所做的那样.然而,继承 UITableViewCells 是一种非常常见、轻巧且 Apple 的方式.

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.

其次,如果你的 UITableView 有 backgroundView,你不希望它的 backgroundView 被缩小到一起.在缩小 UITableView 宽度的同时保持 backgroundView 宽度并非易事,更不用说将子视图扩展到其超视图之外并不是一件非常优雅的事情.

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.

这篇关于如何在分组样式的 UITableView 中设置单元格的宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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