2017-08-15 57 views
16

正如我的標題所說,我想從第一行UITableView左側滑動到右側,當用戶來到那個ViewController時。輕掃UITableViewCell無觸摸

在我的ViewController我有一個UITableView,每行有兩個按鈕「More」和「Delete」動作。看下面的代碼

func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool { 
    return true 
} 

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) { 
    if (editingStyle == UITableViewCellEditingStyle.delete) { 
    } 
} 


func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? { 
    let deleteButton = UITableViewRowAction(style: .normal, title: "Delete") { action, index in 
     // Edit Button Action 
    } 
    deleteButton.backgroundColor = UIColor.red 

    let editButton = UITableViewRowAction(style: .normal, title: "Edit") { action, index in 
     // Delete Button Action 
    } 
    editButton.backgroundColor = UIColor.lightGray 

    return [deleteButton, editButton] 
} 

所有的工作都很好。但我希望當最終用戶第一次登錄此ViewController時,他們會通知有可用的滑動操作,以便他們執行此操作。

問題是:我怎樣才能左右自動刷第一行?

我做了什麼?

override func viewDidAppear(_ animated: Bool) { 
    super.viewDidAppear(animated) 
    let cell = posTblView.cellForRow(at: NSIndexPath(row: 0, section: 0) as IndexPath) as! POSUserTabelCell 
    UIView.animate(withDuration: 0.3, animations: { 
     cell.transform = CGAffineTransform.identity.translatedBy(x: -150, y: 0) 
    }) { (finished) in 
     UIView.animateKeyframes(withDuration: 0.3, delay: 0.25, options: [], animations: { 
      cell.transform = CGAffineTransform.identity 
     }, completion: { (finished) in 
     }) 
    } 
} 

通過上面的代碼刷卡/移動單元格正在工作,但不顯示「刪除」和「更多」按鈕。

所以請引導我正確的方向。

+0

您的方法似乎適合左右滑動。但是,我不確定內置按鈕是否可以像這樣突出顯示。你可以肯定的做法是在你的內容下面放置一個看起來像本地控件的UIView,做一些滑動操作來展示它們,並且將它們從單元格中移除/隱藏。看看這個答案:https://stackoverflow.com/questions/5301728/is-it-possible-to-programmatically-show-the-red-delete-button-on-a-uitableviewce – dirtydanee

+0

你可以使用這個舊的框架: https://github.com/CEWendel/SWTableViewCell。然後在viewdidappear寫: [cell showRightUtilityButtonsAnimated:YES]; –

+0

那麼,你的目標是讓第一行自動滑動,這樣你就可以看到編輯按鈕。是對的嗎? –

回答

6

我發現實現這一目標的方法之一。

override func viewDidAppear(_ animated: Bool) { 
    super.viewDidAppear(animated) 

    if arrStudent.count > 0 { 
     let indexPath = NSIndexPath(row: 0, section: 0) 
     let cell = tblStudent.cellForRow(at: indexPath as IndexPath); 

     let swipeView = UIView() 
     swipeView.frame = CGRect(x: cell!.bounds.size.width, y: 0, width: 170, height: cell!.bounds.size.height) 
     swipeView.backgroundColor = .clear 

     let swipeEditLabel: UILabel = UILabel.init(frame: CGRect(x: 0, y: 0, width: 80, height: cell!.bounds.size.height)) 
     swipeEditLabel.text = "Edit"; 
     swipeEditLabel.textAlignment = .center 
     swipeEditLabel.font = UIFont.systemFont(ofSize: 19) 
     swipeEditLabel.backgroundColor = UIColor(red: 180/255, green: 180/255, blue: 180/255, alpha: 1) // Light Gray: Change color as you want 
     swipeEditLabel.textColor = UIColor.white 
     swipeView.addSubview(swipeEditLabel) 

     let swipeDeleteLabel: UILabel = UILabel.init(frame: CGRect(x: swipeEditLabel.frame.size.width, y: 0, width: 90, height: cell!.bounds.size.height)) 
     swipeDeleteLabel.text = "Delete"; 
     swipeDeleteLabel.textAlignment = .center 
     swipeDeleteLabel.font = UIFont.systemFont(ofSize: 19) 
     swipeDeleteLabel.backgroundColor = UIColor(red: 255/255, green: 41/255, blue: 53/255, alpha: 1) // Red color: Change color as you want 
     swipeDeleteLabel.textColor = UIColor.white 
     swipeView.addSubview(swipeDeleteLabel) 

     cell!.addSubview(swipeView) 

     UIView.animate(withDuration: 0.60, animations: { 
      cell!.frame = CGRect(x: cell!.frame.origin.x - 170, y: cell!.frame.origin.y, width: cell!.bounds.size.width + 170, height: cell!.bounds.size.height) 
     }) { (finished) in 
      UIView.animate(withDuration: 0.60, animations: { 
       cell!.frame = CGRect(x: cell!.frame.origin.x + 170, y: cell!.frame.origin.y, width: cell!.bounds.size.width - 170, height: cell!.bounds.size.height) 

      }, completion: { (finished) in 
       for subview in swipeView.subviews { 
        subview.removeFromSuperview() 
       } 
       swipeView.removeFromSuperview() 
      }) 

     } 
    } 
} 

在電池的末端添加自定義UIView和刪除動畫完成後。

  • ,只要你想
  • 添加標籤(S)與背景顏色和標題,你想,你可以設置自定義視圖的框架。

在這裏你應該寫的代碼不是每次調用這個代碼在viewDidAppear它應該只調用一次只有用戶指示。

+0

我會嘗試你的邏輯,希望它能幫助我。 – Johnty

+0

非常感謝你:)我盡我所能,努力工作。感謝agin男士。 – Johnty

+0

@Johnty很高興幫助你:) – Govaadiyo

1

將單元格轉換到左側不會使操作按鈕可見。

您需要通過調用單元上的setEditing方法發送到單元格編輯模式,把這個在您的viewDidAppear

let cell = posTblView.cellForRow(at: IndexPath(row: 0, section: 0)) 
cell.setEditing(true, animated: true) 
+0

試過但沒有工作。 – Johnty

相關問題