gpt4 book ai didi

ios - 自定义 UITableViewCell 中的按钮操作会影响其他单元格

转载 作者:搜寻专家 更新时间:2023-11-01 05:58:16 26 4
gpt4 key购买 nike

我有一个 TablewView,它的原型(prototype) 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 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
}

}

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

这是我的 CellForRowAtIndexPath 代码:

  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

}

在我的 cell.btnGetUpdates 中,我在 CellForIndex 代码的最后部分添加了一个操作,GetUpdatesButton:

这是代码:

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()
}

问题是,当我向下滚动 tableView 时,不仅索引 0 中的按钮被突出显示,而且我还会看到来自其他索引/行的随机按钮被突出显示。

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

我有 20 个项目(20 行),当我单击第 0 行中的按钮时,第 3、6、9 行......也有突出显示的按钮。

第 0 行,点击了按钮

ROW ZERO , button clicked

第 3 行,按钮也被点击了,但我并没有真正点击它。 enter image description here

最佳答案

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

检查下面按钮点击的源代码:

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(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

希望能解决您的问题!谢谢。

关于ios - 自定义 UITableViewCell 中的按钮操作会影响其他单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32558003/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com