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

查看:134
本文介绍了如何在分组样式中设置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的框架,这样表格会更小。这将导致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.

此解决方案如下所示:

-(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天时间尝试了很多可能性。这就是剩下的。

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天全站免登陆