how to change the button title on click inside a tableview cell in swift 3(如何在swift 3中的tableview单元格内单击按钮标题)
问题描述
我有一个 tableview 单元格,里面有图像视图、标签和一个 ui 按钮,我需要在点击时更改按钮标题文本和背景颜色.但是当我尝试从多个单元格中做按钮时,效果是一样的.请帮帮我!
I got a tableview cell inside that i have image view , label and a ui button , i need to change the button title text and background colour on click . But when i try to do button from multiple cell is having the same effect . Please do help me !
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell1", for: indexPath) as! subTableViewCell
cell.catNameLabel.text = categoryNameArray[indexPath.row]
cell.descLabel.text = descriptionArray[indexPath.row]
cell.freqLabel.text = frequency[indexPath.row] + " at " + frequencyTime[indexPath.row]
let url = "http://wiinnova.com/tenly/image/category_image/(categoryImageArray[indexPath.row])"
cell.img.imageFromServerURL(urlString: url)
cell.button.tag = indexPath.row
return cell
}
推荐答案
首先你应该用一个 Array
来管理点击/改变 UIButton
for reusableCell
否则,如果你上下滚动你的 tableView 那么它将被删除效果
First of all you should take one Array
for manage clicked/changed UIButton
for reusableCell
otherwise if you scroll your tableView Up-Down then It will be remove effect
我正在给出我的逻辑.
1) 取一个可变数组名称为 temArray
(大小等于您的 tableView 的行) 并且每个索引都有 0 值.你可以做到通过简单的重复值0.
1) Take one mutable Array name is temArray
(Size equal to your tableView's Row) and it has 0 value for each index. you can do it by easy repeat value 0.
2) 在你的 cellForRowAtIndexPath
数据源方法检查
2) in you cellForRowAtIndexPath
datasource method check
if temArray[indexPath.row] == 0 {
/// Write code for default UIButton - That has normal behavior means not changed title and no set background color
}
else {
/// Write code for What you want to keep UIButton title and background color
}
cell.yourButtonObject.tag = indexPath.row
cell.yourButtonObject.addTarget(self, action:#selector(handleButtonClicked(_:)), for: .touchUpInside).
3) 并添加 handleButtonClicked
方法并更改 temArray
值
3) And add handleButtonClicked
method and change temArray
value
func handleButtonClicked(_ sender: UIButton) {
let myIndexPath = NSIndexPath(row: sender.tag, section: 0)
let cell = tblViewPref.cellForRow(at: myIndexPath as IndexPath)
if temArray[sender.tag] == 0 {
/// You can access your button by
// cell.myButton..... change here text and background color
// And change "temArray"
temArray[sender.tag] = 1
}
else {
/// You can access your button by
// cell.myButton..... change here text and background color
// And change "temArray"
// SET DEFULT BUTTON TITLE AND BACKGROUND COLOR
temArray[sender.tag] = 0
}
}
因此,当您向上滚动表格时,temArray
将由 cellForRowAtIndexPath
数据源方法中 indexPath
处的值管理.
So when you scroll your table Up-down temArray
will be managed by it's value at indexPath
in cellForRowAtIndexPath
Datasource method.
这篇关于如何在swift 3中的tableview单元格内单击按钮标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在swift 3中的tableview单元格内单击按钮标题
基础教程推荐
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01