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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2539021/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 17:05:08  来源:igfitidea点击:

How to set the width of a cell in a UITableView in grouped style

iosiphoneuitableviewwidth

提问by Tomen

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

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

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

问题是:是否可以使分组的 UITableView 中的单元格的宽度更小?

The answer is: No.

答案是不。

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

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

Solution #1: A thinner tableIt 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.

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

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

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

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

这里描述了这个解决方案: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.

我希望这些信息对您有所帮助。我花了大约 2 天的时间尝试了很多可能性。这是剩下的。

回答by an0

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

实现这一点的更好、更简洁的-setFrame:方法是继承 UITableViewCell 并像这样覆盖它的方法:

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

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

    首先,这是不可靠的,superview 或 parent view controller 可能会在-viewWillAppear:被调用后进一步调整 table view frame 。当然,您可以-setFrame:像我在这里为 UITableViewCells 所做的那样为您的 UITableView子类化和覆盖。但是,子类化 UITableViewCells 是一种非常常见、轻巧且 Apple 的方式。

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

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

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

回答by JuJoDi

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

要在不提供设置变量的方法的 Swift 中执行此操作,您必须覆盖frame. 最初发布(至少在我找到它的地方)在这里

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

回答by includeMe

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.

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

回答by Graham

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:

我发现接受的解决方案在轮换时不起作用。为了实现具有固定宽度和灵活边距的 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];
}

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

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

回答by Guy

There is a method that is called when the screen is rotated : viewWillTransitionToSize

有一个在屏幕旋转时调用的方法: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);
    }];
}

回答by L?u ??c Quy

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

这对我有用

回答by bharath gangupalli

In .h file add the delegate 'UITableViewDataSource'

在 .h 文件中添加委托 'UITableViewDataSource'

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