2015-03-31 20 views
6

Mam UITableView z wieloma sekcjami i w mojej metodzie cellForRowAtIndexPath Dodaję UITapGestureRecognizer do obrazu komórki (każda komórka ma mały obraz). I udało się dostępu do tego obrazu (w celu jej zmiany) za pomocą:Uzyskiwanie UITableViewCell z UITapGestureRecognizer

var imageView : UIImageView! = sender.view! as UIImageView 

Co muszę zrobić, teraz jest jednak dostęp do danych komórka, która moim zdaniem oznacza, że ​​muszą być w stanie uzyskać dostęp do komórki, aby uzyskać numer sekcji i wiersza. Czy ktoś może doradzić, jak to zrobić? Chodzi o to, że zmieniam obrazek na niezaznaczone pole wyboru, jeśli zadanie zostało wykonane, ale w moich danych muszę zaznaczyć to zadanie jako wykonane.

Odpowiedz

17

W swojej funkcji, która obsługuje aparat rozpoznawania gestów kranu użyj indexPathForRowAtPoint funkcję tableView „s do uzyskania i opcjonalna ścieżka indeks zdarzenia dotykowy tak:

func handleTap(sender: UITapGestureRecognizer) { 
    let touch = sender.locationInView(tableView) 
    if let indexPath = tableView.indexPathForRowAtPoint(touch) { 
     // Access the image or the cell at this index path 
    } 
} 

Stąd można wywołać cellForRowAtIndexPath aby uzyskać komórkę lub dowolną jej zawartość, ponieważ teraz masz ścieżkę indeksu dotkniętej komórki.

+0

Witam, to prawie zadziałało! Lokalizacja musi być jednak niepoprawna, ponieważ wykonuje operacje na niewłaściwych komórkach. Jakieś przemyślenia na ten temat? – iOSBeginner

4

można uzyskać pozycję obrazu podsłuchu w celu znalezienia indexPath a stamtąd znaleźć komórkę, która ma ten obraz:

var position: CGPoint = sender.locationInView(self.tableView) 
var indexPath: NSIndexPath = self.tableView.indexPathForRowAtPoint(position)!  
var cell = self.tableView(tableView, cellForRowAtIndexPath: indexPath) 
+0

Witam, to prawie zadziałało! Lokalizacja musi być jednak niepoprawna, ponieważ wykonuje operacje na niewłaściwych komórkach. Jakieś przemyślenia na ten temat? – iOSBeginner

+1

Powinno być: var komórka = self.tableView.cellForRowAtIndexPath (indexPath) – Josh

2

tracisz, że można użyć wzorca projektowania Delegat tutaj .

Tworzenie protokół, który mówi delegatów zmiany stanu w checkbox (ImageView):

enum CheckboxState: Int { 
    case .Checked = 1 
    case .Unchecked = 0 
} 

protocol MyTableViewCellDelegate { 
    func tableViewCell(tableViewCell: MyTableViewCell, didChangeCheckboxToState state: CheckboxState) 
} 

I zakładając, że masz UITableViewCell podklasy:

class MyTableViewCell: UITableViewCell { 

    var delegate: MyTableViewCellDelegate? 

    func viewDidLoad() { 

     // Other setup here... 

     let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: "didTapImage:") 
     imageView.addGestureRecognizer(tapGestureRecognizer) 
     imageView.userInteractionEnabled = true 
    } 

    func didTapImage(sender: AnyObject) { 

     // Set checked/unchecked state here 
     var newState: CheckboxState = .Checked // depends on whether the image shows checked or unchecked 

     // You pass in self as the tableViewCell so we can access it in the delegate method 
     delegate?.tableViewCell(self, didChangeCheckboxToState: newState) 
    } 

} 

iw swoim UITableViewController podklasy:

class MyTableViewController: UITableViewController, MyTableViewCellDelegate { 

    // Other methods here (viewDidLoad, viewDidAppear, etc)... 

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) { 
     var cell = tableView.dequeueReusableCellWithIdentifier("YourIdentifier", forIndexPath: indexPath) as MyTableViewCell 

     // Other cell setup here... 

     // Assign this view controller as our MyTableViewCellDelegate 
     cell.delegate = self 

     return cell 
    } 

    override func tableViewCell(tableViewCell: MyTableViewCell, didChangeCheckboxToState state: CheckboxState) { 

     // You can now access your table view cell here as tableViewCell 

    } 

} 

Mam nadzieję, że to pomoże!

+1

'UITableViewCell' nie posiada' viewDidLoad' metoda –

+0

korzystanie awakeFromNib zamiast viewDidLoad – djdance