自定义UITableViewCell中的按钮操作会影响其他单元格 [英] Button action in custom UITableViewCell affects other cells

查看:72
本文介绍了自定义UITableViewCell中的按钮操作会影响其他单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 TablewView ,其原型 UITableViewCell 有自己的类,



自定义UITableViewCell类

  import UIKit 

class H_MissingPersonTableViewCell:UITableViewCell {

@IBOutlet weak var strName:UILabel!
@IBOutlet weak var imgPerson:UIImageView!
@IBOutlet weak var Date1:​​UILabel!
@IBOutlet weak var Date2:UILabel!
@IBOutlet weak var MainView:UIView!
@IBOutlet weak var btnGetUpdates:UIButton!
@IBOutlet weak var btn评论:UIButton!
@IBOutlet weak var btnReadMore:UIButton!


覆盖func awakeFromNib(){
super.awakeFromNib()
//初始化代码
}

覆盖func setSelected(selected:Bool,animated:Bool){
super.setSelected(selected,animated:animated)

//配置所选状态的视图
}

}

TableView加载完美,dataSource和委托工作正常。但是,当我在IndexPath.row = 0(零)处单击按钮,然后向下滚动时,我会看到随机(?)或其他单元格也因为在第0行中单击按钮而突出显示...

  func tableView(tableView:UITableView, cellForRowAtIndexPath indexPath:NSIndexPath) - > UITableViewCell {

println(called)
var cell:CustomCell


cell = tableView.dequeueReusableCellWithIdentifier(CustomCellID,forIndexPath:indexPath)如! CustomCell
cell.strName.text = self.names [indexPath.row]
cell.imgPerson.image = UIImage(名称:\(self.persons [indexPath.row]))!

cell.btnGetUpdates.layer.borderColor = UIColor.darkGrayColor()。CGColor
cell.btnGetUpdates.layer.borderWidth = 1
cell.btnGetUpdates.layer.cornerRadius = 5.0


cell.btnComment.layer.borderColor = UIColor.darkGrayColor()。CGColor
cell.btnComment.layer.borderWidth = 1
cell.btnComment.layer.cornerRadius = 5.0

cell.btnReadMore.layer.borderColor = UIColor.darkGrayColor()。CGColor
cell.btnReadMore.layer.borderWidth = 1
cell.btnReadMore.layer.cornerRadius = 5.0

cell.dateMissingPersonPlace.text = self.MissingPeoplePlace [indexPath.row]
cell.dateMissingPersonSince.text = self.MissingPeopleSince [indexPath.row]

cell .btnGetUpdates.tag = indexPath.row
cell.btnGetUpdates.addTarget(self,action:GetUpdatesButton:,forControlEvents:.TouchUpInside)

return cell

}
cell.btnGetUpdates 中的$ c>

,我在我的CellForIndex代码的最后一部分中看到了一个动作, GetUpdatesButton:



这是代码:

  func GetUpdatesButton(发件人:UIButton){
println(Button Clicked \(sender.tag))

var sen:UIButton = sender
var g :NSIndexPath = NSIndexPath(forRow:sen.tag,inSection:0)
var t:CustomCell = self.myTableView.cellForRowAtIndexPath(g)as! CustomCell
t.btnGetUpdates.backgroundColor = UIColor.redColor()
}

问题是,不只是索引0中的按钮正在突出显示,但我还会看到其他索引/行中的随机按钮突出显示为向下滚动我的tableView。



我哪里出错了,我怎么才能更新我点击的按钮...而不是其他按钮..



我有20个项目(20行)和时间我点击第0行,第3,6,9行....按钮也有突出显示的按钮。



ROW 0,点击按钮





ROW 3,按钮也点击了,虽然我没有真正点击它。

解决方案

这是由于UITableview具有可重用单元策略而发生的。要解决此问题,您需要在cellForRowAtIndexPath方法中维护一个选定项目数组,您必须验证所选项目数组正在保持此按钮的天气。如果是,则应用选择样式,否则应用普通样式。



