Is it possible to set all the image in the same size? I tried to use cell.imageView?.frame.size.width = something. But, it doesn't work. Any suggestions? Thanks.
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
}
You can not change the properties of cell.imageView
field when you use UITableViewCell
because imageView
is read-only property in this case. The easiest way to achieve the result in this case is to create subclass of UITableViewCell
and use it to customize what you need in layoutSubviews
method for example like this:
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)
}
}
And inside your tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath)
function you can only replace cell object creation from UITableViewCell
to 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]
cell.detailTextLabel?.text = self.message3Array[indexPath.row]
return cell
}