簡體   English   中英

使用iOS Swift隱藏三個按鈕中的一個按鈕

[英]Hiding one button out of three buttons using iOS Swift

我有一個大約5個單元格(行)的UITableView。 每個單元格(行)都添加了三個UIButton作為各自單元格的子視圖。

Row1: Button1 Button2 Button3 //所有這些按鈕都具有tag = row_number = 1

第2行: Button1 Button2 Button3 //所有這些按鈕都具有tag = row_number = 2

Row3: Button1 Button2 Button3 //所有這些按鈕都具有tag = row_number = 3

第4行: Button1 Button2 Button3 //所有這些按鈕都具有tag = row_number = 4

注意:所有行的Buttons1連接到相同的IBAction。 類似地,所有行的Buttons2連接到相同的IBAction。 等等...我完全能夠檢測到某個單元格中按下了哪個按鈕。 我為此使用標簽。

我要執行以下操作:

  1. 如果連續按下某個按鈕,則該按鈕應在按下時隱藏,而同一行中的所有其他按鈕應保持顯示狀態。 一段時間后,如果從同一行按下了另一個按鈕,則新按下的按鈕應隱藏,而先前隱藏的按鈕應返回到視圖。

目前,我可以隱藏第一個按下的按鈕,但是如果之后再按下同一行中的另一個按鈕,則無法將其恢復。

  1. 是否還可以在同一行內跟蹤先前按下的按鈕?

請指導我如何執行此操作。

提前致謝。

由於您的問題只會一次影響單個單元格,因此我們可以專注於一個單元格。

您應該使用UITableViewCell子類,該子類保留對其按鈕的引用,並且是其操作的target

因此,該子類可以管理所有與視圖相關的界面更改,並且封裝得很好。

然后,您可以使用委派將動作傳播到視圖控制器並處理實際邏輯。

編輯

關於第二個問題,通過此設置,可以通過在每次按下時將每個按鈕的索引附加到[Int]上來輕松跟蹤單元格中按鈕的按下順序。

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
 {
  let cell = self.tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! CustomCell



  // add a line for each one of the buttons
   cell.btn1.addTarget(self, action: "handleButtonPressed:", forControlEvents: .TouchUpInside)
   cell.btn2.addTarget(self, action: "handleButtonPressed:", forControlEvents: .TouchUpInside)
   cell.btn3.addTarget(self, action: "handleButtonPressed:", forControlEvents: .TouchUpInside)
   cell.btn4.addTarget(self, action: "handleButtonPressed:", forControlEvents: .TouchUpInside)


   return cell
  }

   @IBAction func handleButtonPressed(sender:UIButton!)
 {

 switch sender.tag {

   let button = sender as! UIButton
   let view = button.superview!
   let cell = view.superview as! custom cell
  // cell.btn1  - will get here 
  //cell.btn2 - will get here
  // do based on what on button action hide or show
  case 0:
  print(sender.tag)

  case 1:
  print(sender.tag)
  case 2:
  print(sender.tag)
  case 3:
  print(sender.tag)

    default:
    print("Tag was not found")
   }
 }

暫無
暫無

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

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