我正在尝试添加一个全新的自定义单元格,该单元格具有从库中选取图像和从文本字段中输入标题的功能,我在情节提要中有两个视图控制器(1.带有图片和两个标签的提要视图控制器,所有这些都在TableView.2. 第二个 ViewController 负责通过从库中选取图像并通过 textField 创建两个标签来添加新图像,其中两个我连接到一个类中(我不确定这是否合法)。所以我的问题是,在选择图像并输入标签并单击按钮后,对象不会附加到 UI 中,但在数组的打印语句中也会附加。
import UIKit
import Firebase
class ChatViewController: UIViewController, UITableViewDelegate, UITableViewDataSource{
override func viewDidLoad() {
super.viewDidLoad()
// cardTableView.isHidden = true
}
@IBOutlet weak var titleTextField: UITextField!
@IBOutlet weak var descriptionTextField: UITextField!
@IBOutlet weak var imageView: UIImageView!
@IBAction func AddPurchase(_ sender: UIButton) {
titles.append(titleTextField.text!)
pictures.append(imageView.image!)
descriptions.append(descriptionTextField.text!)
print(titles)
print(descriptions)
print(pictures)
}
@IBOutlet var cardTableView: UITableView!
var pictures: [UIImage] = [UIImage(named: "banan.jpg")!, UIImage(named: "pineapple.jpg")!, UIImage(named: "dragonfruit.jpg")!]
var titles: [String] = ["Banana Paradise", "Pineapple island", "The Dragon's Fruit"]
var descriptions: [String] = ["Hello", "Hello", "Hello"]
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return pictures.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cardCell", for: indexPath) as! CardCell
cell.configure(picture: pictures[indexPath.row], title: titles[indexPath.row], description: descriptions[indexPath.row])
return cell
}
//Add
@IBAction func didTapButton(_ sender: UIButton) {
let vc = UIImagePickerController()
vc.sourceType = .photoLibrary
vc.delegate = self
vc.allowsEditing = true
present(vc, animated: true)
}
}
extension ChatViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate{
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let imageShow = info[UIImagePickerController.InfoKey(rawValue: "UIImagePickerControllerEditedImage")] as? UIImage{
imageView.image = imageShow
}
picker.dismiss(animated: true, completion: nil)
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
picker.dismiss(animated: true, completion: nil)
}
}
我尝试将图像和文本附加到按钮函数中的数组。请帮我解决这个问题。