定位 UITableViewCell 的 imageView

发布于 2024-10-03 04:48:27 字数 401 浏览 2 评论 0原文

在 UITableView 的每个 UITableViewCell 中,我都将图像放置在其 imageView 中。但是,如果我使用 tableView:heightForRowAtIndexPath: 增加单元格的高度以容纳更多文本,我发现:

  1. 图像垂直居中对齐。
  2. 图像的左边距增加。这会将文本向右推,使其左边距不再与上方和下方单元格中的文本对齐。

我希望图像垂直顶部对齐,以便它位于单元格的左上角(如下图中的第一个单元格),并且其左边距不增加(以便第二个单元格中的文本与第一个单元格中的文本对齐)。

改变imageView的框架、中心等似乎没有影响。有什么想法吗?谢谢...

(我有这个问题的图像,但是还不允许我发布图像,因为我是一个声誉低于 10 的菜鸟。Grrrr...)

In every UITableViewCell of a UITableView I'm placing an image in its imageView. However, if I increase the height of a cell using tableView:heightForRowAtIndexPath: to accomodate more text, I've found that:

  1. The image is center-aligned vertically.
  2. The left margin of the image increases. This pushes the text right so its left margin is no longer aligned with the text in the cells above and below.

I'd like the image to be top-aligned vertically, so that it is in the upper-left corner of the cell (like the first cell in the image below) and not have its left margin increased (so that the left margin of the text in the second cell aligns with the text in the first cell).

Altering the frame, center, etc. of the imageView seems to have no affect. Any ideas? Thanks...

(I have an image of the problem, but SO won't let me post an image yet because I'm a noob with less than 10 reputation. Grrr...)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(7

尾戒 2024-10-10 04:48:27

您需要子类化 UITableViewCell 并重写 layoutSubviews,如下所示:

- (void) layoutSubviews
{   
    [super layoutSubviews];

    self.imageView.frame = CGRectMake( 10, 10, 50, 50 ); // your positioning here
}

在 cellForRowAtIndexPath: 方法中,确保返回新单元格类型的实例。

You need to subclass UITableViewCell and override layoutSubviews, as follows:

- (void) layoutSubviews
{   
    [super layoutSubviews];

    self.imageView.frame = CGRectMake( 10, 10, 50, 50 ); // your positioning here
}

In your cellForRowAtIndexPath: method, be sure to return an instance of your new cell type.

紫轩蝶泪 2024-10-10 04:48:27

当您认为需要调整默认样式中项目的大小或位置时,您就需要考虑创建一个自定义单元格,其中您自己的项目的大小和位置按照您想要的方式进行。这就是您可以创建自定义单元格的原因。 :)

The moment you think you need to adjust sizes or positions of items in the default styles, is the time you need to think about creating a custom cell with your own items sized and positioned the way you want them to be. This is why you can create custom cells. :)

日久见人心 2024-10-10 04:48:27

另一种方法(适用于 iOS 6)是创建一个 UIImageView 并将其作为子视图添加到单元格中

 UIImageView *yourImageView = [[UIImageView alloc] initWithFrame:CGRectMake(x,y,20,20)];

[yourImageView setImage:[UIImage imageNamed:[imagesArray objectAtIndex:indexPath.row]]]; //You can have an Array with the name of 

[yourcell.contentView addSubview:yourImageView]; //add the imageView as a subview

Another way (works in iOS 6) is to create a UIImageView and add it to the cell as a subview

 UIImageView *yourImageView = [[UIImageView alloc] initWithFrame:CGRectMake(x,y,20,20)];

[yourImageView setImage:[UIImage imageNamed:[imagesArray objectAtIndex:indexPath.row]]]; //You can have an Array with the name of 

[yourcell.contentView addSubview:yourImageView]; //add the imageView as a subview
只是我以为 2024-10-10 04:48:27

您可能没有尝试过的一个非常有用的函数(在 UITableViewDelegate 中)是 tableView:willDisplayCell:forRowAtIndexPath:。它在单元格显示之前立即调用,其目的是使您能够对布局进行操作,而无需操作系统在显示之前再次对其进行修改。实际上,您拥有完全的控制权。我用它来更改单元格中的字体,但您也可以迭代单元格的子视图并修改它们的框架。

不过我同意你现在是时候研究自定义单元并停止与操作系统对抗了。这并不难,苹果在 iOS 表格视图编程指南

A really useful function you might not have tried (in UITableViewDelegate) is tableView:willDisplayCell:forRowAtIndexPath:. It is called immediately before a cell is displayed and its purpose is to enable you to do things to the layout without the OS modifying it again before display. Effectively you have total control. I've used it for changing fonts in cells but you can iterate the cell's subviews and modify their frames too.

Still I agree you are at the point where it is time to look into custom cells and stop fighting the OS. It isn't hard to do and Apple gives a good example in Table View Programming Guide for iOS.

冰之心 2024-10-10 04:48:27

TomSwift 解决方案的 Swift 4 变体(对我来说效果很好):

override public func layoutSubviews() {
        super.layoutSubviews()
        imageView?.frame = CGRect( x: 16, y: 9, width: imageView!.intrinsicContentSize.width, height:  imageView!.intrinsicContentSize.height)
    }

Swift 4 variation of TomSwift's solution (which worked for me nicely):

override public func layoutSubviews() {
        super.layoutSubviews()
        imageView?.frame = CGRect( x: 16, y: 9, width: imageView!.intrinsicContentSize.width, height:  imageView!.intrinsicContentSize.height)
    }
假装不在乎 2024-10-10 04:48:27

您可以创建自定义单元格并将对象放置在您需要的位置以及设置自动遮罩值。

You can create a custom cell and place the objects where you need them to be as well as setting the auto mask values.

甜味超标? 2024-10-10 04:48:27
class CustomCell: UITableViewCell {
    override public func layoutSubviews() {
        super.layoutSubviews()
        imageView?.frame = CGRect( x: 20, y: 10, width: imageView!.intrinsicContentSize.width, height:  imageView!.intrinsicContentSize.height)
    }
}

在故事板中使用 CustomCell 代替 UITableViewCell

输入图片此处描述

还从数据源返回 CustomCell 实例,如下所示

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if let cell: CustomCell = tableView.dequeueReusableCell(withIdentifier: "revcell", for: indexPath) as? CustomCell
        {
            //code
            return cell
        }
        return UITableViewCell()
    } 
class CustomCell: UITableViewCell {
    override public func layoutSubviews() {
        super.layoutSubviews()
        imageView?.frame = CGRect( x: 20, y: 10, width: imageView!.intrinsicContentSize.width, height:  imageView!.intrinsicContentSize.height)
    }
}

Use CustomCell in your storyboard insted of UITableViewCell.

enter image description here

Also return CustomCell instance from datasource as

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if let cell: CustomCell = tableView.dequeueReusableCell(withIdentifier: "revcell", for: indexPath) as? CustomCell
        {
            //code
            return cell
        }
        return UITableViewCell()
    } 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文