2015-04-28 24 views
15

我有一個應用程序在自定義單元格中有一個自定義按鈕。如果您選擇單元格,它會跳轉到詳細視圖,這非常完美。如果我在單元格中選擇一個按鈕,下面的代碼將把單元格索引打印到控制檯中。如何使用按鈕標籤快速訪問自定義單元格的內容?

我需要訪問所選單元格的內容(使用按鈕)並將它們添加到數組或字典中。我對此很陌生,所以很難找出如何訪問單元格的內容。我試着用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)") 

} 

回答

55

OPTION 1.代表團

處理它處理從移動的子視圖觸發的事件,正確的方法是使用代表團

所以,你可以按照如下步驟:

1.你上面的類定義寫一個協議與您的自定義單元格中的單個實例方法:

protocol CustomCellDelegate { 
    func cellButtonTapped(cell: CustomCell) 
} 

2.你的類中定義聲明委託變量並在代理上調用協議方法:

var delegate: CustomCellDelegate? 

@IBAction func buttonTapped(sender: AnyObject) { 
    delegate?.cellButtonTapped(self) 
} 

順應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) 
    } 
} 

其中是在我的視圖控制器定義的數組:

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) 
} 

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)") 
} 
+0

我喜歡這個解決方案不是更好一個在上面。但是,你忘了提及他必須真正設置代表。除此之外,解決方案是堅實的。 –

+0

當然我應該,我的壞。將編輯它。謝謝。 –

+0

非常感謝你們。它的作用就像一個魅力,讓我對代表有點洞察力......還是一個新手。 –

9

由於您在表格視圖中有1個部分,您可以獲取如下的單元格對象。

let indexPath = NSIndexPath(forRow: tag, inSection: 0) 
let cell = tableView.cellForRowAtIndexPath(indexPath) as! CustomCell! 

其中標籤,你會從按鈕標籤。

+0

它不會工作,每次即使其一個section.suppose你的tableview刪除一個單元格,如果你訪問標籤作爲indexPath它會給你錯了index.so提防的是... –

1
@IBAction func buttonTap(sender: UIButton) { 
     let button = sender as UIButton 
     let indexPath = self.tableView.indexPathForRowAtPoint(sender.center)! 
} 
0

根據曹的答案,這裏是處理在收集觀察室按鈕的解決方案。

@IBAction func actionButton(sender: UIButton) { 
     let point = collectionView.convertPoint(sender.center, fromView: sender.superview) 
     let indexPath = collectionView.indexPathForItemAtPoint(point) 
} 

請注意,convertPoint()函數調用將轉換集合視圖空間中的按鈕點座標。 沒有,indexPath永遠指向同一個手機號碼0

4

斯威夫特3 我只是使用按鈕標籤的上海華獲得接入小區解決方案@IBAction功能。

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 
1

我更新從瓦西里Garov答案的選項1 斯威夫特3

爲您CustomCell協議:

protocol CustomCellDelegate { 
    func cellButtonTapped(cell: CustomCell) 
} 

2.爲了您的TableViewCell聲明一個delegate變量並調用其上的協議方法:

var delegate: CustomCellDelegate? 

@IBAction func buttonTapped(sender: AnyObject) { 
    delegate?.cellButtonTapped(self) 
} 

順應CustomCellDelegate在培訓班裏你tableView是:

class ViewController: CustomCellDelegate 

4.設置你的細胞的delegate

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as CustomCell 
    cell.delegate = self 

    return cell 
} 

5.落實在您的需要的方法。

0

XCODE 8:重要提示

不要忘記將標籤設置爲不同的值大於0。

如果試圖將對象強制轉換與標籤= 0它可能工作,但在某些怪異的情況下並非如此。

解決方法是將代碼設置爲不同的值。 希望它可以幫助別人。

相關問題