我有一个带有菜单选项的tableView。在这个菜单选项中,我有图像和标签。
我希望当我点击一个选项时,图标图像会将当前颜色更改为另一种颜色。
var menuNameArr : Array = [String]()
var iconImageNormal : Array = [UIImage]()
var iconImageSelected : Array = [UIImage]()
@IBOutlet weak var imgLogo: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
menuNameArr = ["Menu", "Map", "Favorites", "Orders", "Settings", "Help"]
iconImageNormal = [UIImage(named: "icon-coffee-cup-gray")!, UIImage(named: "icon-placeholder-gray")!,
UIImage(named: "icon-star-gray")!, UIImage(named: "icon-shopping-cart-gray")!,
UIImage(named: "icon-settings-gray")!, UIImage(named: "icon-help-gray")!]
iconImageSelected = [UIImage(named: "icon-coffee-cup-green")!, UIImage(named: "icon-placeholder-green")!,
UIImage(named: "icon-star-green")!, UIImage(named: "icon-shopping-cart-green")!,
UIImage(named: "icon-settings-green")!, UIImage(named: "icon-help-green")!]
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return menuNameArr.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "MenuTableViewCell", for: indexPath) as! MenuTableViewCell
cell.imgIcon.image = iconImageNormal[indexPath.row]
cell.lblMenuName.text! = menuNameArr[indexPath.row]
cell.lblMenuName.textColor = UIColor(colorLiteralRed: 183.0/255.0, green: 183.0/255.0, blue: 183.0/255.0, alpha: 1)
return cell
}
有谁知道我是怎么做到的?
答案 0 :(得分:1)
您可以添加tableview didSelectRowAtIndexPath。
的委托func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath){
self.tableView.reloadData()
if let cell = tableView.cellForRow(at: indexPath) as? MenuTableViewCell {
cell.imgIcon.image = iconImageSelected[indexPath.row]
}
}
希望它能奏效。
答案 1 :(得分:0)
首先,确保设置UITableViewDelegate,然后将以下函数添加到您的类中:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){
if let cell = tableView.cellForRow(at: indexPath) as? MenuTableViewCell {
cell.imgIcon.image = iconImageSelected[indexPath.row]
}
}