2016-07-12 36 views
1

ユーザーがセルで長押しのジェスチャを実行しても更新していないときにUITableViewCellの枠線を更新しようとしています。ここでUITableViewCellの周りにUILongPressGestureRecognizerで選択したときに枠線を描きます。

は私のcellForRowAtIndexPath:方法

let objLongPressHandler = UILongPressGestureRecognizer(target: self, action: #selector(self.longPressHandler(_:))) 
objLongPressHandler.view?.tag = indexPath.row 
objLongPressHandler.delegate = self 
objLongPressHandler.enabled = true 
objLongPressHandler.minimumPressDuration = 0.1 

cell.contentView.addGestureRecognizer(objLongPressHandler) 

これは私の機能UILongPressGestureRecognizer機能です。

func longPressHandler(objGesture: UILongPressGestureRecognizer) { 

    let center = objGesture.view?.center 
    let rootViewPoint = objGesture.view!.superview?.convertPoint(center!, toView: self.tableView) 
    let indexPath = self.tableView.indexPathForRowAtPoint(rootViewPoint!) 
    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath!) as! GroupTableViewCell 
    cell.contentView.layer.borderColor = UIColor.redColor().CGColor 
    cell.contentView.layer.borderWidth = 3 
    cell.setNeedsLayout() 
} 
+0

'objGesture'は何ですか?それはジェスチャー認識者ですか?このコードはどこで呼び出されていますか?コードの各行ごとにどのような結果が得られますか(つまり、 'center'、' rootViewPoint'、 'indexPath'、' cell'に対してどんな値を得ますか) –

+0

私はこのコードで正しいindexpathを得ています。そして私はcellforIRowatIndexPathのコードをコールしています –

+0

私が正しく理解すれば、ユーザーがセルを長押ししたときにセルの境界線を変更したいと思っています。長押しのセルだけが変わるはずです。これは正しいです? –

答えて

2
  1. 複数のジェスチャーrecognisersは必要ありません。 View Controllerのメインビューでは、1つの長いタップジェスチャ認識機能を使用します。
  2. ジェスチャを処理するときは、locationInViewを使用して、場所をテーブルビューの座標に変換します。選択した行をtableView.indexPathForRowAtPointに呼び出して取得します。
  3. テーブルビューの表示行をループします。行が選択されたインデックスパスにある場合は、境界線を表示し、そうでない場合は境界線を削除します。

例:

override func viewDidLoad() { 
    super.viewDidLoad() 

    // Install tap gesture recogniser on main view. 
    let gesture = UILongPressGestureRecognizer(target: self, action: #selector(self.longPressHandler(_:))) 
    gesture.enabled = true 
    gesture.minimumPressDuration = 0.1 

    view.addGestureRecognizer(gesture) 
} 

func longPressHandler(gesture: UILongPressGestureRecognizer) { 

    // Get the location of the gesture relative to the table view. 
    let location = gesture.locationInView(tableView) 

    // Determine the row where the touch occurred. 
    guard let selectedIndexPath = tableView.indexPathForRowAtPoint(location) else { 
     return 
    } 

    // Iterate through all visible rows. 
    guard let indexPaths = tableView.indexPathsForVisibleRows else { 
     return 
    } 

    for indexPath in indexPaths { 

     // Get the cell for each visible row. 
     guard let cell = tableView.cellForRowAtIndexPath(indexPath) else { 
      continue 
     } 

     // If the index path is for the selected cell, then show the highlighted border, otherwise remove the border. 
     let layer = cell.contentView.layer 

     if indexPath == selectedIndexPath { 
      layer.borderColor = UIColor.redColor().CGColor 
      layer.borderWidth = 3 
     } 
     else { 
      layer.borderWidth = 0 
     } 

     cell.setNeedsLayout() 
    } 

} 

override func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
    return 1 
} 

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return 10 
} 

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) 
    cell.textLabel?.text = "Cell #\(indexPath.row)" 
    return cell 
} 

enter image description here

+0

しかし、私は選択したオブジェクトに境界線を設定するだけで済みます。 –

+0

質問に詳細を追加して、達成しようとしていることを説明してください。より具体的になりますか?どこかにジェスチャー認識機能があると思いますが、それは正しいのですか?あなたはテーブルビューの上に何かをドラッグしようとしていますか?または、ユーザーがタップしたセルの周囲に境界線が必要なだけですか? –

+0

私は自分の質問を編集しました。選択したオブジェクトの境界を変更しようとしています。しかし、私は長いプレゼンター関数で変更する必要があります。最初に中心点を持つセルとインデックスパスを取得し、境界線を変更しようとしました。 –

関連する問題