@H_
404_0@
我有一个应用程序在
自定义单元格中有
一个自定义按钮.如果你选择的细胞,它将细节视图,这是完美的.如果我在单元格中选择了
一个按钮,下面的
代码会将单元索引打印到控制台中.
我需要访问所选单元格的内容(使用按钮)并将其添加到数组或字典中.我是新来的,所以很难找出如何访问单元格的内容.我尝试使用didselectrowatindexpath,但我不知道如何强制索引是标签的索引…
所以基本上,如果在每个单元格中有3个单元格的“Dog”,“Cat”,“Bird”作为cell.repeatLabel.text,我选择行1和3(索引0和2)中的按钮,应该将“狗”和“鸟”添加到数组/字典中.
// MARK: - Table View
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView,numberOfRowsInSection section: Int) -> Int {
return postsCollection.count
}
override func tableView(tableView: UITableView,cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: CustomCell = tableView.dequeueReusableCellWithIdentifier("cell",forIndexPath: indexPath) as! CustomCell
// Configure the cell...
var currentRepeat = postsCollection[indexPath.row]
cell.repeatLabel?.text = currentRepeat.product
cell.repeatCount?.text = "Repeat: " + String(currentRepeat.currentrepeat) + " of " + String(currentRepeat.totalrepeat)
cell.accessoryType = UITableViewCellAccessoryType.DetaildisclosureButton
cell.checkButton.tag = indexPath.row;
cell.checkButton.addTarget(self,action: Selector("selectItem:"),forControlEvents: UIControlEvents.TouchUpInside)
return cell
}
func selectItem(sender:UIButton){
println("Selected item in row \(sender.tag)")
}
选择1.处理委托
处理单元格子视图中发生的事件的正确方法是使用委派.
所以你可以按照以下步骤:
1.在您的类定义之上,在自定义单元格中,使用单个实例方法编写协议:
protocol CustomCellDelegate {
func cellButtonTapped(cell: CustomCell)
}
2.在你的类定义中,声明一个委托变量,并在委托中调用协议方法:
var delegate: CustomCellDelegate?
@IBAction func buttonTapped(sender: AnyObject) {
delegate?.cellButtonTapped(self)
}
3.符合您的表视图的类中的CustomCellDelegate:
class ViewController: CustomCellDelegate
4.设置单元格的委托
func tableView(tableView: UITableView,cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell",forIndexPath: indexPath) as CustomCell
cell.delegate = self
return cell
}
5.在视图控制器类中实现所需的方法.
编辑:首先定义一个空数组,然后修改它:
private var selectedItems = [String]()
func cellButtonTapped(cell: CustomCell) {
let indexPath = self.tableView.indexPathForRowAtPoint(cell.center)!
let selectedItem = items[indexPath.row]
if let selectedItemIndex = find(selectedItems,selectedItem) {
selectedItems.removeAtIndex(selectedItemIndex)
} else {
selectedItems.append(selectedItem)
}
}
其中item是我的视图控制器中定义的数组:
private let items = ["Dog","Cat","Elephant","Fox","Ant","Dolphin","Donkey","Horse","Frog","Cow","Goose","Turtle","Sheep"]
选项2.使用闭包处理它
我已经决定回来,向您展示另一种处理这种情况的方式.在这种情况下使用关闭将导致更少的代码,您将实现您的目标.
1.在单元格类中声明一个闭包变量:
var tapped: ((CustomCell) -> Void)?
2.调用按钮处理程序中的关闭.
@IBAction func buttonTapped(sender: AnyObject) {
tapped?(self)
}
3.在tableView(_:cellForRowAtIndexPath :)中的包含视图控制器类:
let cell = tableView.dequeueReusableCellWithIdentifier("Cell",forIndexPath: indexPath) as! CustomCell
cell.tapped = { [uNowned self] (selectedCell) -> Void in
let path = tableView.indexPathForRowAtPoint(selectedCell.center)!
let selectedItem = self.items[path.row]
println("the selected item is \(selectedItem)")
}