2017-03-24 2 views
0

Meine Entwicklungsumgebung ist swift3, xcode8. Ich mache eine Liste App wie Apples Nachrichten App. Wenn ich die Liste in der Tabellenansicht auswähle, gehe ich auf die Detailseite (durch das seg) Und jetzt möchte ich mehrere Löschfunktionen implementieren.swift - wie kann ich mehrere Zeilen in der Tabellenansicht ohne Übergang auswählen

Aber es gibt ein Problem. Wenn ich Modus bearbeite, kann ich das Auswahlfenster sehen. Aber wenn ich dieses Auswahlfenster auswähle, gehe einfach zur Detailseite.

Vielleicht bevor ich auf die Detailseite durch Seg gehe ich denke, ich sollte es zu einer Multiple Choice machen. Was soll ich tun?

Antwort

1

Stellen Sie sicher, dass Sie etwas wie unter dem Code entsprechen;

class TableviewController:UITableViewController{ 
override func viewDidLoad() { 
    super.viewDidLoad() 
    var isMultipleSelectionActive = false 
    var selectedItems: [String: Bool] = [:] 
    tableView.allowsMultipleSelectionDuringEditing = true 
    tableView.setEditing(true, animated: false) 
} 
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) 
    cell.textLabel?.text = "\(indexPath.row)" 
    return cell 
} 

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

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
    let selectedItem = items.objectAtIndex(indexPath.row) 
    //add to selectedItems 
    selectedItems[selectedItem] = true 
} 

func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) { 
    let selectedItem = items.objectAtIndex(indexPath.row) 
    // remove from selectedItems 
    selectedItems[selectedItem] = nil 
} 

func getStatusOfSelectedItems() { 
    for item in selectedItems { 
     println(item) 
    } 
} 

//You should override shouldPerformSegueWithIdentifier and return false if isMultipleSelectionActive is true 

override func shouldPerformSegue(withIdentifier identifier: String?, sender: Any?) -> Bool { 
    if let identifierName = identifier { 
     if identifierName == "NameOfYourSegueIdentifier" { 
      if isMultipleSelectionActive { 
        return false 
      } 
     } 
    } 
    return true 
} 
} 
+0

Dank zu wählen. Dieser Code fand jedoch nicht die Quelle, die ich benötigte. Mehrfachauswahl funktioniert nur gut, wenn das Segment nicht funktioniert. Gibt es eine Möglichkeit, im Multi-Auswahlmodus im Editiermodus nicht auf den Übergang zu wechseln? –

+0

Können Sie mir Code zeigen, wie Sie segue ?? –

+0

Wie kann ich Ihnen meinen Code zeigen? Ich weiß es nicht gut, weil ich ein Anfänger bin. Der Übergang wurde erstellt, indem man die Zelle mit der linken Maustaste aus dem Storyboard zog, auf die Detailseite zog und dann show wählte. –

0

Dieser Code verwendet die mehrreihige

für Ihre Antwort
class TableViewController: UITableViewController 
{ 
    var lastSelectedIndexPath = NSIndexPath(forRow: -1, inSection: 0) 

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell 
    { 
     let cell = tableView.dequeueReusableCellWithIdentifier("myCell", forIndexPath: indexPath) 

     // Configure the cell... 
     cell.textLabel!.text = "row: \(indexPath.row)" 

     if cell.selected 
     { 
      cell.selected = false 
      if cell.accessoryType == UITableViewCellAccessoryType.None 
      { 
       cell.accessoryType = UITableViewCellAccessoryType.Checkmark 
      } 
      else 
      { 
       cell.accessoryType = UITableViewCellAccessoryType.None 
      } 
     } 

     return cell 
    } 

    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) 
    { 
     let cell = tableView.cellForRowAtIndexPath(indexPath) 

     if cell!.selected 
     { 
      cell!.selected = false 
      if cell!.accessoryType == UITableViewCellAccessoryType.None 
      { 
       cell!.accessoryType = UITableViewCellAccessoryType.Checkmark 
      } 
      else 
      { 
       cell!.accessoryType = UITableViewCellAccessoryType.None 
      } 
     } 
    } 
+0

Danke für Ihre Antwort. Aber mein Problem ist, dass, wenn ich im Bearbeitungsmodus einchecke, Es ist nicht überprüft, aber geht auf die Detailseite durch den Betrieb der Zelle. Ich möchte es im Bearbeitungszustand überprüfen, nicht die Operation Wenn ich kompilieren ohne Übergang Mehrfachauswahl funktioniert gut. –

Verwandte Themen