2016-06-01 11 views
0

Hier habe ich Probleme mit tableview multiselect, kann mir jemand helfen. Ich möchte Tabellenansicht Auswahl mit bestimmten Anzahl von Zeilen einschränken (für ex 5 Zeilen aus der Liste) .ausgewählte Zeilen müssen im Auswahlstil als .blue, wenn ich versuche, 6. Zeile, die Zeile Auswahlstil sollte .none.but i versuchte es mit etwas funktioniert nicht gut.Wie man den Auswahlstil basierend auf ausgewählter Zeilenanzahl in uitableview multiselect in ios swift einschränkt

Dies ist mein Code

tableview.allowsMultipleSelectionDuringEditing = true 

in cellForRowAtIndexPath Methode,

if SelectedArray.count <= 5 
{ 
    cell.selectionStyle = UITableViewCellSelectionStyle.Blue 
} 
else { 
     cell.selectionStyle = UITableViewCellSelectionStyle.None 
} 

und auch in didSelectRowAtIndexPath diese oben Erklärung definiert, willDisplayCell und meine Tabelle auch wenn didselectrow, laden

dies auch versucht mit didSelectRowAtIndexPath Methode,

self.tableView(tableView, willDisplayCell: cell, forRowAtIndexPath: indexPath) 

Aber es gibt keinen Gebrauch davon, pls helfen mir

Vielen Dank im Voraus.

+0

Sie sollten versuchen, <5 anstelle von <= 5. – sanman

Antwort

1

bitte versuchen Sie dieses

func tableView(tableView: UITableView, willSelectRowAtIndexPath indexPath: NSIndexPath) -> NSIndexPath? { 

    if let sr = tableView.indexPathsForSelectedRows { 
     if sr.count == limit { 
      let alertController = UIAlertController(title: "Oops", message: 
       "You are limited to \(limit) selections", preferredStyle: .Alert) 
      alertController.addAction(UIAlertAction(title: "OK", style: .Default, handler: {action in 
      })) 
      self.presentViewController(alertController, animated: true, completion: nil) 

      return nil 
     } 
    } 

    return indexPath 
} 

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 

    print("selected \(intervalNames[indexPath.row])") 

    if let cell = tableView.cellForRowAtIndexPath(indexPath) { 
     if cell.selected { 
      cell.accessoryType = .Checkmark 
     } 
    } 

    if let sr = tableView.indexPathsForSelectedRows { 
     print("didDeselectRowAtIndexPath selected rows:\(sr)") 
    } 
} 

func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) { 

    print("deselected \(intervalNames[indexPath.row])") 

    if let cell = tableView.cellForRowAtIndexPath(indexPath) { 
     cell.accessoryType = .None 
    } 

    if let sr = tableView.indexPathsForSelectedRows { 
     print("didDeselectRowAtIndexPath selected rows:\(sr)") 
    } 
    } 

} 

Für weitere Details besuchen Sie bitte den Link https://github.com/genedelisa/LimitTableExample

+0

Vielen Dank –

+0

Es ist mein plasure –