UIImages as icons in side push menu
我想先说一下我是iOS的新手,以此作为开头。
我目前正在尝试创建一个侧面推动菜单,该菜单在单击时在每个菜单项旁边都有图标。我目前有侧面推式菜单,其中列出了几个菜单项,但是,我真的很努力地尝试让每个菜单项旁边的图像都出现。
我用下面的代码编写了侧面推送菜单并以编程方式列出了项目。
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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | import UIKit class SideMenuTable: UITableViewController { let sideMenuImages = [UIImage(named:"Main"), UIImage(named:"Departments"), UIImage(named:"Deliveries"), UIImage(named:"Warehouse"), UIImage(named:"Help")] var menu = [Menu]() @IBOutlet weak var test: UIImageView! override func viewDidLoad() { super.viewDidLoad() self.title ="Change Menu View" menu = [ Menu(title:"Main"), Menu(title:"Departments"), Menu(title:"Deliveries"), Menu(title:"Warehouse"), Menu(title:"Help") ] } override func viewWillAppear(_ animated: Bool) { if tableView.isHidden { if let selectionIndexPath = tableView.indexPathForSelectedRow { tableView.deselectRow(at: selectionIndexPath, animated: animated) } } else if tableView.isFocused { if let selectionIndexPaths = tableView.indexPathForSelectedRow { tableView.deselectRow(at: selectionIndexPaths, animated: animated) } } super.viewWillAppear(animated) } override func numberOfSections(in tableView: UITableView) -> Int { return 1 } override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return menu.count } override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier:"sideCell", for: indexPath) let menuSide: Menu // var image: UIImage = UIImage(named:".png") menuSide = menu[indexPath.row] cell.textLabel!.text = menuSide.title cell.textLabel?.textAlignment = .center return cell } override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { if(indexPath.section == 1) { if(indexPath.row == 3) { dismiss(animated: true, completion: nil) performSegue(withIdentifier:"Warehouse", sender: self) } else { NSLog("NO SELECTION AVAILABLE") } } } } |
从下面的图片中可以看到。
] 1
我已经开始尝试通过将UIImage插入原型单元中来解决我的问题。
但是,当我将UIImage作为outlets连接到上面的代码中时,收到此错误。 (注意:在出口上方的代码中名为test)
在这里我被困住了。
我需要连接此outlets,以便为我拥有的各种菜单项添加图标。
我想念什么?
为菜单项创建图标的问题是否有其他解决方法?
我需要创建一个单独的子类吗?
您需要为UITableviewcell创建一个自定义类,在其中需要创建IBOutlet来进行图像查看和标记。
现在您可以在cellForRowAtIndexPath方法中访问它。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier:"sideCell", for: indexPath) as YourCustomCell let menuSide: Menu var image: UIImage = UIImage(named:".png") menuSide = menu[indexPath.row] cell.customLabel!.text = menuSide.title cell.customLabel?.textAlignment = .center Cell.imgViewName.image = image return cell } |
此处customLabel和imgViewName是您需要从情节提要中拖入自定义单元格的IBOutlet的名称,还可以通过选择该特定单元格来在情节提要中提供自定义单元格的名称。
如果您不想创建自定义类,请在uiimageview和uilabel上设置任何标签,并按以下方式使用
1 2 3 4 5 6 7 8 | override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let lblmenu : UILabel = cell.contentView.viewWithTag(12) as! UILabel lblmenu.text = ArrMenuoption[indexPath.row] as? String let img : UIImageView = cell.contentView.viewWithTag(11) as! UIImageView img.image = UIImage.init(named: ArrMenuoptionimg[indexPath.row] as! String ) return cell } |