UITableview到带有图像的单元格细节

时间:2019-04-25 08:44:51

标签: ios swift xcode uitableview uiimageview

我目前有一个UITabelview显示药物列表,每种药物都通过自己的图像视图连接到自己的场景。 是否可以将UITableview仅连接到一个场景并仅根据用户在表中选择的内容更改图像?

这是查看我的故事板的图像的链接: https://ibb.co/6FcDySW

代码也会显示。

class MedicationsController: UITableViewController {
    var RXnames = [String] ()
    var RXidentities = [String] ()
    var RXdetail = [String] ()

    @IBAction func home(_ sender: Any) {
        self.dismiss(animated: true, completion: nil)
    }

    override func viewDidLoad() {

        RXnames = ["Acetaminophen", "Activated Charcoal","Adenosine","Albuterol", "Amiodarone", "Aspirin","Atropine Sulfate",
        "Calcium Chloride","Dextrose","Diltiazem","Diphenhydramine","Dopamine","Epinephrine","Etomidate","Fentanyl","Furosemide",
        "Glucagon","Glucose (Oral)","Ibuprofen","Ipratropium Bromide","Ketamine Hydrochloride","Ketoralac","Lidocaine","Lorazepam",
        "Magnesium Sulfate","Methylprednisolone","Metoprolol","Midazolam","Morphine Sulfate","Naloxone","Nitroglycerin",
        "Nitrous Oxide","Norepinephrine","Ondansetron","Oxygen","Promethazine","Racemic Epinephrine","Rocuronium","Sodium Bicarbonate",
        "Succinylcholine","Transexamic Acid","Vecuronium"]

     RXidentities = ["1","2","3","4","5","6","7","8","9","10",
            "11","12","13","14","15","16","17","18","19","20",
              "21","22","23","24","25","26","27","28","29","30",
            "31","32","33","34","35","36","37","38","39","40","41","42","43"]

      RXdetail = ["Tylenol", "CharcoAid","Adenocard","Ventolin", "Cordarone", "Bayer","Atropen",
        "CaCl","D50W","Cardizem","Benadryl","Intropin","Adrenalin","Amidate","Sublimaze","Lasix",
        "Glucagon","Glucose (Oral)","Motrin","Atrovent","Ketalar","Toradol","Lidocaine","Ativan",
        "Mag","Solu-Medrol","Lopressor","Versed","Duramorph","Narcan","Nitrostat",
        "Nitrous","Levophed","Zofran","O2","Phenergan","Rac Epi","Zemuron","Bicarb",
        "Anectine","TXA","Norcuron"]


    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return RXnames.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "RXcell")

        cell?.textLabel!.text = RXnames[indexPath.row]

      cell?.detailTextLabel!.text = RXdetail[indexPath.row]


        return cell!
    }

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

      let vcName = RXidentities[indexPath.row]
       let viewController = storyboard?.instantiateViewController(withIdentifier: (vcName))
    self.navigationController?.pushViewController(viewController!, animated: true)

    }

}

1 个答案:

答案 0 :(得分:1)

创建一个完整的模型类的时间很短,您可以在将您的RXDetail和RXName合并在一起的情况下进行类似的操作,以使数据保持在一起,并减少了更改订单或添加/删除项目时出现错误的机会。根据要求,我已对此进行更新,以包括图像数组。我希望这会有所帮助。

class MedicationsController: UITableViewController {

    var RXItems : [(name:String, detail:String, images:[UIImage])]!

    override func viewDidLoad() {
        RXItems = [("Acetaminophen", "Tylenol", [#imageLiteral(resourceName: "example-image"), #imageLiteral(resourceName: "example-image")]),
                   ("Activated Charcoal", "CharcoAid", [#imageLiteral(resourceName: "example-image")])] //etc
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return RXItems.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "RXcell")

        cell?.textLabel?.text = RXItems[indexPath.row].name

        cell?.detailTextLabel?.text = RXItems[indexPath.row].detail


        return cell!
    }

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        self.performSegue(withIdentifier: "showDetailsSegue", sender: indexPath.row)
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if(segue.identifier == "showDetailsSegue") {
            let viewController = segue.destination as! DetailsViewController
            viewController.images = RXItems[sender as! Int].images
            viewController.title = RXItems[sender as! Int].name
        }
    }

}

似乎您没有在情节提要中正确设置委托,但是我改进了我的代码,既在代码中设置了委托,又将图像的大小设置为最多限制为根的宽度视图(self.view),同时保持宽高比:

import UIKit
import AVKit
class DetailsViewController: UIViewController, UIScrollViewDelegate {

    var images : [UIImage]?
    var imageViews = [UIImageView]()
    @IBOutlet private weak var scrollView: UIScrollView!

    private var _contentView : UIView?
    var contentView: UIView {
        if(_contentView == nil) {
            _contentView = UIView.init(frame: CGRect.zero)
            self.scrollView.addSubview(_contentView!)
        }
        return _contentView!
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        self.scrollView.delegate = self
        if let images = self.images {
            var contentSize = CGSize(width: self.view.frame.size.width, height: 0)
            for img in images {
                let size = CGSize(width: self.view.frame.size.width, height: CGRect.infinite.height)
                let rect = CGRect(origin: CGPoint.zero, size: size)
                let iv = UIImageView.init(frame: CGRect(origin: CGPoint(x:0, y:contentSize.height), size: AVMakeRect(aspectRatio: img.size, insideRect: rect).size))
                contentSize.height += iv.frame.size.height
                iv.image = img
                self.contentView.addSubview(iv)
                self.imageViews.append(iv)
            }
            self.contentView.frame.size = contentSize
            self.scrollView.contentSize = contentSize
        }
    }

    override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
        var contentSize = CGSize(width: self.view.frame.size.width, height: 0)
        for iv in self.imageViews {
            let size = CGSize(width: size.width, height: CGRect.infinite.height)
            let rect = CGRect(origin: CGPoint.zero, size: size)
            iv.frame = CGRect(origin: CGPoint(x:0, y:contentSize.height), size: AVMakeRect(aspectRatio: iv.image!.size, insideRect: rect).size)
            contentSize.height += iv.frame.size.height
        }
        self.contentView.frame.size = contentSize
        self.scrollView.contentSize = contentSize
    }

    func viewForZooming(in scrollView: UIScrollView) -> UIView? {
        return self.contentView
    }
}

然后将您的故事板设置为: Storyboard layout

仅将UIImageView替换为UIScrollView,并相应地设置委托和出口。