2016-10-26 2 views
0

Ich habe ein Problem mit dem Häkchen für eine Zeile in iOS-Tabellenansicht Wenn ich ein Element oben auswählen, wird auch das nächste 13. Element ausgewählt, ich weiß nicht warum?Multi-Auswahl funktioniert nicht richtig in IOS

Muss ich etwas mit der Tabelle machen, bevor ich das Häkchen gesetzt habe, weil ich nur eine Bedingung überprüfe und wenn diese Bedingung zutrifft, setze ich den accessoryType als Häkchen, unten steht der Code.

Hinweis: - In diesem Fall wird die 13. Zeile nicht ausgewählt, sondern nur der Zubehörtyp dieser Zeile geändert.

if let cell = tableView.cellForRowAtIndexPath(indexPath) { 
       if cell.selected { 
        if(self.sections[indexPath.section].files[indexPath.row].type != "cloud"){ 
         print(self.sections[indexPath.section].files[indexPath.row]) 
         cell.accessoryType = .Checkmark 
         NSNotificationCenter.defaultCenter().postNotificationName("enableOptions", object: nil) 
        } 
       } 
      } 

CellForIndexPath Code:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! MyFilesTableViewCell 

     let fileSection = sections[indexPath.section] 
     let file = fileSection.files[indexPath.row] 

     cell.title.text = file.name 
     if file.timeStamp.isEmpty{ 
      cell.timeStamp.hidden = true 
     }else{ 
      cell.timeStamp.hidden = false 
      cell.timeStamp.text = file.timeStamp 
     } 
     cell.icon.image = file.icon 
     cell.actionsBtn.row = indexPath.row 
     cell.actionsBtn.section = indexPath.section 
     cell.actionsBtn.setTitle("\u{f142}", forState: .Normal) 
     cell.actionsBtn.addTarget(self, action: #selector(MyFilesTableViewController.buttonClicked(_:)), forControlEvents: UIControlEvents.TouchUpInside) 
     if(editingTable){ 
      cell.actionsBtn.hidden = true 
     }else{ 
      cell.actionsBtn.hidden = false 
     } 
     if(file.type == "cloud"){ 
      cell.actionsBtn.hidden = true 
     } 
     cell.progressBar.progress = 0.0 
     cell.progressBar.hidden = true 
     return cell 
    } 
+0

Sie können den cellforrowatindex-Pfadcode –

+0

eingeben Das Problem besteht hier in der UITableView-Eigenschaft zur Wiederverwendung von Zellen. Ich hoffe, Sie verwenden Prototypzellen. Wenn ja, lass es mich wissen – KrishnaCA

+0

http://stackoverflow.com/questions/40057655/ios-swift-uiimageview-change-image-in-tableview-cell/40058685#40058685 Beantworten Sie diese Antwort @aoxi –

Antwort

1

Sein Problem mit Ihrer Zelle wiederverwendbar in cellForRowAtIndexPath. Bitte verwenden Sie den folgenden Code.

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! MyFilesTableViewCell 

cell.accessoryType = .None 

if cell.selected { 
       if(self.sections[indexPath.section].files[indexPath.row].type != "cloud"){ 
        print(self.sections[indexPath.section].files[indexPath.row]) 
        cell.accessoryType = .Checkmark 
       } 
      } 

let fileSection = sections[indexPath.section] 
let file = fileSection.files[indexPath.row] 

cell.title.text = file.name 
if file.timeStamp.isEmpty{ 
    cell.timeStamp.hidden = true 
}else{ 
    cell.timeStamp.hidden = false 
    cell.timeStamp.text = file.timeStamp 
} 
cell.icon.image = file.icon 
cell.actionsBtn.row = indexPath.row 
cell.actionsBtn.section = indexPath.section 
cell.actionsBtn.setTitle("\u{f142}", forState: .Normal) 
cell.actionsBtn.addTarget(self, action: #selector(MyFilesTableViewController.buttonClicked(_:)), forControlEvents: UIControlEvents.TouchUpInside) 
if(editingTable){ 
    cell.actionsBtn.hidden = true 
}else{ 
    cell.actionsBtn.hidden = false 
} 
if(file.type == "cloud"){ 
    cell.actionsBtn.hidden = true 
} 
cell.progressBar.progress = 0.0 
cell.progressBar.hidden = true 
return cell 
} 
+0

Ihren tollen, vielen Dank :) es hat funktioniert. –

+0

@Aoxi - Danke .. Es ist gut für Entwickler, wenn es um die Wiederverwendbarkeit in 'tableview' und' collectionview' in ios geht. Weisen Sie den Standardwerten immer die ursprünglichen Werte zu, damit die Zellen nicht mit den verschiedenen Contnet des Arrays oder einer anderen Auflistung zusammenfallen. –

0

Mit dieser Methode können Sie Ihre gesamte tableView reload und nur ändern Sie Ihre ausgewählten Zellinhalt weglassen.

schaffen eine Anordnung, die Informationen der ausgewählten Zeile der Zelle speichern

myArray:[Int] = [] 

In Ihrer cellForRowAtIndexPath Methode eingestellt

cell.actionsBtn.tag = indexPath.row 
myArray.insert(1, at: indexPath.row) 

anstelle von

cell.actionsBtn.row = indexPath.row` 

In Ihrer buttonClicked(_:) Verfahren Verwendung als

func buttonClicked(sender: AnyObject) 
{ 

    let indexPath = IndexPath(row: sender.tag, section: 0) 
    let cell = tableView.cellForRow(at: indexPath) as! MyFilesTableViewCell 
    if myArray[sender.tag] == 1 
    { 
     cell.actionsBtn.setImage(UIImage(named:"checkmark_box"), for: .normal) 
     myArray[sender.tag] = 0 
    } 
    else 
    { 
     cell.actionsBtn.setImage(UIImage(named:"tick_mark"), for: .normal) 
     myArray[sender.tag] = 1 
    } 
} 
Verwandte Themen