加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

使用cell.imageView.image的不同大小的图像.迅速

发布时间:2020-12-14 04:28:37 所属栏目:百科 来源:网络整理
导读:是否可以将所有图像设置为相同的尺寸?我试过使用cell.imageView?.frame.size.width = something.但是,它不起作用.有什么建议?谢谢. func tableView(tableView: UITableView,cellForRowAtIndexPath indexPath: NSIndexPath) - UITableViewCell { let cell:
是否可以将所有图像设置为相同的尺寸?我试过使用cell.imageView?.frame.size.width = something.但是,它不起作用.有什么建议?谢谢.

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
}

解决方法

使用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,40,40)
        self.imageView?.contentMode = UIViewContentMode.ScaleAspectFit
        // Costomize other elements
        self.textLabel?.frame = CGRectMake(60,self.frame.width - 45,20)
        self.detailTextLabel?.frame = CGRectMake(60,20,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
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读