簡體   English   中英

在自定義單元格按鈕操作中獲取節號

[英]Get section number in custom cell button action

我有一個tableView動態填充了幾個部分中的自定義單元格。

在我的CustomCell類中,我在單元格中有一個@IBAction用於自定義復選框按鈕。 有沒有辦法在IBAction函數中獲取發送者按鈕的節號?

就像是:

@IBAction func checkboxButton(sender: CheckBox) {
    //Change Button state
    if sender.isChecked == true { sender.isChecked = false }
    else { sender.isChecked = true }

    //Get section number of clicked button
    var sectionNumber = ???????

}

您可以通過計算發件人的位置並在UITableView找到該位置的行來輕松找到該行

var position: CGPoint = sender.convertPoint(CGPointZero, toView: self.tableView)
if let indexPath = self.tableView.indexPathForRowAtPoint(position)
{
    let section = indexPath.section
    let row = indexPath.row
}

這就是Apple在Accessory示例中的做法。 https://developer.apple.com/library/ios/samplecode/Accessory/Listings/AccessoryViewController_m.html#//apple_ref/doc/uid/DTS40008066-AccessoryViewController_m-DontLinkElementID_4

在Swift 3.0中,有些東西稍微改變,比如'CGPointZero' - >'CGPoint.zero'和'indexPathForRowAtPoint' - >'indexPathForRow(at:position)'等等,這樣你就可以得到這個按鈕的實際行和部分碼:

let position: CGPoint = sender.convert(CGPoint.zero, to: self.tableView)
        if let indexPath = self.tableView.indexPathForRow(at: position)
        {
            let section = indexPath.section
            let row = indexPath.row

        }

在調用按鈕時,在Swift 3和Swift 4中查找indexPath,

//首先在“cellForRowAt”按鈕上添加目標

cell.myBtn.addTarget(self, action: #selector(self.btnAction(_:)), for: .touchUpInside)

//添加功能

func btnAction(_ sender: UIButton) {
    let point = sender.convert(CGPoint.zero, to: yourTableView as UIView)
    let indexPath: IndexPath! = yourTableView.indexPathForRow(at: point)
    print("indexPath.row is = \(indexPath.row) && indexPath.section is = \(indexPath.section)")
}

其中一種方法,假設您在單元格中有自定義單元格和按鈕...在配置內部單元格時,通常的模式是:(如果您使用帶有數據源的表視圖):

- (UITableViewCell *)tableView:(UITableView *)tableView
     cellForRowAtIndexPath:(NSIndexPath *)indexPath

你可以設置:

cell.button.tag = sectionIndex

所以在:

func checkboxButton(sender: CheckBox) { ...

sender.tag將是sectionIndex

希望你明白了。

我最近得到了一個解決這個問題的方法,利用Segues並在Swift 2.2和XCode8中實現它。

我有一個自定義單元格,我用於具有2個標簽和UIButton的標題。 我的所有數據都在一個數組中,我想動態訪問數據。 每個部分都是一個部分標題數組,在該類中,這些部分包含其他數組以完成我的tableView生成。 我的目標是根據自定義單元格(標題)中的按鈕單擊獲取該部分的索引,並在不同的視圖中更改數據。

使用我使用的自定義標題單元格填充tableView:

override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let myCell = tableView.dequeueReusableCellWithIdentifier("REUSABLE ID")! as! MyHeaderCell
    let team = array[section]
    myCell.teamName.text = team.name
    myCell.projectName.text = team.project
    myCell.addMemberButton.tag = section        
    return myCell
}

使用故事板,MyHeaderCell中按鈕(addMemberButton)的IBOutlet具有.tag字段,我用它來保存節索引。 因此,當我准備segue時,我可以使用此值動態訪問DataSource中的數組。 如下

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "segue1" {

        ...

    }else if segue.identifier == "AddMemberSegue"{
        let ix = sender?.tag
        let destVC = segue.destinationViewController as! AddMemberViewController
        self.myArray = array[ix!]
        destVC.delegate = self

    }else if segue.identifier == "segue3"{

       ...

    }
} 

這里發送者是按鈕,因此.tag字段為我們提供了訪問數組中正確位置的特定部分索引

希望這有助於某人。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM