如何在swift 3

时间:2017-08-30 07:42:47

标签: ios swift uibutton

我在里面有一个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 
}

3 个答案:

答案 0 :(得分:3)

首先,您应该使用一个Array来管理点击/更改UIButton reusableCell,否则如果你滚动你的tableView Up-Down然后它将会删除效果

我正在给出我的逻辑。

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将通过indexPath数据源方法中cellForRowAtIndexPath处的值进行管理。

答案 1 :(得分:2)

您根本不需要分配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 的回答建议。

答案 2 :(得分:0)

在tableview' didSelectRowAtIndexPath委托

中设置按钮的标题和背景颜色属性
cell.button.setTitle("title", for: .normal)
cell.button.backgroundColor = .darkGray