[英]how to change button background colour in tableview cell swift3
I have one UITableViewCell
. 我有一个
UITableViewCell
。 In TableViewCell, I have 7 buttons in single row. 在TableViewCell中,我在单行中有7个按钮。 I want to play music.
我想播放音乐。 When i press button1, its background colour should be changed.
当我按下button1时,其背景色应更改。 If I pressed button2, its background colour should be changed, and button1 would be deselect.
如果按了button2,则应更改其背景色,然后取消选择button1。 I also set the tag for each button in row, but i don't understand, how to do?
我也为行中的每个按钮设置标签,但我不知道该怎么办? Sound is playing fine but i am unable to select and deselect buttons.
声音播放正常,但我无法选择和取消选择按钮。 I am using
swift3
. 我正在使用
swift3
。 Thanks in advance. 提前致谢。
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> tableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "PlayerCell") as! tableViewCell
cell.Btn_one.addTarget(self, action: #selector(TableViewController.BtnAction_One), for: .touchUpInside)
//For example
if (cell.Btn_one.tag == 1){
cell.Btn_one.backgroundColor = .white
}else if (cell.Btn_two.tag == 2){
cell.Btn_two.backgroundColor = .red
}
}
@IBAction func BtnAction_One(_ sender: UIButton) {
self.play()
}
@Chetan solution for this : first one @Chetan解决方案:第一个
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cellData", for: indexPath)
let btn1 = cell.viewWithTag(1) as! UIButton
let btn2 = cell.viewWithTag(2) as! UIButton
let btn3 = cell.viewWithTag(3) as! UIButton
let btn4 = cell.viewWithTag(4) as! UIButton
let btn5 = cell.viewWithTag(5) as! UIButton
let btn6 = cell.viewWithTag(6) as! UIButton
btn1.addTarget(self, action: #selector(btnClicked(sender:)), for: .touchUpInside)
btn2.addTarget(self, action: #selector(btnClicked(sender:)), for: .touchUpInside)
btn3.addTarget(self, action: #selector(btnClicked(sender:)), for: .touchUpInside)
btn4.addTarget(self, action: #selector(btnClicked(sender:)), for: .touchUpInside)
btn5.addTarget(self, action: #selector(btnClicked(sender:)), for: .touchUpInside)
btn6.addTarget(self, action: #selector(btnClicked(sender:)), for: .touchUpInside)
return cell
}
when this will to btnClicked ` func btnClickec(sender:UIButton){ sender.isSelected = true 何时将btnClicked`func btnClickec(sender:UIButton){sender.isSelected = true
let cell = sender.superview?.superview as! UITableViewCell
let btn1 = cell.viewWithTag(1) as! UIButton
let btn2 = cell.viewWithTag(2) as! UIButton
let btn3 = cell.viewWithTag(3) as! UIButton
let btn4 = cell.viewWithTag(4) as! UIButton
let btn5 = cell.viewWithTag(5) as! UIButton
let btn6 = cell.viewWithTag(6) as! UIButton
let arr = [btn1,btn2,btn3,btn4,btn5,btn6];
for index in 0...5{
let btn = arr[index]
if index+1 == sender.tag{
btn.isSelected = true
}else{
btn.isSelected = false
}
}
}
` `
And second one : or you can collection view inside tableviewcell also 第二个:或者您也可以在tableviewcell内收集视图
Well, I suggest you to subclass from UIButton, like this: 好吧,我建议您从UIButton继承类,如下所示:
class CustomButton: UIButton {
var isPressed = false
override init(frame: CGRect) {
super.init(frame: frame)
}
convenience init(frame: CGRect, title: String, tag: Int) {
self.init(frame: frame)
self.frame = frame
setTitle(title, for: .normal)
setTitleColor(.white, for: .normal)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
switch tag {
case 0: //Play a music
if isPressed{
backgroundColor = .black
} else {
backgroundColor = .clear
}
case 1: //Play a video
if isPressed{
backgroundColor = .black
} else {
backgroundColor = .clear
}
default: //Other
if isPressed{
backgroundColor = .black
} else {
backgroundColor = .clear
}
}
if isPressed { isPressed = false } else { isPressed = true }
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
} }
After that, in your cell I'd have some like... 之后,在您的单元格中,我会喜欢...
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: .default, reuseIdentifier: nil)
let musicB = CustomButton(frame: CGRect(x: 0, y: 0, width: 0, height: 0), title: "1", tag: 1)
addSubview(musicB)
}
This is how I did it: 这是我的方法:
first the function: 首先功能:
func switchColor(isWhite: Bool, sender: UIButton) {
if isWhite == true {
sender.backgroundColor = UIColor.clear
sender.setTitleColor(UIColor.white, for: UIControlState.normal)
}else if isWhite == false {
sender.backgroundColor = UIColor.white
sender.setTitleColor(UIColor.clear, for: UIControlState.normal)
}
Then where I call it: 然后我称之为:
@IBAction func RepeatWeeklyPressed(_ sender: UIButton) {
if sender.backgroundColor == UIColor.white {
switchColor(isWhite: true, sender: sender)
}else {
switchColor(isWhite: false, sender: sender)
}
print("Pressed a repeat button")
}
Hope this helps! 希望这可以帮助!
You'll want to subclass the button as CustomButton or whatever you would like to call it. 您需要将按钮归类为CustomButton或您想调用的任何子类。 And you'll need to create a delegate to let your viewcontroller know whats up.
而且,您需要创建一个委托以使您的视图控制器知道最新情况。
Create a func that takes in an integer. 创建一个接受整数的函数。 This will be the code that'll take in your button's tag and preform some code.
这将是将包含在按钮标签中并执行一些代码的代码。 @Gabs had the right idea there.
@Gabs在这里有正确的想法。
func select(index: Int) {
switch sender.tag {
case 0: //do something
case 1: //do something
}
}
Create a single IBAction then link all your buttons to it. 创建一个IBAction,然后将所有按钮链接到它。 Inside, call the method created above
在内部,调用上面创建的方法
@IBAction func buttonTapped(_ sender: Any) {
select(index: sender.tag)
}
all that's missing is the viewController being informed on which button has been tapped. 所缺少的只是viewController被告知点击了哪个按钮。 Set a delegate for it in your CustomCell view file and have it capture the sender.tag in your IBAction.
在CustomCell视图文件中为其设置一个委托,并使其在IBAction中捕获sender.tag。 Be sure to conform your viewcontroller too so the delegation works.
确保也符合您的视图控制器的要求,以便进行委派。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.