如何在swift 3中的tableview单元格内单击按钮标题

how to change the button title on click inside a tableview cell in swift 3(如何在swift 3中的tableview单元格内单击按钮标题)
本文介绍了如何在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单元格内单击按钮标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Why local notification is not firing for UNCalendarNotificationTrigger(为什么没有为UNCalendarNotificationTrigger触发本地通知)
iOS VoiceOver functionality changes with Bundle Identifier(IOS画外音功能随捆绑包标识符而变化)
tabbar middle tab out of tabbar corner(选项卡栏中间的选项卡角外)
Pushing UIViewController above UITabBar(将UIView控制器推送到UITabBar上方)
Dropbox Files.download does not start when number of files in folder is gt; 1000(当文件夹中的文件数为1000时,Dropbox Files.Download不会启动)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)