检查buttonclick的以下源代码:

  func GetUpdatesButton(sender:UIButton)
{
var sen:UIButton = sender
var g:NSIndexPath = NSIndexPath(forRow:sen.tag,inSection: 0)
var t:CustomCell = self.myTableView.cellForRowAtIndexPath(g)as! CustomCell
t.btnGetUpdates.backgroundColor = UIColor.redColor()
self.selectedButtonsArray.addObject(indexpath.row)
}

以下代码用于在CellForRowAtIndexPath中的按钮上应用样式:

  func tableView (tableView:UITableView,cellForRowAtIndexPath indexPath:NSIndexPath) - > UITableViewCell {
var cell:CustomCell
cell = tableView.dequeueReusableCellWithIdentifier(CustomCellID,forIndexPath:indexPath)as! CustomCell
cell.strName.text = self.names [indexPath.row]
cell.imgPerson.image = UIImage(名称:\(self.persons [indexPath.row]))!

if(self.selectedButtonsArray.containsObject(indexpath.row)){

cell.btnGetUpdates.backgroundColor = UIColor.redColor()
cell.btnGetUpdates.layer .borderColor = UIColor.darkGrayColor()。CGColor
cell.btnGetUpdates.layer.borderWidth = 1
cell.btnGetUpdates.layer.cornerRadius = 5.0

} else {

cell.btnGetUpdates.layer.borderColor = UIColor.darkGrayColor()。CGColor
cell.btnGetUpdates.layer.borderWidth = 1
cell.btnGetUpdates.layer.cornerRadius = 5.0

}

cell.btnComment.layer.borderColor = UIColor.darkGrayColor()。CGColor
cell.btnComment.layer.borderWidth = 1
cell.btnComment.layer .cornerRadius = 5.0

cell.btnReadMore.layer.borderColor = UIColor.darkGrayColor()。CGColor
cell.btnReadMore.layer.borderWidth = 1
cell.btnReadMore.layer。 cornerRadius = 5.0

cell.dateMissingPersonPlace.text = se lf.MissingPeoplePlace [indexPath.row]
cell.dateMissingPersonSince.text = self.MissingPeopleSince [indexPath.row]

cell.btnGetUpdates.tag = indexPath.row
cell.btnGetUpdates .addTarget(self,action:GetUpdatesButton:,forControlEvents:.TouchUpInside)

返回单元格

}



我希望这有助于解决您的问题!谢谢。


I have a TablewView that has prototype UITableViewCell that has its own class,

Custom UITableViewCell Class

import UIKit

class H_MissingPersonTableViewCell: UITableViewCell {

    @IBOutlet weak var strName: UILabel!
    @IBOutlet weak var imgPerson: UIImageView!
    @IBOutlet weak var Date1: UILabel!
    @IBOutlet weak var Date2: UILabel!
    @IBOutlet weak var MainView: UIView!
    @IBOutlet weak var btnGetUpdates: UIButton!
    @IBOutlet weak var btnComment: UIButton!
    @IBOutlet weak var btnReadMore: UIButton!


    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}

The TableView loads perfectly and the dataSource and delegate work fine. However, when I click a BUTTON at IndexPath.row = 0 (zero), and then I scroll down , I would see random(?) or other cells also being highlighted as a result of clicking BUTTON in row 0...

Here is my CellForRowAtIndexPath code:

  func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        println("called")
        var cell : CustomCell


        cell = tableView.dequeueReusableCellWithIdentifier("CustomCellID", forIndexPath: indexPath) as! CustomCell
        cell.strName.text = self.names[indexPath.row]
        cell.imgPerson.image = UIImage(named: "\(self.persons[indexPath.row])")!

        cell.btnGetUpdates.layer.borderColor = UIColor.darkGrayColor().CGColor
        cell.btnGetUpdates.layer.borderWidth = 1
        cell.btnGetUpdates.layer.cornerRadius = 5.0


        cell.btnComment.layer.borderColor = UIColor.darkGrayColor().CGColor
        cell.btnComment.layer.borderWidth = 1
        cell.btnComment.layer.cornerRadius = 5.0

        cell.btnReadMore.layer.borderColor = UIColor.darkGrayColor().CGColor
        cell.btnReadMore.layer.borderWidth = 1
        cell.btnReadMore.layer.cornerRadius = 5.0

        cell.dateMissingPersonPlace.text = self.MissingPeoplePlace[indexPath.row]
        cell.dateMissingPersonSince.text = self.MissingPeopleSince[indexPath.row]

        cell.btnGetUpdates.tag = indexPath.row
        cell.btnGetUpdates.addTarget(self, action: "GetUpdatesButton:", forControlEvents: .TouchUpInside)

        return cell

    }

