2017-01-11 1 views
0

메뉴 옵션이있는 tableView가 있습니다. 이 메뉴 옵션에는 이미지와 레이블이 있습니다.Swift의 tableView에서 이미지 색상을 변경하십시오. 3

하나의 옵션을 클릭하면 아이콘 이미지가 현재 색상을 다른 색상으로 변경하고 싶습니다.

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

, 당신이 다음 클래스에 다음 함수를 추가, 당신의 UITableViewDelegate를 설정합니다

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

    if let cell = tableView.cellForRow(at: indexPath) as? MenuTableViewCell { 
     cell.imgIcon.image = iconImageSelected[indexPath.row] 
    } 
} 
+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] 
    } 
} 

희망.

+0

감사합니다! 신속한 3을 사용하기 때문에 @toddg 솔루션을 사용했습니다. –

관련 문제