【问题标题】:how to change the button title on click inside a tableview cell in swift 3如何在swift 3中的tableview单元格内单击按钮标题
【发布时间】:2017-08-30 07:42:47
【问题描述】:

我有一个 tableview 单元格,里面有图像视图、标签和一个 ui 按钮,我需要在点击时更改按钮标题文本和背景颜色。但是当我尝试从多个单元格中做按钮时,效果是一样的。请帮帮我!

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 
}

【问题讨论】:

  • 点击按钮时重新加载你的tableview
  • 我重新加载但没有变化..同样的问题

标签: ios swift uibutton


【解决方案1】:

您根本不需要分配button.tag,方法如下:

首先将target 添加到您的按钮。喜欢:

cell.button.addTarget(self, action:#selector(handleRegister(_:)), for: .touchUpInside). 

然后:

//This function will be called for each button click. 

func handleRegister(sender: UIButton) {

let buttonPosition = sender.convert(CGPoint.zero, to: YOUR_TABLE_VIEW)
let indexPath: IndexPath? = YOUR_TABLE_VIEW.indexPathForRow(at: buttonPosition)

let cell = YOUR_TABLE_VIEW.cellForRow(at: indexPath as IndexPath)

//Now change the text and background colour
cell.button.setTitle("Button Title",for: .normal)
cell.button.backgroundColor = UIColor.blueColor()

   //...
}

要跟踪更改的按钮背景和标题,您需要一个数组,正如 @Tofaani Kaanudo 的回答所建议的那样。

【讨论】:

  • 我已经尝试过了,但同时其他行中的按钮也有同样的效果
  • 您能用您尝试过的代码更新您的问题吗?
  • tableView Scroll Up-Down 时不起作用,因为 cell 可以重复使用。
  • 是的,抱歉,我需要使用converPoint 函数将单元格放在此处。查看更新的答案
  • 是的,当我向上向下滚动时,单元格正在重用..所以我可以做些什么来避免它..我可以使用 tableview.reload() 以及我可以在哪里使用它
【解决方案2】:

首先你应该拿一个Array 来管理点击/更改UIButton 用于reusableCell 否则如果你上下滚动你的tableView 那么它将被删除效果

我正在给出我的逻辑。

1) 取一个可变数组名称为temArray(大小等于tableView 的行),并且每个索引都有0 值。 you can do it by easy repeat value 0.

2)在你cellForRowAtIndexPath数据源方法检查

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

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 的值管理。

【讨论】:

  • 第 2 步要写什么,即 if case 和 else case
  • @ShafeerPuthalan - temArray[indexPath.row] == 0 表示您还没有点击按钮,所以将其设为默认标题和默认颜色,并在 else 部分 中写入与您在 handleButtonClicked 方法中编写的按钮代码相同。
  • 当我们再次点击按钮并将按钮设置为默认标题和默认颜色时会怎样
  • 很高兴帮助您接受我的回答,以便其他有相同问题的开发人员使用此代码。
  • @ShafeerPuthalan 如果真的对您有帮助,您应该接受 Tofaani Kaanudo 的回答!
【解决方案3】:

在tableview的didSelectRowAtIndexPathdelegate中设置按钮的标题和背景颜色属性

cell.button.setTitle("title", for: .normal)
cell.button.backgroundColor = .darkGray

【讨论】:

  • 所有按钮都在改变我想改变唯一被点击的按钮
  • 好吧,你必须检查你的按钮标签,如果匹配就改变属性
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-25
  • 1970-01-01
  • 1970-01-01
  • 2016-12-20
相关资源
最近更新 更多