我有一個應用程序在自定義單元格中有一個自定義按鈕。如果您選擇單元格,它會跳轉到詳細視圖,這非常完美。如果我在單元格中選擇一個按鈕,下面的代碼將把單元格索引打印到控制檯中。如何使用按鈕標籤快速訪問自定義單元格的內容?
我需要訪問所選單元格的內容(使用按鈕)並將它們添加到數組或字典中。我對此很陌生,所以很難找出如何訪問單元格的內容。我試着用didSelectRowAtIndexPath方法,但我不知道如何強制指標是標籤的...
所以基本上,如果有3個細胞,「狗」,「貓」,「鳥」作爲每個單元格中的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)")
}
我喜歡這個解決方案不是更好一個在上面。但是,你忘了提及他必須真正設置代表。除此之外,解決方案是堅實的。 –
當然我應該,我的壞。將編輯它。謝謝。 –
非常感謝你們。它的作用就像一個魅力,讓我對代表有點洞察力......還是一個新手。 –