2013-07-28 58 views
13

目前我使用UITableViewSelectionStyleNone然後改變顏色基於委託方法小區覆蓋標準UITableViewSelectionStyle:刪除的UITableViewCell選擇

- (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]]; 
} 

這幾乎工作,只是每當我選中一個單元格,然後拖我手指沒有實際選擇它,顏色不會變成白色......如果我將它設置爲[UIColor RedColor],它可以正常工作。這是爲什麼?

編輯:

不知怎的,當我打印出來的indexPath.row didUnhlightRowAtIndexPath後,我得到「indexpath:2147483647」從我的NSLog

+1

indexPath'2147483647'等同於'NSNotFound'。 – Malloc

回答

29

你可以嘗試:

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

,如果你只想高亮選擇小區後消失。除非我誤解你的問題。

1

通過維修器材的本地實例我indexPath,我能夠找到最後選中的單元格,並將其顏色更改爲白色。看來真的是煩人,我要保持自己的狀態,但它是什麼......

+1

不知道爲什麼我得到一個downvote ...只是解釋我如何解決我自己的問題,爲他人在堆棧上的利益... – Apollo

+1

是的,我感到你的痛苦和恨它,當這種情況發生.. +1爲你的問題和回答我的朋友:) – abbood

+0

你不必保持狀態。 UITableView具有'indexPathsForSelectedRows'方法 – sosborn

13

你也可以試試這個

​​

這樣

cell.selectionStyle = UITableViewCellSelectionStyleNone; 

一個更

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

這裏的另一種方式是Swift 2 version

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

您還可以從故事板中完成。選擇tableViewCell下屬性檢查器,您可以選擇選擇>無

enter image description here