swift - 通过tableview单元格中的按钮将图像导入imageview
问题描述
我想通过 tableviw 单元格中的按钮导入照片。因此特定表格视图单元格中的按钮将图像导入该特定单元格中的图像视图。因此,一个按钮将图像导入该特定单元格中的一个图像视图。我试图将照片导入代码放在 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.", preferredStyle: .alert)
ac.addAction(UIAlertAction(title: "OK", style: .default))
present(ac, 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, y: 3, 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
inViewController
而不是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.", preferredStyle: .alert)
ac.addAction(UIAlertAction(title: "OK", style: .default))
present(ac, 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, y: 3, 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)
}
}
推荐阅读
- database - 创建具有多个数据库的应用程序
- flutter - 环境中的 java_home 变量,以匹配您在 Android Studio 上的 java 安装位置
- c++ - 是否可以在 MSVC 中对隐式浮点到布尔转换发出警告?
- python - # 替换 django 中的 url 模板标签
- google-sheets - 电子表格获取单个值并计数
- vue.js - 我在哪里可以解决 ChunkLoadingError: Loading chunk nameofchunk failed in a Vue application?
- testng - 我在 BDD 工作,并尝试通过 TestNG 类运行测试场景。在我的“理智”组中,所有测试都由 Alphabet 运行,我需要一个接一个地运行它
- php - PHP 从自动下载文件的链接下载文件
- sql-server - 面向广告用户的 azure sql server 行级安全性
- php - 当我开始一个新会话时,值已设置,但在下一页我无法“看到”它们