2015-10-31 13 views
6

Ben bunu ve benzeri benim film şeridi göz içine 4 statik hücreyi eklenen benim film şeridi bir tableView var: my hücreler statik çünküStatik bir hücre tabloya nasıl sürüklenirViewView?

enter image description here

Bu tableView herhangi dataSource yok.

Ve bir hücreyi sürüklemek için aşağıdaki kodu kullanıyorum ve bir tabloyu kaydırıncaya kadar gayet iyi çalışıyor. Hücre yine görünmüyor Gördüğünüz gibi

enter image description here

: Ben sürükleyerek sonra Kaydırdığınızda gibi

import UIKit 

class TableViewController: UITableViewController { 

    var sourceIndexPath: NSIndexPath = NSIndexPath() 
    var snapshot: UIView = UIView() 
    let longPress: UILongPressGestureRecognizer = { 
     let recognizer = UILongPressGestureRecognizer() 
     return recognizer 
     }() 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     longPress.addTarget(self, action: "longPressGestureRecognized:") 
     self.tableView.addGestureRecognizer(longPress) 

     self.tableView.allowsSelection = false 
    } 

    override func viewWillAppear(animated: Bool) { 

     self.tableView.reloadData() 
    } 

    // MARK: UIGestureRecognizer 
    func longPressGestureRecognized(gesture: UILongPressGestureRecognizer){ 

     let state: UIGestureRecognizerState = gesture.state 
     let location:CGPoint = gesture.locationInView(self.tableView) 
     if let indexPath: NSIndexPath = self.tableView.indexPathForRowAtPoint(location){ 

      switch(state){ 

      case UIGestureRecognizerState.Began: 
       sourceIndexPath = indexPath 

       let cell: UITableViewCell = self.tableView .cellForRowAtIndexPath(indexPath)! 

       //take a snapshot of the selected row using helper method 
       snapshot = customSnapshotFromView(cell) 

       //add snapshot as subview, centered at cell's center 
       var center: CGPoint = cell.center 
       snapshot.center = center 
       snapshot.alpha = 0.0 
       self.tableView.addSubview(snapshot) 
       UIView.animateWithDuration(0.25, animations: {() -> Void in 
        center.y = location.y 
        self.snapshot.center = center 
        self.snapshot.transform = CGAffineTransformMakeScale(1.05, 1.05) 
        self.snapshot.alpha = 0.98 
        cell.alpha = 0.0 
        }, completion: { (finished) in 
         cell.hidden = true 
       }) 

      case UIGestureRecognizerState.Changed: 
       let cell: UITableViewCell = self.tableView.cellForRowAtIndexPath(indexPath)! 

       var center: CGPoint = snapshot.center 
       center.y = location.y 
       snapshot.center = center 
       print("location \(location.y)") 

       //is destination valid and is it different form source? 
       if indexPath != sourceIndexPath{ 
        //update data source 
        //I have commented this part because I am not using any dataSource. 
//     self.customArray.exchangeObjectAtIndex(indexPath.row, withObjectAtIndex: sourceIndexPath.row) 
        //move the row 
        self.tableView.moveRowAtIndexPath(sourceIndexPath, toIndexPath: indexPath) 
        //and update source so it is in sync with UI changes 
        sourceIndexPath = indexPath 
       } 

       if (location.y < 68) || (location.y > 450) { 
        print("cancelled") 
        self.snapshot.alpha = 0.0 
        cell.hidden = false 
        UIView.animateWithDuration(0.10, animations: {() -> Void in 

         self.snapshot.center = cell.center 
         self.snapshot.transform = CGAffineTransformIdentity 
         self.snapshot.alpha = 0.0 
         //undo fade out 
         cell.alpha = 1.0 

         }, completion: { (finished) in 
          self.snapshot.removeFromSuperview() 
        }) 
       } 

      case UIGestureRecognizerState.Ended: 
       //clean up 
       print("ended") 
       let cell: UITableViewCell = tableView.cellForRowAtIndexPath(indexPath)! 
       cell.hidden = false 

       UIView.animateWithDuration(0.25, animations: {() -> Void in 

        self.snapshot.center = cell.center 
        self.snapshot.transform = CGAffineTransformIdentity 
        self.snapshot.alpha = 0.0 
        //undo fade out 
        cell.alpha = 1.0 

        }, completion: { (finished) in 
         self.snapshot.removeFromSuperview() 
       }) 
       break 

      default: 
       break 
      } 
     }else{ 
      gesture.cancelsTouchesInView = true 
     } 
    } 

    func customSnapshotFromView(inputView: UIView) -> UIView { 

     // Make an image from the input view. 
     UIGraphicsBeginImageContextWithOptions(inputView.bounds.size, false, 0) 
     inputView.layer.renderInContext(UIGraphicsGetCurrentContext()!) 
     let image = UIGraphicsGetImageFromCurrentImageContext() 
     UIGraphicsEndImageContext(); 

     // Create an image view. 
     let snapshot = UIImageView(image: image) 
     snapshot.layer.masksToBounds = false 
     snapshot.layer.cornerRadius = 0.0 
     snapshot.layer.shadowOffset = CGSize(width: -5.0, height: 0.0) 
     snapshot.layer.shadowRadius = 5.0 
     snapshot.layer.shadowOpacity = 0.4 

     return snapshot 
    } 

} 

görünüyor. Statik hücreyi sürükleyip bırakmak istiyorum ve bu pozisyonu kaydetmek istiyorum, böylece kaydırdığımda yeniden düzenlemeyeceğim. Daha fazla bilgi için

Sample projesi.

Bu yalnızca bir tanıtım projesidir. Ancak hücreye birçok öğe ekledim ve her hücrenin farklı kullanıcı arayüzü var.

