2013-07-28 21 views
13

Obecnie jestem nadrzędnymi standardowego UITableViewSelectionStyle za pomocą UITableViewSelectionStyleNone a następnie zmianę koloru komórki w oparciu o metody Delegat:Usuwanie UITableViewCell Wybór

- (void)tableView:(UITableView *)tableView 
     didHighlightRowAtIndexPath:(NSIndexPath *)indexPath { 
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];     
    [cell setBackgroundColor:[UIColor yellowColor]]; 
} 

- (void)tableView:(UITableView *)tableView 
    didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
    [cell setBackgroundColor:[UIColor whiteColor]]; 
} 

- (void)tableView:(UITableView *)tableView 
    didUnhighlightRowAtIndexPath:(NSIndexPath *)indexPath { 
    NSLog(@"indexpath: %i",indexPath.row); 
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
    [cell setBackgroundColor:[UIColor whiteColor]]; 
} 

- (void)tableView:(UITableView *)tableView 
    didDeselectRowAtIndexPath:(NSIndexPath *)indexPath { 
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
    [cell setBackgroundColor:[UIColor whiteColor]]; 
} 

To prawie działa oprócz tego, że ilekroć zaznaczyć komórkę, a następnie przeciągnąć mój oderwij go bez wybrania, kolor nie zmieni się na biały ... jeśli ustawię go na [UIColor RedColor], działa perfekcyjnie. Dlaczego jest to ...

Edit:

Jakoś kiedy wydrukować indexPath.row po didUnhlightRowAtIndexPath otrzymuję "indexpath: 2147483647" Z mojego NSLog

+1

indexPath '2147483647' jest równoważna' NSNotFound'. – Malloc

Odpowiedz

29

można spróbować:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
[tableView deselectRowAtIndexPath:indexPath animated:YES]; 
} 

jeśli chcesz tylko punktem kulminacyjnym odejść po wybraniu komórki. Chyba że źle zrozumiałem twoje pytanie.

1

Poprzez utrzymywanie lokalną instancję mój indexPath, udało mi się znaleźć ostatnio wybraną komórkę i zmienić jej kolor na biały. Wydaje się naprawdę irytujące, że mam do utrzymywania stanu siebie, ale jest to co to jest ...

+1

Nie wiem, dlaczego dostałem skargę ... tylko wyjaśniam, jak rozwiązałem swój własny problem z korzyścią dla innych na stosie ... – Apollo

+1

Tak, czuję twój ból i nienawidzę go, kiedy to się dzieje .. +1 dla twojego pytania i odpowiedz mojemu przyjacielowi :) – abbood

+0

Nie musisz utrzymywać stanu. UITableView ma metodę 'indexPathsForSelectedRows' – sosborn

13

Można również spróbować tej

tableView.allowsSelection = NO; 

inny sposób robi to

cell.selectionStyle = UITableViewCellSelectionStyleNone; 

jeden

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
[tableView deselectRowAtIndexPath:indexPath animated:YES]; 
} 
2

Oto Swift 2 version

if let indexPaths = self.tableView.indexPathsForSelectedRows { 
     for indexPath in indexPaths { 
      self.tableView.deselectRowAtIndexPath(indexPath, animated: true) 
     } 
    } 
2

Możesz to również zrobić z storyboardu. Wybierz tableViewCell i pod Atrybuty Inspector można wybrać Selection> Brak

enter image description here