我在我的tableView中创建了5个单元格,第一个单元格是imageCell,它由两部分组成。我的第一个单元格是imageCell,它由两部分组成。一个是 userImageView 另一个是 用户名文本. 我做了所有的指示,如编写委托协议和其他。我没有收到任何错误,构建成功,但它没有工作,我发现我没有在cellForRowAt indexPath 0处定义userImageView。我发现我没有在cellForRowAt indexPath 0处定义userImageView,我只定义了userNameText。我想这可能是原因。我试着像你看到的底部那样定义,但有一个问题。但是有一个问题。UIImage() 如果我不写任何关于userImageView的内容,我可以从分配的Assets中看到图片,我可以点击,但什么都没有发生。如果有人能帮助我,我将非常高兴。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.row == 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: "imageCell") as! imageCell
cell.userImageView.image = UIImage()
cell.userNameText.text = ""
return cell
这是我的UITableViewController。
import UIKit
protocol ImagePickerDelegate {
func pickImage()
}
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.row == 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: "imageCell") as! imageCell
cell.userImageView.image = UIImage()
cell.userNameText.text = ""
return cell
} else if indexPath.row == 1 {
let cell = tableView.dequeueReusableCell(withIdentifier: "TCNumberCell") as! TCNumberCell
cell.TCnumberText.text = ""
return cell
} else if indexPath.row == 2 {
let cell = tableView.dequeueReusableCell(withIdentifier: "birthdayCell") as! birthdayCell
cell.birthdayText.text = ""
return cell
} else if indexPath.row == 3 {
let cell = tableView.dequeueReusableCell(withIdentifier: "companyCell") as! companyCell
cell.companyNumberText.text = ""
return cell
} else {
let cell = tableView.dequeueReusableCell(withIdentifier: "schoolCell") as! schoolCell
cell.schoolNumberText.text = ""
return cell
}
}
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
tableView.delegate = self
tableView.dataSource = self
}
func pickImage() {
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerController.SourceType.photoLibrary
imagePicker.allowsEditing = false
self.present(imagePicker, animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
let cell:imageCell = tableView.cellForRow(at: IndexPath(row: 0, section: 0)) as! imageCell
cell.userImageView.image = image
}
picker.dismiss(animated: true, completion: nil)
}
}
这是我的imageCell
import UIKit
class imageCell: UITableViewCell, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
@IBOutlet weak var userNameText: UITextField!
@IBOutlet weak var userImageView: UIImageView!
var delegate : ImagePickerDelegate?
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
addTapGestureRecognizer(for: userImageView)
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
func addTapGestureRecognizer(for view: UIImageView) {
let tap = UITapGestureRecognizer(target: self, action: #selector(pickImage))
tap.numberOfTapsRequired = 1
view.isUserInteractionEnabled = true
view.addGestureRecognizer(tap)
}
@objc func pickImage() {
delegate?.pickImage()
}
}
该错误是因为UITableViewCell没有功能。present
. 你应该有一个回调到显示这个表格视图单元格的UIViewController,并让它向UIImagePickerController展示 presentViewController(imagePicker, animated: true, completion: nil)
.
如果你不清楚如何进行回调,请看这里的这个答案。https:/stackoverflow.coma229042724608154
本质上,你在你的TableViewCell类中创建了一个协议,并将ViewController分配为委托人,然后当你的TableViewCell想要显示UIImagePickerController时,ViewController将有一个方法触发。然后当你的TableViewCell想要显示UIImagePickerController时,ViewController将有一个方法被触发。
你给单元格中的imageView分配了一个空图像。在你现有的代码中检查Table view delegate函数。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.row == 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: "imageCell") as! imageCell
cell.userImageView.image = UIImage()
按照下面的指令进行操作
中为所选图像添加一个变量。视图控制器.
class ViewController : UIViewController, UITableViewDelegate, UITableViewDataSource, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
var selectedImage = UIImage(named:"placeholder") // use as variable. add placeholder image if require
更新参考资料 cell.userImageView.image. 在单元格去库时引用所选图像。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.row == 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: "imageCell") as! imageCell
cell.userImageView.image = selectedImage
cell.userNameText.text = ""
return cell
一旦用户从选取器中选取图像,更新所选图像变量。确保 IF 条件应该为真,才能分配图像。
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
self.selectedImage = image
let cell:imageCell = tableView.cellForRow(at: IndexPath(row: 0, section: 0)) as! imageCell