gpt4 book ai didi

swift 2.0,UITableView : Fetch index of 'checked' rows

转载 作者:行者123 更新时间:2023-11-28 11:13:52 25 4
gpt4 key购买 nike

我在 UIViewController 中有一个 UITableView。我在这个 View Controller 的底部有一个按钮。所有表格单元格都包含一个复选框,它是 UIButton 和 UILabel 的子类。按下按钮时,我想获取所有“选中”单元格的标签文本。现在我不能使用 cellForRowAtIndexPath,因为它会为不可见的单元格返回一个 nil 值。因此,我想索引我的数据源。但问题是,由于 UIButton 没有像 UITextfield 这样的 UIButtonDelegate,我不知道如何捕获按钮点击并存储选中复选框的单元格的 indexPath 行。请帮忙!非常感谢!

这是我的复选框代码,供引用

class CheckBox: UIButton {

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
override func drawRect(rect: CGRect) {
// Drawing code
}
*/
// images
var checkedImage = UIImage(named: "check")
var unCheckedImage = UIImage(named: "uncheck")

// bool property
var isChecked: Bool = false {
didSet {
if isChecked == true {
self.setImage(checkedImage, forState: .Normal)
} else {
self.setImage(unCheckedImage, forState: .Normal)
}
}
}

override func awakeFromNib() {
self.addTarget(self, action: "buttonClicked:", forControlEvents: UIControlEvents.TouchUpInside)
self.isChecked = false
}

func buttonClicked(sender: UIButton) {
if (sender == self) {
if isChecked == true {
isChecked = false
} else {
isChecked = true
}
}
}
}

最佳答案

您可以实现一个tableviewcell。因为你必须在你的单元格中创建所有元素。在那个单元格类中你必须维护这个变量var indexofRow//在为 UITableView 设置单元格时设置它。

  override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) - > UITableViewCell {
let cell: yourFeedCell = tableView.dequeueReusableCellWithIdentifier("yourTableCell") as!yourFeedCell
cell.indexofRow = indexPath.row;

}

/*Code in you cell class*/
//Maintain one dictonary Globally
var listOfCheckedCells = [Int: Bool]()
// bool property



var isChecked: Bool = false {
didSet {
if isChecked == true {
self.setImage(checkedImage, forState: .Normal)
listOfCheckedCells[self.indexofRow] = true
} else {
self.setImage(unCheckedImage, forState: .Normal)
listOfCheckedCells[self.indexofRow] = false
}
}
}

希望这会有所帮助

关于 swift 2.0,UITableView : Fetch index of 'checked' rows,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33098048/

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