2016-05-05 7 views
0

Ich versuche, eine Swipeable-Tabellenansicht in meinem Swift 2, Xcode 7-Projekt zu implementieren.Versuch, MGSwipeTableCell in einer iOS-Tabellenansicht zu implementieren

Ich möchte diese Bibliothek verwenden:

https://github.com/MortimerGoro/MGSwipeTableCell

Ich bin gerade der richtige Weg, um herauszufinden, das in mein Projekt umzusetzen.

Ich habe eine UIView mit einem Tableview in ihn sowie einen benutzerdefinierten TableViewCell

Hier ist die Implementierung Code pro Repo GitHub:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell 
{ 
let reuseIdentifier = "programmaticCell" 
var cell = self.table.dequeueReusableCellWithIdentifier(reuseIdentifier) as! MGSwipeTableCell! 
if cell == nil 
{ 
    cell = MGSwipeTableCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: reuseIdentifier) 
} 

cell.textLabel!.text = "Title" 
cell.detailTextLabel!.text = "Detail text" 
cell.delegate = self //optional 

//configure left buttons 
cell.leftButtons = [MGSwipeButton(title: "", icon: UIImage(named:"check.png"), backgroundColor: UIColor.greenColor()) 
    ,MGSwipeButton(title: "", icon: UIImage(named:"fav.png"), backgroundColor: UIColor.blueColor())] 
cell.leftSwipeSettings.transition = MGSwipeTransition.Rotate3D 

//configure right buttons 
cell.rightButtons = [MGSwipeButton(title: "Delete", backgroundColor: UIColor.redColor()) 
    ,MGSwipeButton(title: "More",backgroundColor: UIColor.lightGrayColor())] 
cell.rightSwipeSettings.transition = MGSwipeTransition.Rotate3D 

return cell 
} 

Hier ist die cellForRowAtIndexPath func in meinem aktuellen Projekt:

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

    let post = posts[indexPath.row] 

    if let cell = tableView.dequeueReusableCellWithIdentifier("PostCell") as? PostCell { 

     cell.request?.cancel() 

     var img: UIImage? 

     if let url = post.imageUrl { 
      img = FeedVCViewController.imageCache.objectForKey(url) as? UIImage 
     } 

     cell.configureCell(post, img: img) 

     return cell 
    } else { 
     return PostCell() 
    } 
} 

ich muss auch diesen Rückruf implementieren:

MGSwipeButton(title: "Delete", backgroundColor: UIColor.redColor(), callback: { 
    (sender: MGSwipeTableCell!) -> Bool in 
    println("Convenience callback for swipe buttons!") 
    return true 
}) 

Kann mir jemand in die richtige Richtung weisen, wie das geht? Ich versuche nur, mir eine Menge Kopfschmerzen zu ersparen, da ich neu in der mobilen Entwicklung bin.

Ich bin meist verwirrt über die "Cell" -Deklaration und "if" -Anweisung im Implementierungscode und wie man es mit der benutzerdefinierten Zelle, die ich erstellt habe, zu kombinieren.

Vielen Dank im Voraus!

+0

Sie sollten 'PostCell' eine Unterklasse von' MGSwipeTableCell' machen, falls Sie es noch nicht getan haben. Ich nehme an, du fragst nach 'var cell = ...; wenn Zelle == Nil. Dies ist eine alternative, weniger schnelle Art zu sagen, wenn Zelle = ... aber die If und Else Blöcke sind umgekehrt. – beyowulf

+0

Können Sie ein bisschen mehr ausarbeiten? Ich habe 'PostCell' als eine Unterklasse von' MGSwipeTableCell'. Und ja, ich frage nach 'var cell = ...; wenn Zelle == Nil. – ryanbilak

Antwort

0

if let cell = tableView.dequeueReusableCellWithIdentifier("PostCell") ist die gleiche wie zu sagen

if tableView.dequeueReusableCellWithIdentifier("PostCell") != nil { 
    let cell = tableView.dequeueReusableCellWithIdentifier("PostCell") 
    } 

Sie tun etwas ähnliches durch die Deklaration einer Variablen es gleich eine optionale Einstellung. Sie prüfen, ob das optionale NULL ist. Wenn dies der Fall ist, initialisieren sie ein neues Objekt und setzen Zelle gleich, damit sie sicher auspacken können.

Wenn PostCell eine Unterklasse von MGSwipeTableCell ist, können Sie leftButtons und rightButtons genau wie sie hinzufügen. z.B.

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

    let post = posts[indexPath.row] 

    if let cell = tableView.dequeueReusableCellWithIdentifier("PostCell") as? PostCell { 
    cell.request?.cancel() 

    var img: UIImage? 

    if let url = post.imageUrl { 
     img = FeedVCViewController.imageCache.objectForKey(url) as? UIImage 
    } 

    cell.configureCell(post, img: img) 



    cell.rightButtons = [MGSwipeButton(title: "Delete", backgroundColor: UIColor.redColor(), callback: { 
     (sender: MGSwipeTableCell!) -> Bool in 
     print("Convenience callback for delete button!") 
     return true 
    }) 
     ,MGSwipeButton(title: "More",backgroundColor: UIColor.lightGrayColor(),callback: { 
     (sender: MGSwipeTableCell!) -> Bool in 
     print("Convenience callback for more button!") 
     return true 
    })] 

    return cell 
    } else { 
     return PostCell() 
    } 
} 
Verwandte Themen