How to detect one button in tableview cell(如何检测表格视图单元格中的一个按钮)
问题描述
如何检测 UITableviewCell 中的一个按钮,我在 UITableViewCell 中有 10 个 UIButton,接下来当我点击 UIButton 然后它检测到多个按钮,(如奇数列表).我的 UITableView 启用了分页.这是我的所有代码.
How to detect one button in UITableviewCell, I have 10 UIButton in UITableViewCell, next when I click on UIButton then it detects multiple buttons, (as like odd number list). my UITableView is with paging enabled. Here is my all code.
表格视图
class HomeViewController: UIViewController, UITableViewDelegate,UITableViewDataSource {
@IBOutlet weak var homeTableView: UITableView!
let mainArray = [["1","2","3","4"],["5","6","7","8"],["9","10","11","12"],["13","14","15","16"]]
override func viewDidLoad() {
super.viewDidLoad()
self.homeTableView.delegate = self
self.homeTableView.dataSource = self
}
func numberOfSections(in tableView: UITableView) -> Int {
return mainArray.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return mainArray[section].count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "HomeTableViewCell", for: indexPath) as! HomeTableViewCell
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return self.view.frame.size.height
}
}
TableViewCell
class HomeTableViewCell: UITableViewCell {
@IBOutlet weak var bookMarkBtn: UIButton!
@IBAction func bookMarkBtnAction(_ sender: UIButton) {
sender.isSelected = !sender.isSelected
if(sender.isSelected == true)
{
sender.setImage(UIImage(named:"favorite_blue"), for: UIControlState.normal)
}
else
{
sender.setImage(UIImage(named:"favorite_white"), for: UIControlState.normal)
}
}
}
推荐答案
要检测 UITableViewCell 中的 UIButton,您可以采用以下任一方法:
To detect a UIButton in a UITableViewCell, you can follow any of the below approaches:
1.使用 UIButton IBOutlets
您可以创建一个 IBOutlet 对应于 UITableViewCell 中的每个 UIButton 并使用这些 outlet 来识别执行哪个按钮操作.
You can create an IBOutlet corresponding to each UIButton in the UITableViewCell and use those outlets to identify which button action is performed.
示例:
class CustomCell: UITableViewCell
{
@IBOutlet weak var button1: UIButton!
@IBOutlet weak var button2: UIButton!
@IBOutlet weak var button3: UIButton!
@IBOutlet weak var button4: UIButton!
@IBOutlet weak var button5: UIButton!
@IBAction func onTapButton(_ sender: UIButton)
{
if sender === button1
{
//button1 specific code here
}
else if sender === button2
{
//button2 specific code here
}
//and so on..
}
}
<强>2.使用 UIButton Tag 属性
2. Use UIButton Tag property
您可以为 UITableViewCell 中的每个 UIButton 提供一个 tag 值,然后使用该标签来识别特定按钮.
You can provide a tag value to each of the UIButton present in the UITableViewCell and then use that tag to identify the specific button.
示例:
class CustomCell: UITableViewCell
{
@IBAction func onTapButton(_ sender: UIButton)
{
if sender.tag == 1
{
//button1 has a tag = 1
//button1 specific code here
}
else if sender.tag == 2
{
//button2 has a tag = 2
//button2 specific code here
}
//and so on..
}
}
要在 UIButton 的选中/未选中状态下设置不同的图像,您可以使用情节提要:
For setting different images in selected/unselected state of UIButton, you can use storyboard for that:
对于未选中状态:
对于选定状态:
如果您仍然遇到任何问题,请告诉我.
Let me know if you still face any issues.
这篇关于如何检测表格视图单元格中的一个按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何检测表格视图单元格中的一个按钮
基础教程推荐
- 如何从 logcat 中删除旧数据? 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
