簡體   English   中英

獲取包含按下​​的UIButton的自定義UITableViewCell

[英]Get the custom UITableViewCell that contains the UIButton pressed

所以我有一個自定義的UITableViewCell類:

class customCell{
     let button1 = UIButton()
     let view1 = UIView()
     view1.frame = CGRectMake(30, 60, 10, 10)
     view1.backgroundColor = UIColor.blueColor()
     button1.frame = CGRectMake(10,10,50,50)
     button1.addTarget(tableView(), action: "test:", forControlEvents: .TouchUpInside)
}

這就是我的視圖控制器的樣子,省略了不相關的代碼:

class tableViewController: UIViewController, UITableViewDataSource, UITableViewDelegate{
    let table = UITableView()
    let currentCell = customCell()
    var selectedIndexPath: NSIndexPath? = nil

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        selectedIndexPath = indexPath
        let cell = table.cellForRowAtIndexPath(indexPath) as! customCell
        currentCell = cell
        table.beginUpdates()
        tableView.endUpdates()
    }

   func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
       if (selectedIndexPath != nil && indexPath.row == selectedIndexPath!.row && indexPath.section == selectedIndexPath!.section){
           return 155
        }
       return 90
    }

   func test(sender:UIButton){
       currentCell.view1.hidden = true // does not work    
   }
}

如代碼中標記的那樣,當我使用currentCell來保持選定的單元格並嘗試操縱其子視圖時,即隱藏子視圖時,它不起作用。 似乎currentCell根本沒有引用tableView中的選定單元格。

您可以使用以下代碼找到索引路徑:

@IBAction func buttonAction(sender: UIButton) {

    let point = sender.convertPoint(CGPointZero, toView: tableView)

    let indexPath = tableView.indexPathForRowAtPoint(point)

    // (use your index path)
}

接下來,您可以手動調用self.tableView(_:cellForRowAtIndexPath:)傳遞獲取的索引路徑,但這可能會配置一個新的(重用的)表視圖單元實例,以顯示與該索引路徑(行,節)關聯的數據。

如果你想獲得屏幕上按下按鈕的完全相同的表格視圖單元格實例,你將不得不做一些不同的事情,可能是丑陋的; 也許從按鈕開始遍歷視圖層次結構並轉向其超級視圖?


更新: 數據源方法: tableView(_:cellForRowAtIndexPath:)肯定會配置一個不同的(可能重用的)UITableViewCell實例,但UITableView方法: cellForRowAtIndexPath()將為您提供屏幕上的相同實例。

UITAbleView的文檔中沒有提到它,但我做了一個示例項目來測試它:

TableViewController.swift

/* 
   Table cells are of custom subclass TableViewCell, with one outlet declared like this:

       @IBOutlet weak var button:UIButton!

   The button is connected with this action in the custom UITableViewController subclass that holds the table view:
*/
 @IBAction func buttonAction(sender: UIButton)
 {
        let point = sender.convertPoint(CGPointZero, toView: tableView)

        let indexPath = tableView.indexPathForRowAtPoint(point)

        let cell = tableView.cellForRowAtIndexPath(indexPath!) as! TableViewCell

        let cellButton = cell.button

        if cellButton == sender {
            // Same button instance, means also same 
            // instance of table view cell:

            print ("same instance!")
        }
    }

(為簡潔起見省略了nil 。在生產代碼中,不要強制解包選項!)

我不太了解斯威夫特,但Obj-c。 實際上我認為你可以把test:方法放到CustomCell類中,也就是說

class customCell{
     let button1 = UIButton()
     let view1 = UIView()
     view1.frame = CGRectMake(30, 60, 10, 10)
     view1.backgroundColor = UIColor.blueColor()
     button1.frame = CGRectMake(10,10,50,50)
     button1.addTarget(tableView(), action: "test:", forControlEvents:                 .TouchUpInside)

    func test(sender:UIButton){
       self.view1.hidden = true    
    }
}

暫無
暫無

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

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