问题描述
我想通过tableviw单元中的按钮导入照片。因此特定表格视图单元格中的按钮会将图像导入到该特定单元格中的图像视图。因此,一个按钮会将图像导入到该特定单元格中的一个imageview。我试图将照片导入代码放在tableview单元格类中。代码未编译。
import UIKit
class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell",for: indexPath)
cell.textLabel?.text = "\(indexPath.row)"
return cell
}
var numberOfRows = 3
func tableView(_ tableView: UITableView,numberOfRowsInSection section: Int) -> Int { numberOfRows }
func tableView(_ tableView: UITableView,heightForRowAt indexPath: IndexPath) -> CGFloat {
return 118
}
var tableView = UITableView()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
setTableVIew()
}
func setTableVIew(){
let VCframe = self.view.frame
let height = VCframe.height * 0.8
let widthx = VCframe.width
tableView.frame = CGRect(x: 0,y: 0,width: widthx,height: height)
tableView.delegate = self
tableView.dataSource = self
self.view.addSubview(tableView)
tableView.backgroundColor = .blue
tableView.register(customtv.self,forCellReuseIdentifier: "cell")
}
}
class customtv: UITableViewCell,UIImagePickerControllerDelegate,UINavigationControllerDelegate {
var imagePicker: UIImagePickerController!
func imagePickerController(_ picker: UIImagePickerController,didFinishPickingMediawithInfo info: [UIImagePickerController.InfoKey : Any]) {
// The info dictionary may contain multiple representations of the image. You want to use the original.
guard let selectedImage = info[.originalImage] as? UIImage else {
fatalError("Expected a dictionary containing an image,but was provided the following: \(info)")
}
// Set photoImageView to display the selected image.
imagedisplayer.image = selectedImage
// dismiss the picker.
dismiss(animated: true,completion: nil)
}
@objc func image(_ image: UIImage,didFinishSavingWithError error: Error?,contextInfo: UnsafeRawPointer) {
if let error = error {
// we got back an error!
let ac = UIAlertController(title: "Save error",message: error.localizedDescription,preferredStyle: .alert)
ac.addAction(UIAlertAction(title: "OK",style: .default))
present(ac,animated: true)
} else {
let ac = UIAlertController(title: "Saved!",message: "Your altered image has been saved to your photos.",animated: true)
}
}
lazy var backView : UIView = {
let view = UIView(frame: CGRect(x: 10,y: 6,width: self.frame.width,height: 110))
view.backgroundColor = .green
print(self.frame.width)
return view
}()
lazy var press : UIButton = {
let press = UIButton(frame: CGRect(x: 2,y: 3,width: 65,height: 50))
press.backgroundColor = .systemOrange
press.setTitle("import",for: .normal)
press.addTarget(self,action: #selector(importPhoto),for: .touchDown)
return press
}()
lazy var imagedisplayer : UIImageView = {
let press = UIImageView(frame: CGRect(x: 100,width: 80,height: 90))
press.backgroundColor = .systemPink
return press
}()
@objc func importPhoto(){
imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = .photoLibrary
present(imagePicker,animated: true,completion: nil)
}
override func layoutSubviews() {
backView.clipsToBounds = true
}
override func setSelected(_ selected: Bool,animated: Bool) {
super.setSelected(animated,animated: true)
addSubview(backView)
backView.addSubview(press)
backView.addSubview(imagedisplayer)
}
}
解决方法
您必须在UIImagePickerController
中显示ViewController
,而不是customtv
。
在ViewController
中:
class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource,UIImagePickerControllerDelegate,UINavigationControllerDelegate {
var numberOfRows = 3
func tableView(_ tableView: UITableView,numberOfRowsInSection section: Int) -> Int { numberOfRows }
func tableView(_ tableView: UITableView,heightForRowAt indexPath: IndexPath) -> CGFloat { 118 }
var tableView = UITableView()
var selectedIndexPath = IndexPath(row: 0,section: 0)
override func viewDidLoad() {
super.viewDidLoad()
setTableVIew()
}
func setTableVIew(){
let VCframe = view.frame
let height = VCframe.height * 0.8
let widthx = VCframe.width
tableView.frame = CGRect(x: 0,y: 0,width: widthx,height: height)
tableView.delegate = self
tableView.dataSource = self
view.addSubview(tableView)
tableView.backgroundColor = .blue
tableView.register(customtv.self,forCellReuseIdentifier: "cell")
}
func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell",for: indexPath) as! customtv
cell.textLabel?.text = "\(indexPath.row)"
cell.press.tag = indexPath.row
cell.press.addTarget(self,action: #selector(importPhoto),for: .touchDown)
return cell
}
@objc func importPhoto(_ button: UIButton) {
selectedIndexPath = IndexPath(row: button.tag,section: 0)
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = .photoLibrary
present(imagePicker,animated: true,completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController,didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
guard let selectedImage = info[.originalImage] as? UIImage else {
fatalError("Expected a dictionary containing an image,but was provided the following: \(info)")
}
guard let cell = tableView.cellForRow(at: selectedIndexPath) as? customtv else { return }
cell.imageDisplayer.image = selectedImage
tableView.reloadRows(at: [selectedIndexPath],with: .automatic)
dismiss(animated: true,completion: nil)
}
@objc func image(_ image: UIImage,didFinishSavingWithError error: Error?,contextInfo: UnsafeRawPointer) {
if let error = error {
// we got back an error!
let ac = UIAlertController(title: "Save error",message: error.localizedDescription,preferredStyle: .alert)
ac.addAction(UIAlertAction(title: "OK",style: .default))
present(ac,animated: true)
} else {
let ac = UIAlertController(title: "Saved!",message: "Your altered image has been saved to your photos.",animated: true)
}
}
}
在customtv
中:
class customtv: UITableViewCell {
lazy var backView : UIView = {
let view = UIView(frame: CGRect(x: 10,y: 6,width: self.frame.width,height: 110))
view.backgroundColor = .green
print(self.frame.width)
return view
}()
lazy var press : UIButton = {
let press = UIButton(frame: CGRect(x: 2,y: 3,width: 65,height: 50))
press.backgroundColor = .systemOrange
press.setTitle("import",for: .normal)
return press
}()
lazy var imageDisplayer : UIImageView = {
let press = UIImageView(frame: CGRect(x: 100,width: 80,height: 90))
press.backgroundColor = .systemPink
return press
}()
override func layoutSubviews() {
backView.clipsToBounds = true
}
override func setSelected(_ selected: Bool,animated: Bool) {
super.setSelected(animated,animated: true)
addSubview(backView)
backView.addSubview(press)
backView.addSubview(imageDisplayer)
}
}