2017-01-20 44 views
0

目前,我有一個CustomTableViewCell,用於四個或五個不同的地方。自定義單元格具有延遲加載的UILongPressGestureRecognizer屬性,該屬性被添加爲父VC中的cellForRowAtIndexPath中的手勢識別器。使用自定義UITableViewCell顯示吐司彈出框的模式

self.tableView.addGestureRecognizer(cell.longPress) 

當用戶啓動長按,我想Toast通知彈出顯示一些上下文信息,然後在幾秒鐘後消失。我在我的代碼中包含了這個CustomTableViewCell,但所有這些決定都開始「嗅覺」。是否有更智能,更合乎邏輯的方式來執行這些決定?

此表視圖電池具有以下代碼:

weak var parentTableView: UITableView? 

lazy var longPress: UILongPressGestureRecognizer = { 

    let longPress = UILongPressGestureRecognizer(target: self, action: #selector(longPressSelector)) 

    longPress.minimumPressDuration = 0.5 
    longPress.delegate = self 

    return longPress 

}() 

func longPressSelector(_ longPress: UILongPressGestureRecognizer!) { 

    if let tableView = self.parentTableView { 

     let point = longPress.location(in: tableView) 

     let indexPath = tableView.indexPathForRow(at: point) 

     if ((indexPath! as NSIndexPath).section == 0 && longPress.state == .began) { 


      // do some work 

      // Show informational popup 
      let toast = createToastNotification(withTitle: addedSong.name) 

      Timer.scheduledTimer(withTimeInterval: 2.0, repeats: false) { (timer) -> Void in 
       UIView.animate(withDuration: 1.0) {() -> Void in 
        toast.alpha = 0.0 
        toast = nil 
       } 
      } 
     } 
    } 

} 

func createToastNotification(withTitle title: String) -> UIView { 

    if let tableView = self.parentTableView { 
     let windowFrame = tableView.superview?.bounds 

     let height:CGFloat = 145, width: CGFloat = 145 

     let x = (windowFrame?.width)!/2 - width/2 
     let y = (windowFrame?.height)!/2 - height/2 

     let toast = EnsembleToastView.create() 

     toast.frame = CGRect(x: x, y: y, width: width, height: height) 
     toast.songLabel.text = title 
     toast.layer.cornerRadius = 5 

     tableView.superview?.addSubview(toast) 

     return toast 
    } 

    return UIView() 
} 

回答

1

我覺得它更有意義的TableView中知道如何顯示舉杯,所以我會在你的tableViewCell創建一個協議,所以我會採取以下步驟。

  • 充分利用TableViewController負責:
    • 創建吐司(僅一次)
    • 配置敬酒
    • 顯示敬酒
    • 響應長按手勢
    • 在配置表格視圖單元格
  • 允許YourTableViewCell只委派

因此,讓我們做響應長按手勢第一

protocol TableViewCellLongPressDelegate { 
    func tableViewCellHadLongPress(_ cell: YourTableViewCell) 
} 

然後擴展您的TableViewController以符合新的協議

extension YourTableViewController : TableViewCellLongPressDelegate { 
    func tableViewCellHadLongPress(_ cell: YourTableViewCell){ 
     //configure toast based on which cell long pressed 
     configureToastNotification(with title: cell.title){ 
     //show toast 
    } 
} 

現在,在配置表格視圖單元格您TableViewController內配置細胞和分配TableViewController作爲longPressDelegate

let cell = YourTableViewCell.dequeue(from: self.tableView)! 
//configure cell 
cell.tableViewCellLongPressDelegate = self 

這種做法是很好的,因爲你可以移動createToastNotification()方法你TableViewController和負責創建吐司(僅一次)

var toastNotification : UIView? 
viewDidLoad(){ 
    //yatta yatta 
    toastNotification = createToastNotification() 
} 

然後您可以更改創建ToastNotification到

func createToastNotification() -> UIView? { 

    let windowFrame = self.bounds 

    let height:CGFloat = 145, width: CGFloat = 145 

    let x = (windowFrame?.width)!/2 - width/2 
    let y = (windowFrame?.height)!/2 - height/2 

    let toast = EnsembleToastView.create() 

    toast.frame = CGRect(x: x, y: y, width: width, height: height) 
    toast.layer.cornerRadius = 5 

    self.addSubview(toast) 

    return toast 
} 

最後的YourTableViewController,配置敬酒,讓我們創建一個configureToastNotification(標題爲:字符串),如:

func configureToastNotification(with title: String){ 
    if let toast = self.toastNotification { 
     toast.songLabel.text = title 
    } 
} 

爲末,我們刪除很多來自YourTableViewCell的責任和只允許它代理

protocol TableViewCellLongPressDelegate : class { 
    func tableViewCellHadLongPress(_ cell: YourTableViewCell) 
} 

class YourTableViewCell : UITableViewCell { 

    //initializers 

    weak var longPressDelegate: TableViewCellLongPressDelegate? 

    lazy var longPress: UILongPressGestureRecognizer = { 

     let longPress = UILongPressGestureRecognizer(target: self, action: #selector(longPressHappened)) 

     longPress.minimumPressDuration = 0.5 
     longPress.delegate = self 

     return longPress 

    }() 

    func longPressHappened() { 
     self.longPressDelegate?.tableViewCellHadLongPress(self) 
    } 
} 
+0

這很完美。我已經開始在委託方向上工作,並且包含CustomTableViewCell上的新選擇器的編輯將其置於頂端。謝謝你的幫助! – dstepan

+0

@dstepan很高興我能幫到你。 –