我有一個在自定義單元格中具有自定義按鈕的應(yīng)用程序。如果選擇該單元格,它將顯示到詳細(xì)信息視圖,這是完美的。如果選擇單元格中的按鈕,則下面的代碼將單元格索引打印到控制臺中。我需要訪問所選單元格的內(nèi)容(使用按鈕)并將其添加到數(shù)組或字典中。我對此很陌生,因此很難找到如何訪問單元格的內(nèi)容。我嘗試使用didselectrowatindexpath,但是我不知道如何將索引強(qiáng)制為標(biāo)簽的索引...因此,基本上,如果每個單元格中有3個單元格帶有“ Dog”,“ Cat”,“ Bird”作為cell.repeatLabel.text,并且我選擇了第1行和第3行(索引0和2)中的按鈕,在數(shù)組/字典中添加“狗”和“鳥”。 // MARK: - Table Viewoverride 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)") }
3 回答

臨摹微笑
TA貢獻(xiàn)1982條經(jīng)驗(yàn) 獲得超2個贊
由于您在表格視圖中有1個部分,因此可以獲取如下的單元格對象。
let indexPath = NSIndexPath(forRow: tag, inSection: 0)
let cell = tableView.cellForRowAtIndexPath(indexPath) as! CustomCell!
您將從按鈕標(biāo)簽獲得的標(biāo)簽。

米脂
TA貢獻(xiàn)1836條經(jīng)驗(yàn) 獲得超3個贊
Swift 3 我只是使用按鈕標(biāo)簽的超級視圖在@IBAction函數(shù)中獲取訪問單元的解決方案。
let cell = sender.superview?.superview as! ProductCell
var intQty = Int(cell.txtQty.text!);
intQty = intQty! + 1
let strQty = String(describing: intQty!);
cell.txtQty.text = strQty
- 3 回答
- 0 關(guān)注
- 459 瀏覽
添加回答
舉報(bào)
0/150
提交
取消