使用 cell.imageView.image 的不同大小的图像。 Swift
Different size of image using cell.imageView.image. Swift
是否可以将所有图片设置为相同大小?我尝试使用 cell.imageView?.frame.size.width = 东西。但是,它不起作用。有什么建议么?谢谢
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
}
当您使用 UITableViewCell
时,您不能更改 cell.imageView
字段的属性,因为在这种情况下 imageView
是只读的 属性。在这种情况下实现结果的最简单方法是创建 UITableViewCell
的子类并使用它来自定义 layoutSubviews
方法中所需的内容,例如:
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)
}
}
并且在您的 tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath)
函数中您只能将单元格对象创建从 UITableViewCell
替换为 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
}
是否可以将所有图片设置为相同大小?我尝试使用 cell.imageView?.frame.size.width = 东西。但是,它不起作用。有什么建议么?谢谢
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
}
当您使用 UITableViewCell
时,您不能更改 cell.imageView
字段的属性,因为在这种情况下 imageView
是只读的 属性。在这种情况下实现结果的最简单方法是创建 UITableViewCell
的子类并使用它来自定义 layoutSubviews
方法中所需的内容,例如:
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)
}
}
并且在您的 tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath)
函数中您只能将单元格对象创建从 UITableViewCell
替换为 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
}