如何在TableView中调整cell.imageView的大小并在Swift中应用tintColor

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

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

然后我添加一个testing图像到单元格的imageView:

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

结果:

在这里输入图像说明

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

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

结果:

调整颜色的单元格imageView

单元格中的图像太大,所以我使用以下代码resize:

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

在图像resize的同时,tintColor丢失:

在这里输入图像说明

有什么想法吗?

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

1)用自己的UIImageView大小和2个独立的标签创build自定义单元格

2)添加UIImageView作为细胞的Subview

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

解决scheme没有定制单元

 func imageWithImage(image:UIImage,scaledToSize newSize:CGSize)->UIImage{ UIGraphicsBeginImageContext( newSize ) image.drawInRect(CGRect(x: 0,y: 0,width: newSize.width,height: newSize.height)) let newImage = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return newImage.imageWithRenderingMode(.AlwaysTemplate) } override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! UITableViewCell cell.imageView?.tintColor = UIColor.greenColor() cell.imageView?.image = imageWithImage(UIImage(named: "imageName")!, scaledToSize: CGSize(width: 20, height: 20)) // Configure the cell... return cell } 

对于Swift 3

 func imageWithImage(image:UIImage,scaledToSize newSize:CGSize)->UIImage{ UIGraphicsBeginImageContext( newSize ) image.draw(in: CGRect(x: 0,y: 0,width: newSize.width,height: newSize.height)) let newImage = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return newImage!.withRenderingMode(.alwaysTemplate) } override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! UITableViewCell cell.imageView?.tintColor = UIColor.greenColor() cell.imageView?.image = imageWithImage(image: UIImage(named: "imageName")!,scaledToSize: CGSize(width: 20, height: 20)) // Configure the cell... return cell } 

对于没有自定义tableViewCell的OBJECTIVE C.

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"abc" forIndexPath:indexPath]; // Configure the cell... if (cell==nil) { cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"abc"]; } cell.textLabel.text = [menuItemsArray objectAtIndex:indexPath.row]; cell.imageView.image = [UIImage imageNamed:[menuItemsImagesArray objectAtIndex:indexPath.row]]; CGSize itemSize = CGSizeMake(20, 20); UIGraphicsBeginImageContextWithOptions(itemSize, false, self.view.contentScaleFactor); CGRect imageRect = CGRectMake(0, 0, itemSize.width, itemSize.height); [cell.imageView.image drawInRect:imageRect]; cell.imageView.image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return cell; }