所以在我的應用程式中,我目前有一個帶有自定義單元格的 tableView,其中有一個 imageView。這是一張供參考的圖片,以查看它的外觀,這是定義自定義單元格的代碼:
class GearComponentTableViewCell: UITableViewCell {
var delegate:Stepper?
var tableViewCellPosition: Int! = nil
// Image
@IBOutlet weak var itemImage: UIImageView!
// Name
@IBOutlet weak var itemName: UILabel!
// Weight
@IBOutlet weak var itemWeight1: UILabel!
@IBOutlet weak var itemWeight2: UILabel!
// Quanity
@IBOutlet weak var itemQuanity: UILabel!
@IBAction func stepperPressed (_ sender: UIStepper!) {
if (sender.value == 1) {
sender.value = 0
delegate?.stepperWasPressed(didIncrease: true, namePassed: itemName.text!, userindexPath: tableViewCellPosition)
} else if (sender.value == -1) {
sender.value = 0
delegate?.stepperWasPressed(didIncrease: false, namePassed: itemName.text!, userindexPath: tableViewCellPosition)
}
}
// Notes
@IBOutlet weak var itemNotes: UILabel!
}
這些單元格是可點擊的,當點擊它們時,它們會顯示一個警報,其中包含用于將影像添加到您選擇的單元格的選項:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("User selected item: \(indexPath.row) and name is \(itemArray[indexPath.row].name)")
showPopUp(itemChosen: indexPath)
}
這是彈出/警報的邏輯:
func showPopUp(itemChosen: IndexPath) {
let alert = UIAlertController(title: "Add Photo To Item", message: .none, preferredStyle: .actionSheet)
// alert actions
let action1 = UIAlertAction(title: "Take Photo", style: .default) { action in
self.takePhoto()
}
let action2 = UIAlertAction(title: "Import Photo", style: .default) { action in
self.importPhoto(position: itemChosen)
}
let action3 = UIAlertAction(title: "Cancel", style: .destructive)
alert.addAction(action1)
alert.addAction(action2)
alert.addAction(action3)
present(alert, animated: true,completion: nil)
}
忽略該takePhoto()
函式,因為它還沒有被使用,但在它的正下方,我正在呼叫該importPhoto()
函式,該函式采用您選擇的專案的位置:
func importPhoto(position: IndexPath) {
print("User chose to import photo")
let picker = UIImagePickerController()
picker.allowsEditing = true
picker.delegate = self
present(picker, animated: true) {
print("cell tapped \(position)")
self.cellChosen = position
}
}
然后它為您選擇的專案設定一個全域變數(我知道這不是一個好的做法,而不是傳遞實際的 indexPath),我用來傳遞給 imagePickerControllerDelegate 函式:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
guard let image = info[.editedImage] as? UIImage else { return }
dismiss(animated: true)
if let cell = tableView(gearTableView, cellForRowAt: cellChosen) as? GearComponentTableViewCell {
cell.itemImage.image = image
self.updateUI()
print("item image set for the item at position: \(cellChosen.row)")
}else {
print("did not set item image")
}
}
這就是我現在遇到問題的地方。當呼叫該委托函式時,它會成功列印 if/else 陳述句中的真實情況,并且實際上并未設定照片,并且在設定時我沒有收到任何錯誤。我收到的唯一錯誤是當我從帖子"Changing the translatesAutoresizingMaskIntoConstraints property of a UICollectionViewCell that is managed by a UICollectionView is not supported, and will result in incorrect self-sizing."
中的第一張照片中點擊右側欄按鈕項以將新單元格添加到 tableView 時。在實際創建單元格之前,我需要另一個帶有文本欄位的警報形式的一些資訊,以獲取有關您正在創建的專案的一些資訊,我不確定該錯誤是否真的與我當前正在處理的內容相關。
關于如何在單元格中設定 imageView 的影像的任何想法?我已經嘗試將Layout
我的 imageView 從更改Inferred (constraints)
為Auto-resizing mask
并且任何新單元格都停止出現...
這是我的存盤庫/我正在使用的 viewController 的鏈接。
uj5u.com熱心網友回復:
這是因為您沒有更新表視圖的資料源上的影像。當您使單元格出列時更新,但您呼叫呼叫 reloadData() 的 updateUI。
例如,您的方法imagePickerController可以像這樣作業:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
guard let image = info[.editedImage] as? UIImage else { return }
dismiss(animated: true)
let item = itemArray[cellChosen.row]
let userSubmittedItem = GearItem(itemName: item.name,
itemImage: image,
itemWeight1: item.weight1,
itemWeight2: item.weight2,
itemQuantity: item.quantity,
itemNotes: item.notes)
itemArray[cellChosen.row] = userSubmittedItem
self.updateUI()
}
uj5u.com熱心網友回復:
將方法 imagePickerController(_ picker:didFinishPickingMediaWithInfo:) 的實作更改為
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
guard let image = info[.editedImage] as? UIImage else { return }
picker.dismiss(animated: true)
itemArray[cellChosen.row].image = image
gearTableView.reloadRows(at: [cellChosen], with: .none)
}
當您需要更新資料源模型的影像時。您更新模型并呼叫重新加載行。
關于為什么你的代碼不起作用,在 處設定一個斷點func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
,你會發現你的 imageView 的影像在上面的方法中被重置了。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/516272.html