本文介绍了如何在TableView中调整cell.imageView的大小并在Swift中应用tintColor的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有在cellForRowAtIndexPath中创建的单元格的tableView并添加一些虚拟文本:

I have a tableView with a cell created in cellForRowAtIndexPath and add some dummy text:

cell = UITableViewCell(style: UITableViewCellStyle.Subtitle,
            reuseIdentifier: "cell")
cell.textLabel?.text = "Test Title"
cell.detailTextLabel?.text = "Test detail label"

然后我将测试图像添加到单元格的imageView中:

Then I add a test image to the cell's imageView:

var image = UIImage(named: "cd.png")
cell.imageView!.image = image

结果:

要调整颜色,我使用以下代码:

To adjust the color, I use the following code:

cell.imageView?.tintColor = UIColor.redColor()

结果:

单元格中的图像太大,所以我调整了尺寸使用以下代码:

The image is too big in the cell, so I adjust the size using the following code:

var itemSize:CGSize = CGSizeMake(20, 20)
UIGraphicsBeginImageContextWithOptions(itemSize, false, UIScreen.mainScreen().scale)
var imageRect : CGRect = CGRectMake(0, 0, itemSize.width, itemSize.height)
cell.imageView!.image?.drawInRect(imageRect)
cell.imageView!.image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()

同时图像调整大小,tintColor丢失:

Whilst the image does resize, the tintColor is lost:

任何想法吗?

推荐答案

你可以通过另一种方式做到:

You can do it by another way :

1)使用您自己的 UIImageView 大小和2个单独的标签

1) Create custom cell with your own UIImageView size and 2 separate labels

2)将 UIImageView 添加为子视图 of Cell

2) Add UIImageView as Subview of Cell

var cellImg : UIImageView = UIImageView(frame: CGRectMake(5, 5, 50, 50))
cellImg.image = UIImage(named: "yourimage.png")
cell.addSubview(cellImg)

这篇关于如何在TableView中调整cell.imageView的大小并在Swift中应用tintColor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 13:07