+0

Sana Evet bunu yapabilirim UITableViewDataSource' protokolü –

cevap

2

Çok benzer bir yaklaşımla tam olarak aradığınız şeyi yapan bir kütüphane var. Buna FMMoveTableView denir, ancak veri kaynağı olan hücreler içindir.

Sorununuza neden olan şey, hücrelerin etrafını hareket ettirdiğinizde ve veri kaynağındaki veri kaynağını kaydırma ile artık tabloyla eşleşmediğini ve bu nedenle hücre nesnesinin yeniden çizilemediğini düşünüyorum.

ben size tablo bu yöntemi uygulayan gerektiğini düşünüyorum:

  1. özel sizin 4 hücrelerini hücreler olun.
  2. Her birini bir alt sınıfı ekleyin.
  3. sağ sayı
+0

'olmadan yaklaşım olduğunu sanmıyorum ... hücre yani canMoveRowAtIndexPath yöntemlerinin sürükleyerek etkinleştirmek için tablo görünümü temsilci uygulamak ama yapmak istediğim Bu mümkün ise statik hücre ile .. :) –

1

Birden dinamik hücreleri oluşturabilir için doğru hücreyi göstermek için Geçersiz Kıl cellForRowAtIndexPath

  • uzun caddesi üzerinde 4
  • Tekrar sıralama dizisi sayılarla 1 ile bir Array oluşturun. Sadece doğru tanıtıcıya sahip hücreleri deşifre etmeniz gerekecek.

  • +0

    Herhangi bir örnek verebilir misiniz? –

    +0

    4 statik hücre oluşturmak yerine. 4 dinamik hücre oluşturun ve tableView'un dataSource'unu TVC'nize bağlayın. ve ilgili hücreler cellForRowAtIndexPathMethod dequeue. e.g: hücrelere bu tanımlayıcıları verin: hücre1, hücre2, hücre3, hücre4. ve sonra cellForRow yönteminde şunu yapın: 'return tableView.dequeueReusableCellWithIdentifier ("cell \ (indexPath.row)")' – zurakach

    +0

    Ayrıca, hücre dizisi dizisini kaydetmeniz gerekir. ve sonra dequeue yöntemi bu gibi görünecektir return tableView.dequeueReusableCellWithIdentifier ("hücre (cellOrderArray [indexPath.row])") – zurakach

    1

    Bunu yalnızca mizanpaj amacıyla yapıyor musunuz, belki UICollectionView veya özel UIScrollView işi yapabilir mi?


    Az asla bir çözüm buldum:

    1. tutan bir IBOutlet koleksiyon oluşturun tüm statik UITableViewCell s
    2. bir "veri kaynağı"
    3. Geçersiz kıl simüle etmek için bir endeks listesi oluşturma cellForRowAtIndexPath Kendi dizin listenizi kullanarak çizim yapmak için
    4. Liste sırasını güncellerken, indexList'i güncelleyin, böylece görünüm bu değişikliği "hatırlar"

    Bu Tablo görünümü denetleyicisi hepsini açıklıyor:

    class TableViewController: UITableViewController { 
    
        @IBOutlet var outletCells: [UITableViewCell]! 
        var indexList = [Int]() 
    
        override func viewDidLoad() { 
        super.viewDidLoad() 
        // Prepare a index list. 
        // We will move positions in this list instead 
        // of trying to move the view's postions. 
        for (index, _) in outletCells.enumerate() { 
         indexList.append(index) 
        } 
        } 
    
        override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
        // Use dynamic count, not needed I guess but 
        // feels better this way. 
        return outletCells.count 
        } 
    
        override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
        // Use the index path to get the true index and return 
        // the view on that index in our IBOutlet collection 
        let realIndexForPos = indexList[indexPath.row] 
        return outletCells[realIndexForPos] 
        } 
    
        @IBAction func onTap(sender: AnyObject) { 
        // Simulating your drag n drop stuff here... :) 
        let swapThis = 1 
        let swapThat = 2 
    
        tableView.moveRowAtIndexPath(NSIndexPath(forItem: swapThis, inSection: 0), toIndexPath: NSIndexPath(forItem: swapThat, inSection: 0)) 
    
        // Update the indexList as this is the "data source" 
        // Do no use moveRowAtIndexPath as this is never triggred 
        // This one line works: swap(&indexList[swapThis], &indexList[swapThat]) 
        // But bellow is easier to understand 
        let tmpVal = indexList[swapThis] 
        indexList[swapThis] = indexList[swapThat] 
        indexList[swapThat] = tmpVal 
        } 
    } 
    

    IBOutlet Interface Builder kullanmak yaratmak. Her Tablo Görünümü Hücresindeki Referencing Outlet Collection'u kullanın ve her biri için, denetleyici kodunuzda aynı @IBOutlet öğesine sürükleyin.

    enter image description here

    +0

    Merhaba, nsmutable dizinde kaynak ve hedef dizin yolumu sakladım, Nasıl depolayabilirim ve yeniden sıralama Uygulamayı açarken tableview hücreleri. func tableView (tableView: UITableView, moveRowAtIndexPath sourceIndexPath: NSIndexPath ,IndexPath hedefiIndexPath: NSIndexPath) { izin ver: String = dataHolder [sourceIndexPath.row]; dataHolder.removeAtIndex (sourceIndexPath.row); dataHolder.insert (item, atIndex: destinationIndexPath.row) toArray.addObject (destinationIndexPath.row) fromArray.addObject (sourceIndexPath.row) } – Apple

    2

    Sen UITableView delegelerden UITableView hücreyi sürükleyebilir ....... 1) kendi temsilci hiçbiri tablo görünümü düzenleme stilini ayarlayın.

    2)

    İlgili konular