in my cell.btnGetUpdates , I put an action as you can see in the last part of my CellForIndex code, GetUpdatesButton:

This is the code:

func GetUpdatesButton(sender: UIButton){
    println("Button Clicked \(sender.tag)")

    var sen: UIButton = sender
    var g : NSIndexPath = NSIndexPath(forRow: sen.tag, inSection: 0)
    var t : CustomCell = self.myTableView.cellForRowAtIndexPath(g) as! CustomCell
    t.btnGetUpdates.backgroundColor = UIColor.redColor()
    }

The problem is, NOT ONLY the button in index 0 is being highlighted, but I would also see random buttons from other index/rows being highlighted as I scroll my tableView down.

Where did I go wrong, how can I only update the button I clicked...and not other buttons..

I have 20 items ( 20 rows) and when I clicked button in row 0, rows 3, 6, 9....are also having highlighted buttons.

ROW 0 , button clicked

ROW 3, button clicked as well, though I did not really click it.

解决方案

This is occurring due to UITableview have reusablecell policy.In order to resolve this issue You need to maintain one array of selected items in cellForRowAtIndexPath method you have to verify weather this button is being hold by selected item array. if yes then apply selection styles otherwise apply normal style to it.

Check below source code for buttonclick:

func GetUpdatesButton(sender: UIButton) 
{
    var sen: UIButton = sender
    var g : NSIndexPath = NSIndexPath(forRow: sen.tag, inSection: 0)
    var t : CustomCell = self.myTableView.cellForRowAtIndexPath(g) as!   CustomCell
    t.btnGetUpdates.backgroundColor = UIColor.redColor()
   self.selectedButtonsArray.addObject(indexpath.row)
}

Below code for applying styles on buttons in CellForRowAtIndexPath:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var cell : CustomCell
    cell = tableView.dequeueReusableCellWithIdentifier("CustomCellID", forIndexPath: indexPath) as! CustomCell
    cell.strName.text = self.names[indexPath.row]
    cell.imgPerson.image = UIImage(named: "\(self.persons[indexPath.row])")!

    if(self.selectedButtonsArray.containsObject(indexpath.row)){

        cell.btnGetUpdates.backgroundColor = UIColor.redColor()
        cell.btnGetUpdates.layer.borderColor = UIColor.darkGrayColor().CGColor
        cell.btnGetUpdates.layer.borderWidth = 1
        cell.btnGetUpdates.layer.cornerRadius = 5.0

    }else{

        cell.btnGetUpdates.layer.borderColor = UIColor.darkGrayColor().CGColor
        cell.btnGetUpdates.layer.borderWidth = 1
        cell.btnGetUpdates.layer.cornerRadius = 5.0

   }

    cell.btnComment.layer.borderColor = UIColor.darkGrayColor().CGColor
    cell.btnComment.layer.borderWidth = 1
    cell.btnComment.layer.cornerRadius = 5.0

    cell.btnReadMore.layer.borderColor = UIColor.darkGrayColor().CGColor
    cell.btnReadMore.layer.borderWidth = 1
    cell.btnReadMore.layer.cornerRadius = 5.0

    cell.dateMissingPersonPlace.text = self.MissingPeoplePlace[indexPath.row]
    cell.dateMissingPersonSince.text = self.MissingPeopleSince[indexPath.row]

    cell.btnGetUpdates.tag = indexPath.row
    cell.btnGetUpdates.addTarget(self, action: "GetUpdatesButton:",forControlEvents: .TouchUpInside)

    return cell

}

I hope this helps to resolve your problem! Thanks.

这篇关于自定义UITableViewCell中的按钮操作会影响其他单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