【发布时间】:2021-04-17 02:02:52
【问题描述】:
我希望我的 swift 代码使用委托将照片从照片库导入特定的表格视图单元格。现在发生的事情是将照片导入到始终进入第一个单元格。因此,如果用户单击第二个单元格中的导入按钮,照片应该转到第二个单元格。标签不工作。如果可能,我想使用委托。
import UIKit
class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource,CustomTvCellDelegateadd, UIImagePickerControllerDelegate & UINavigationControllerDelegate {
@objc func addCeller(_ customTV: CustomTv, location: CGPoint) {
selectedIndexPath = IndexPath(row: 0, section: 0)
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = .photoLibrary
present(imagePicker, animated: true, completion: nil)
}
var cellCount = 5
var tableview = UITableView()
var selectedIndexPath = IndexPath(row: 0, section: 0)
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.pic.image = selectedImage
tableview.reloadData()
dismiss(animated: true, completion: nil)
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return cellCount // use a variable here so you can change it as you delete cells, else it will crash
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 118
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CustomTv
cell.addd.tag = indexPath.row
cell.delegatef = self
cell.addd.addTarget(self, action: #selector(addCeller), for: .touchDown)
return cell
}
override func viewDidLoad() {
super.viewDidLoad()
tableview.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height * 0.8)
view.addSubview(tableview)
tableview.register(CustomTv.self, forCellReuseIdentifier: "cell")
tableview.delegate = self
tableview.dataSource = self
}
}
// you need to declare a cell delegate that will let your tableview know when the button was tapped on the cell
protocol CustomTvCellDelegateadd: AnyObject {
func addCeller(_ customTV: CustomTv, location: CGPoint)
}
class CustomTv: UITableViewCell { // it's good form to Pascal case your class name ;)
weak var delegatef: CustomTvCellDelegateadd?
lazy var backView : UIView = {
let view = UIView(frame: CGRect(x: 10, y: 6, width: self.frame.width , height: 110))
view.backgroundColor = .green
return view
}()
lazy var addd : UIButton = {
let btn = UIButton(frame: CGRect(x: 250-25, y: 0, width: 100 , height: 55))
btn.backgroundColor = .brown
btn.setTitle("Add", for: .normal)
btn.titleLabel?.textAlignment = .center
return btn
}()
lazy var pic : UIImageView = {
let btn = UIImageView(frame: CGRect(x: 50-25, y: 6, width: 100 , height: 110))
btn.backgroundColor = .systemPink
return btn
}()
override func layoutSubviews() {
backView.clipsToBounds = true
backView.frame = CGRect(x: 0, y: 6, width: bounds.maxX , height: 110)
// moved these calls here instead of on setSelected(_selected:animated:)
addSubview(backView)
backView.addSubview(addd)
backView.addSubview(pic)
}
}
【问题讨论】:
标签: swift uitableview tags protocols delegation