2016-09-03 7 views
5

Mam problem z przekazywaniem danych z niestandardowej komórki przez użytkownika dotykającego przycisku w tej komórce niestandardowej. Czasem otrzymuję nieprawidłowe dane komórek od czasu ponownego użycia komórki. Zastanawiałem się, czy istnieje pełny sposób sprawdzenia, czy zawsze uzyskać właściwe dane komórki do jej przycisku w każdej komórce, bez względu na to, która komórka jest aktualnie na ekranie. Poniżej znajduje się mój kod. Każda pomoc jest bardzo doceniana.Przesyłanie danych z przycisku w ponownie użytej komórce niestandardowej

niestandardowej komórek:

protocol CustomCellDelegate { 
    func segueWithCellData() 
} 

class CustomTableViewCell : UITableViewCell { 
    var delegate = CustomCellDelegate? 

    @IBAction func buttonTapped() { 
    if let delegate = self.delegate { 
     delegate.segueWithCellData() 
    } 
    } 
} 

MyTableViewController:

class MyTableViewController : UITableViewController, CustomCellDelegate { 
    var posts = [Post]() 
    var title: String! 

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     let post = posts[indexPath.row] 
     let cell = tableView.dequeueReusableCellWithIdentifier("CustomCellReuseIdentifier", forIndexPath: indexPath) 
     title = post.title 

    cell.delegate = self   

    return cell 
} 

    func segueWithCellData() { 
    self.performSegueWithIdentifier("passMyData", sender: self) 
    } 

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
    if segue.identifier == “passMyData” { 
     let destination = segue.destinationViewController as! UINavigationController 
     let targetVC = destination.topViewController as! nextVC 
     targetVC.title = title  
     } 

    } 
} 
+0

Możesz spróbować dodać właściwość identyfikatora do CustomTableViewCell, w komórceForRowAtIndexPath przydzielić identyfikator, a następnie, gdy wywoływana jest metoda buttonTapped, przekazywać ją (identyfikator) do delegata.Użyj identyfikatora, aby uzyskać właściwe dane do wyświetlenia następnego widoku. – azimov

+0

Jak zawsze: ** Nie nadużywaj widoku ** (komórka) ** jako modelu ** (źródło danych). Wzorzec kontrolera modelu zdecydowanie zniechęca cię do robienia tego. – vadian

+0

@vadian - co jest nie tak w kodzie? Nie zrozumiałem twojego punktu widzenia. –

Odpowiedz

1

niestandardowej komórek:

protocol CustomCellDelegate { 
    func segueWithCellData(cell:CustomTableViewCell) 
} 

class CustomTableViewCell : UITableViewCell { 
    var delegate = CustomCellDelegate? 

    @IBAction func buttonTapped() { 
    if let delegate = self.delegate { 
     delegate.segueWithCellData(self) 
    } 
    } 
} 

CustomCellDelegate Metoda:

func segueWithCellData(cell:CustomTableViewCell) { 
    //Get indexpath of selected cell here 
    let indexPath = self.tableView.indexPathForCell(cell) 
    self.performSegueWithIdentifier("passMyData", sender: self) 
} 

Stąd nie ma potrzeby tagowania komórkę. Ponieważ masz indexPath wybranej komórki, możesz pobrać z niej dane i przekazać je za pomocą parametru sender metody performSegueWithIdentifier.

Na przykład

func segueWithCellData(cell:CustomTableViewCell) { 
    //Get index-path of selected cell here 
    let selectedIndexPath = self.tableView.indexPathForCell(cell) 
    let post = posts[selectedIndexPath.row] 

    self.performSegueWithIdentifier("passMyData", sender: post) 
} 

i uzyskać dane wewnątrz prepareForSegue następująco:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
    if segue.identifier == “passMyData” { 
     let destination = segue.destinationViewController as! UINavigationController 
     let targetVC = destination.topViewController as! nextVC 

     //Get passed data here 
     let passedPost = sender as! Post 

     targetVC.title = title  
     } 
} 
+0

To działało idealnie. Bardzo ci dziękuje za pomoc. – kelsheikh

+0

Tak! Szczęśliwe kodowanie! –

0

najpierw trzeba utworzyć słownik indeksu i tytułów jak ten w MyTableViewController:

var titleDict = [Int:String]() 

ustawić znacznik komórki do indeksu w widoku tabeli i dołączyć tytuł titleDict tak w MyTableViewController:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     let post = posts[indexPath.row] 
     let cell = tableView.dequeueReusableCellWithIdentifier("CustomCellReuseIdentifier", forIndexPath: indexPath) 
     title = post.title 

    let index = indexPath.row 
    cell.tag = index 
    titleDict[index] = title 
    cell.delegate = self   

    return cell 
} 

i przekazać wartość znacznika tej komórki w metodzie delegata komórka jak to w moim niestandardowego komórki:

protocol CustomCellDelegate { 
    func segueWithCellData(index:Int) 
} 

class CustomTableViewCell : UITableViewCell { 
    var delegate = CustomCellDelegate? 

    @IBAction func buttonTapped() { 
    if let delegate = self.delegate { 
     let index = self.tag 
     delegate.segueWithCellData(index) 
    } 
    } 
} 

i dostęp tytuł z titleDict z danego wskaźnika z metodą delegowanego i wartość zmiennej tytułowego MyTableViewController:

func segueWithCellData(index:Int) { 
    if let title = titleDict[index]{ 
     self.title = title 
    } 
    self.performSegueWithIdentifier("passMyData", sender: self) 
    } 
0

Simpl e rozwiązanie: wypełnij tableView z tablicy (String) i zaktualizuj tableView. Jeśli chcesz zmienić niektóre dane w tableView, musisz zaktualizować tablicę i odświeżyć tableView.

Używam tego rozwiązania w moich aplikacjach i działa świetnie.

1

rozwiązanie pełny dowód którego użyłem w niemal wszystkich aplikacjach. Utwórz niestandardową właściwość typu NSIndexPath w klasie kategorii UIButton i przypisz parametr indexPath w funkcji cellForRowAtIndexPath. Teraz w wywołaniu przycisku znajdź obiekt w indeksie za pomocą przycisków indexPath.row ze źródła danych. to nigdy nie zawodzi.