2012-03-14 14 views

Odpowiedz

76

W viewController dodać:

@property (nonatomic, weak) IBOutlet UITableViewCell *theStaticCell; 

Połącz że wylot do komórki w scenorysie.

Teraz w tableView:didSelectRowAtIndexPath metody:

UITableViewCell *theCellClicked = [self.tableView cellForRowAtIndexPath:indexPath]; 
if (theCellClicked == theStaticCell) { 
    //Do stuff 
} 
+0

Musiałem zmienić 'self.table' na' self.tableView'. – Nestor

+0

Bardzo przydatne, dziękuję. – LpLrich

+0

, ale czy jest zoptymalizowany? jeśli nie jest to statyczna komórka, utworzy nową komórkę? – CiNN

9

W komórkach statycznych można nadal implementować - tableView:didSelectRowAtIndexPath: i sprawdzać właściwości indexPath. Jednym ze sposobów jest to, że można zdefiniować konkretny indexPath z #define, i sprawdzić, czy seleted rząd jest w tej indexPath, a jeśli tak, zadzwoń [self myMethod].

+0

Próbowałem, gdy wywołanie indexPath.row powoduje błąd podczas wykonywania. Jak mogę sprawdzić 3. sekcję w drugim rzędzie? – carbonr

+0

if ([indexPath isEqual: [NSIndexPath indexPathForRow: 1 inSection: 2]]) [self myMethod]; – Canopus

+0

jaki jest błąd, przy okazji? – Canopus

3

Oto moje zdanie, gdy zmieszanie komórek statyczne i dynamiczne,

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
        if let staticIndexPath = tableView.indexPathForCell(self.staticCell) where staticIndexPath == indexPath { 
// ADD CODE HERE 
       } 
     } 

ten unika tworząc nową komórkę. Wszyscy są wykorzystywane do tworzenia komórek i skonfigurować go w cellForRowAtIndexPath

0

Po Cinn odpowiedź, jest to wersja Swift 3, która rozwiązuje ten problem.

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
    if let staticIndexPath = tableView.indexPath(for: OUTLET_TO_YOUR_CELL), staticIndexPath == indexPath { 
     // ADD CODE HERE 
    } 
} 

Takie podejście pozwala na niepotrzebne wdrożenie metody cellForRow, szczególnie w przypadku korzystania z komórek statycznych w scenorysie.

Powiązane problemy