在单元格图像中传递选定的图像

问题描述 投票:-4回答:2

我的应用程序包括2个水龙头。首先点击是TableView,第二点是VC。当我在第一个VC上时,我选择了图片并按下按钮,该按钮应该在单元格图像上传输所选图片。

我正在尝试在UserDefaults中保存图像,但事实证明所有行都具有相同的图片

在我的VC中,我通过以下方法保存TextField:

func saveButton() { 
let itemsObject = UserDefaults.standard.object(forKey: "items")

var items:[String]

if let tempItems = itemsObject as? [String] {

    items = tempItems

    items.append(nameTextField.text!)

} else {

    items = [nameTextField.text!]
}
UserDefaults.standard.set(items, forKey: "items")
    nameTextField.text = ""
    navigationController?.popViewController(animated: true)
}

并选择图像

然后点击“保存”按钮

在我的表视图中:

override func viewDidAppear(_ animated: Bool) {
        //     Load Item Func
        let itemsObject = UserDefaults.standard.object(forKey: "items")

        if let tempItems = itemsObject as? [String] {

            items = tempItems
        }
        table.reloadData()
    }

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CellPrototype

        // Configure the cell...
        cell.pointNameLabel.text = items[indexPath.row]
         return cell
    }
ios swift uitableview cell
2个回答
0
投票

你可以将图像从cell传递给detailViewController这里是partail伪代码

tableView (cell , didSelectCellAtIndexPath) {
 if let cell = cell as? CustomCell {
   let image = cell.image 
   let detailVC = DetailViewController(..) // instantiate from storyboard
    detailVC.selectedImage = image 
    navigationViewController.push(detailVC)
}
}

Class DetailVC: UIViewController {
 var selectedImage: UIImage!

 // can access image anywhere in this class after passing  
}

0
投票

您可以通过在Second VC中创建变量来传递图像

UITableView委托方法

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    let cell = tableView.cellForRow(at: indexPath) as? YourCell

    if let DetailVC = self.storyboard?.instantiateViewController(withIdentifier: "DetailViewController") as? DetailViewController{

        DetailVC.Selected_image = cell.YourImageView.image
        self.navigationController?.pushViewController(DetailVC,animated: true)
    }

}

在第二个VC中创建变量

class DetailViewController: UIViewController {

    var Selected_image: UIImage?

    override func viewDidLoad() {
        super.viewDidLoad()
        print(Selected_image)
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.