gpt4 book ai didi

ios - UITableview 到带图像的单元格详细信息

转载 作者:行者123 更新时间:2023-11-28 13:42:35 25 4
gpt4 key购买 nike

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

这是查看我的 Storyboard的图片链接: 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)

}

}

最佳答案

如果不创建整个模型类,您可以执行类似的操作,我将您的 RXDetail 和 RXNames 组合在一起,以便将数据保持在一起并减少在您更改顺序或添加/删除项目时出错的机会。根据要求,我更新了它以包含一组图像。我希望这会有所帮助。

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
}
}

}

看来你没有在 Storyboard 中正确设置委托(delegate),但是我改进了我的代码以在代码中设置它,并将图像的大小设置为最多限制为根的宽度view (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: Storyboard layout

仅将 UIImageView 替换为 UIScrollView,相应地设置委托(delegate)和导出。

关于ios - UITableview 到带图像的单元格详细信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55845211/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com