2015-10-18 15 views
6

Próbuję zaimplementować accessoryButtonTappedForRowWithIndexPath: w Swift 2 na UITableViewController.Implementowanie akcesoriumButtonTappedForRowWithIndexPath: w Swift 2

Jak wyjaśniono poniżej, myślę, że brakuje mi czegoś, gdy tworzę disclosureIndicator, ale nie wiem co. Jest pobierany z kodu, ale moja akcja docelowa nie zostaje wywołana. UGH!

Aby to zrobić programowo, muszę wiedzieć, że muszę dodać wskaźnik detailDisclosure pod numerem cellForRowAtIndexPath, zanim zostanie zwrócony mój cell. Robię to w następujący sposób:

// Create disclosure indicator button in the cell 
let disclosureIndicatorButton = UIButton(type: UIButtonType.DetailDisclosure) 
disclosureIndicatorButton.addTarget(self, action: "disclosureIndicatorPressed:event:", forControlEvents: UIControlEvents.TouchUpInside) 
customCell.accessoryType = .DisclosureIndicator 

W moim kodzie detailDisclosure Chevron zostanie sporządzony, ale metoda działania cel I przypisany do niego nie zostanie wywołana.

Potem trzeba utworzyć uchwyt dla przycisku, gdy jest wciśnięty:

func disclosureIndicatorPressed(sender: UIButton, event: UIControlEvents) { 
    print("disclosure button pressed") 
    // convert touches to CGPoint, then determine indexPath 
    // if indexPath != nil, then call accessoryButtonTappedForRowWithIndexPath 
} 

Wreszcie accessoryButtonTappedForRowWithIndexPath zawiera kod do wykonania segue, co mogę zrobić. czego mi brakuje?

https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableViewDelegate_Protocol/#//apple_ref/occ/intfm/UITableViewDelegate/tableView:accessoryButtonTappedForRowWithIndexPath:

Odpowiedz

12

Nie wiem, dlaczego dodajesz wskaźnik przycisku ujawnienie takiego w kodzie.

Co szukasz jest po prostu proces 2 krok -

Krok 1: Dodaj poprawnego accessoryType na komórkę:

cell.accessoryType = .DetailDisclosureButton 

Krok 2: podjąć działania, gdy przycisk zostanie wykorzystany przez Tworzenie funkcji accessoryButtonTappedForRowWithIndexPath:

+0

To był mój problem. "DisclosureIndicator" to po prostu cukierek do oczu, nie robi nic innego, jak tylko się rysuje. 'DetailDisclosureButton' faktycznie może mieć powiązane z nim działania. Dziękuję, że mnie wyprostowałeś. – Adrian

+1

Cieszę się, że pomogło! Twoje zdrowie :)! – Abhinav