2013-07-28 40 views
13

Hiện nay tôi đang trọng các UITableViewSelectionStyle tiêu chuẩn bằng cách sử dụng UITableViewSelectionStyleNone và sau đó thay đổi màu sắc các tế bào dựa trên phương pháp đại biểu:Loại bỏ UITableViewCell Selection

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

này hầu như làm việc ngoại trừ việc bất cứ khi nào tôi làm nổi bật một tế bào và sau đó kéo tôi ngón tay tắt của nó mà không thực sự lựa chọn nó, màu sắc không thay đổi sang màu trắng ... nếu tôi đặt nó thành [UIColor RedColor] nó hoạt động perfeclty. Tại sao điều này ...

Edit:

Bằng cách nào đó khi tôi in ra indexPath.row sau didUnhlightRowAtIndexPath tôi nhận được "indexPath: 2147483647" từ tôi NSLog

+1

indexPath '2147483647' là tương đương với 'NSNotFound'. – Malloc

Trả lời

29

Bạn có thể thử:

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

nếu bạn chỉ muốn nổi bật ra đi sau khi chọn một tế bào. Trừ khi tôi hiểu lầm câu hỏi của bạn.

1

By maintaing một trường hợp địa phương của tôi indexPath, tôi đã có thể tìm thấy ô được chọn cuối cùng và thay đổi màu của nó thành màu trắng. Dường như thực sự gây phiền nhiễu mà tôi phải duy trì trạng thái bản thân mình, nhưng nó là nó là gì ...

+1

không chắc chắn tại sao tôi có một downvote ... chỉ giải thích làm thế nào tôi giải quyết vấn đề của riêng tôi vì lợi ích của người khác trên Stack ... – Apollo

+1

Vâng tôi cảm thấy nỗi đau của bạn và ghét nó khi điều đó xảy ra .. 1 cho cả câu hỏi của bạn và trả lời bạn tôi :) – abbood

+0

Bạn không phải duy trì trạng thái. UITableView có phương thức 'indexPathsForSelectedRows' – sosborn

13

Bạn cũng có thể thử này

tableView.allowsSelection = NO; 

một cách khác để làm điều này

cell.selectionStyle = UITableViewCellSelectionStyleNone; 

thêm một

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

Đây là một Swift 2 version

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

Bạn cũng có thể làm điều đó từ bảng phân cảnh. Chọn tableViewCell và dưới Attributes Inspector bạn có thể chọn Lựa chọn> Không

enter image description here

Các vấn đề liên quan