Use Cell.ImageView.Image different size images. Quick

Can all images be set to the same size? I tried using cell.imageView? .frame.size.width = something. However, it does not work. Any suggestions? Thank you.

enter image description here

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "Cell")
cell.imageView!.layer.cornerRadius = 20
cell.imageView!.clipsToBounds = true
let imageData = try! resultsImageFileArray[indexPath.row].getData()
let image = UIImage( data: imageData)
cell.imageView?.image = image

cell.textLabel?.text = self.resultsNameArray[indexPath.row]
cell.detailTextLabel?.text = self.message3Array[indexPath.row]

return cell
}

When using UITableViewCell, you cannot change the properties of the cell.imageView field, because in this case, the imageView is a read-only property. The easiest way to achieve the result in this case is to create a subclass of UITableViewCell and use it Customize the layoutSubviews method Required content in the, for example:

class CustomTableViewCell: UITableViewCell {

override func awakeFromNib() {
super. awakeFromNib()
}

override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}

// Here you can customize the appearance of your cell
override func layoutSubviews() {
super.layoutSubviews()
// Customize imageView like you need
self.imageView?.frame = CGRectMake(10,0,40,40)
self.imageView?.contentMode = UIViewContentMode.ScaleAspectFit
// Costomize other elements
self.textLabel?. frame = CGRectMake(60, 0, self.frame.width-45, 20)
self.detailTextLabel?.frame = CGRectMake(60, 20, self.frame.width-45, 15)
}
}

In the tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) function, you can only replace the cell object created from UITableViewCell For CustomTableViewCell:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: CustomTableViewCell = CustomTableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "Cell")
cell.imageView!.layer.cornerRadius = 20
cell.imageView!.clipsToBounds = true
let imageData = try! resultsImageFileArray[indexPath.row].getData( )
let image = UIImage(data: imageData)
cell.imageView?.image = image

cell.textLabel?.text = self.resultsNameArray[indexPath.row]< br /> cell.detailTextLabel?.text = self.message3Array[indexPath.row]

return cell
}

Is it possible Set all images to the same size? I tried using cell.imageView? .frame.size.width = something. However, it does not work. Any suggestions? Thank you.

enter image description here

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "Cell")
cell.imageView!.layer.cornerRadius = 20
cell.imageView!.clipsToBounds = true
let imageData = try! resultsImageFileArray[indexPath.row].getData()
let image = UIImage( data: imageData)
cell.imageView?.image = image

cell.textLabel?.text = self.resultsNameArray[indexPath.row]
cell.detailTextLabel?.text = self.message3Array[indexPath.row]

return cell
}

When using UITableViewCell, cell.imageView cannot be changed The attribute of the field, because in this case, imageView is a read-only attribute. The easiest way to achieve the result in this case is to create a subclass of UITableViewCell and use it to customize the content required in the layoutSubviews method, for example:

class CustomTableViewC ell: UITableViewCell {

override func awakeFromNib() {
super.awakeFromNib()
}

override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}

// Here you can customize the appearance of your cell
override func layoutSubviews() {
super.layoutSubviews()
// Customize imageView like you need
self.imageView?.frame = CGRectMake(10,0,40,40)
self.imageView? .contentMode = UIViewContentMode.ScaleAspectFit
// Costomize other elements
self.textLabel?.frame = CGRectMake(60, 0, self.frame.width-45, 20)
self.detailTextLabel? .frame = CGRectMake(60, 20, self.frame.width-45, 15)
}
}

In the tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) function , You can only replace the cell object creation from UITableViewCell to CustomTableViewCell:

func tableView(tabl eView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: CustomTableViewCell = CustomTableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "Cell")
cell.imageView!.layer.cornerRadius = 20
cell.imageView!.clipsToBounds = true
let imageData = try! resultsImageFileArray[indexPath.row].getData()
let image = UIImage(data: imageData)
cell. imageView?.image = image

cell.textLabel?.text = self.resultsNameArray[indexPath.row]
cell.detailTextLabel?.text = self.message3Array[indexPath.row]

return cell
}

Leave a Comment

Your email address will not be published.