如何以分组样式设置 UITableView 中单元格的宽度

发布于 2024-08-27 08:12:36 字数 919 浏览 6 评论 0原文

我已经为此工作了大约两天,所以我想与您分享我的经验。

问题是:是否可以使分组的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

亣腦蒛氧 2024-09-03 08:12:36

实现此目的的一种更好、更简洁的方法是子类化 UITableViewCell 并重写其 -setFrame: 方法,如下所示:

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

为什么它更好?因为另外两个更糟糕。

  1. -viewWillAppear中调整表格视图宽度:

    首先,这是不可靠的,超级视图或父视图控制器可能会在调用 -viewWillAppear: 后进一步调整表格视图框架。当然,您可以为 UITableView 子类化并重写 -setFrame: ,就像我在这里为 UITableViewCells 所做的那样。然而,子类化 UITableViewCells 是一种非常常见、简单且 Apple 的方式。

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

  2. 自定义单元格渲染以伪造更窄的宽度

    为此,您必须准备带有水平边距的特殊背景图像,并且您必须自己布局单元格的子视图以适应边距。
    相比之下,如果您只是调整整个单元格的宽度,自动调整大小将为您完成所有工作。

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. 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.

  2. 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.

冰之心 2024-09-03 08:12:36

要在 Swift 中执行此操作(它不提供设置变量的方法),您必须重写 frame 的 setter。最初发布(至少在我找到它的地方)此处

override var frame: CGRect {
    get {
        return super.frame
    }
    set (newFrame) {
        let inset: CGFloat = 15
        var frame = newFrame
        frame.origin.x += inset
        frame.size.width -= 2 * inset
        super.frame = frame
    }
}

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

override var frame: CGRect {
    get {
        return super.frame
    }
    set (newFrame) {
        let inset: CGFloat = 15
        var frame = newFrame
        frame.origin.x += inset
        frame.size.width -= 2 * inset
        super.frame = frame
    }
}
诗笺 2024-09-03 08:12:36

如果没有任何效果,您可以尝试

将单元格的背景颜色设置为透明颜色,然后放置所需尺寸的单元格图像。如果您想在该单元格上显示一些文本,请在图像上方放置一个标签。不要忘记将标签的背景颜色也设置为透明颜色。

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.

北方的韩爷 2024-09-03 08:12:36

我发现接受的解决方案在轮换时不起作用。实现具有固定宽度和固定宽度的 UITableViewCells灵活的边距我刚刚将上述解决方案调整为以下内容:

- (void)setFrame:(CGRect)frame {

    if (self.superview) {
        float cellWidth = 500.0;
        frame.origin.x = (self.superview.frame.size.width - cellWidth) / 2;
        frame.size.width = cellWidth;
    }

    [super setFrame:frame];
}

每当设备旋转时就会调用该方法,因此单元格将始终居中。

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:

- (void)setFrame:(CGRect)frame {

    if (self.superview) {
        float cellWidth = 500.0;
        frame.origin.x = (self.superview.frame.size.width - cellWidth) / 2;
        frame.size.width = cellWidth;
    }

    [super setFrame:frame];
}

The method gets called whenever the device rotates, so the cells will always be centered.

岁月静好 2024-09-03 08:12:36

当屏幕旋转时会调用一个方法:viewWillTransitionToSize

这是您应该调整框架大小的地方。参见示例。根据需要更改框架坐标。

- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
{
    [coordinator animateAlongsideTransition:nil completion:^(id<UIViewControllerTransitionCoordinatorContext> context)
    {
        int x = 0;
        int y = 0;
        self.tableView.frame = CGRectMake(x, y, 320, self.tableView.frame.size.height);
    }];
}

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.

- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
{
    [coordinator animateAlongsideTransition:nil completion:^(id<UIViewControllerTransitionCoordinatorContext> context)
    {
        int x = 0;
        int y = 0;
        self.tableView.frame = CGRectMake(x, y, 320, self.tableView.frame.size.height);
    }];
}
絕版丫頭 2024-09-03 08:12:36

我这样做,

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell;
    CGFloat tableBorderLeft = self.view.frame.origin.x + 10;
    CGFloat tableBorderRight = self.view.frame.size.width - 20;

    CGRect tableRect = self.view.frame;
    tableRect.origin.x = tableBorderLeft; 
    tableRect.size.width = tableBorderRight; 
    tableView.frame = tableRect;
}

这对我有用

i do it in

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell;
    CGFloat tableBorderLeft = self.view.frame.origin.x + 10;
    CGFloat tableBorderRight = self.view.frame.size.width - 20;

    CGRect tableRect = self.view.frame;
    tableRect.origin.x = tableBorderLeft; 
    tableRect.size.width = tableBorderRight; 
    tableView.frame = tableRect;
}

And this worked for me

昔梦 2024-09-03 08:12:36

在 .h 文件中添加委托“UITableViewDataSource”

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{ 
    return size;
}

In .h file add the delegate 'UITableViewDataSource'

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{ 
    return size;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文