我正在尝试将按钮的图像更改为用户在imagePicker中选择的图像,但是我在ViewController类中显示了图像选择器,并且按钮在单元格Class中,我不确定如何设置它来自不同类的代码是我用来呈现图像选择器和创建按钮的方式的代码:
//这是我的profileVC类
class UserProfileVC: UICollectionViewController, UICollectionViewDelegateFlowLayout, UserProfileHeaderDelegate {
func handleEditBannerTapped(for header: ProfileHeader) {
let imagePicker = UIImagePickerController()
imagePicker.allowsEditing = true
present(imagePicker, animated: true, completion: nil)
}
}
上面的代码工作正常,并且正在显示设备上的所有图片供用户选择。
//这是我的ProfileHeader Cell类
class ProfileHeader: UICollectionViewCell, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
lazy var profileBanner: UIButton = {
let button = UIButton()
button.clipsToBounds = true
let imageTapped = UITapGestureRecognizer(target: self, action: #selector(handleBannerTapped))
imageTapped.numberOfTapsRequired = 1
button.isUserInteractionEnabled = true
button.addGestureRecognizer(imageTapped)
return button
}()
@objc func handleBannerTapped() {
delegate?.handleEditBannerTapped(for: self)
}
}
我如何将用户从UserProfileVC类拾取的图像设置为profileBanner按钮
答案 0 :(得分:0)
我假设UserProfileVC
是主要类,它包含一个集合视图,而ProfileHeader
是该集合视图中的一个单元格?您需要知道单元格的IndexPath
,然后执行以下操作:
if let cell = collectionView.cellForItem(at: indexPath) as? ProfileHeader {
cell.profileBanner.setImage(imageThatYouWant, forState: .Normal)
}