【发布时间】:2015-07-06 22:17:24
【问题描述】:
我有一个在自定义单元格中有自定义按钮的应用。如果您选择单元格,它会转到详细视图,这是完美的。如果我在单元格中选择一个按钮,下面的代码会将单元格索引打印到控制台中。
我需要访问所选单元格的内容(使用按钮)并将它们添加到数组或字典中。我是新手,所以很难找到如何访问单元格的内容。我尝试使用 didselectrowatindexpath,但我不知道如何强制索引成为标签的索引...
所以基本上,如果有 3 个单元格,每个单元格中都有 'Dog'、'Cat'、'Bird' 作为 cell.repeatLabel.text,然后我选择第 1 行和第 3 行(索引 0 和 2)中的按钮,它应该将 'Dog' 和 'Bird' 添加到数组/字典中。
// 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)")
}
【问题讨论】:
标签: uitableview swift uibutton tags tableviewcell