2011-07-11 27 views
9

tôi có một số chế độ xem bảng trong ứng dụng của mình. Tôi quen thuộc với hành vi thông thường của các bảng, khi bạn chọn một hàng và tiến tới chế độ xem được đẩy, nó sẽ chuyển sang màu xanh, sau đó khi bạn quay lại, nó sẽ có màu xanh trong một giây sau đó chuyển sang màu trắng, để thông báo cho người dùng về hàng nào vừa được chọn.UITableView không giữ hàng được chọn khi trả lại

này đã làm việc một cách hoàn hảo cho đến thời gian gần đây khi tôi nhận thấy nó không còn làm các bit cuối cùng của những gì tôi đã mô tả: nó đã không ở lại màu xanh cho split rằng thứ hai ...

Tôi không biết tại sao, nhưng sau khi đọc một vài bài viết liên quan trên trang này tôi nhận ra rằng đoạn mã mà được gọi là trong phương thức "viewDidAppear". Điều gây nhầm lẫn cho tôi là tôi không ghi đè phương thức này, nhưng tôi đã thử nghiệm nó với một NSLog để chỉ cho tôi đường dẫn chỉ mục cho hàng mà nó sẽ bỏ chọn, trả về (null).

Vì vậy, điều này khiến tôi tin rằng bằng cách nào đó, tableView sẽ sớm bỏ chọn hàng.

Dưới đây là phương pháp didSelectRowForIndexPath tôi:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    NSLog(@"Selected row at index path: %@", indexPath); 

    //setting our view controller as what we want 
    TripDetails *detailViewController = [[TripDetails alloc] initWithNibName:@"TripDetails" bundle:nil]; 
    self.tripsDetailViewController = detailViewController; 
    [detailViewController release]; 


// Pass the selected object to the new view controller. 
    NSLog(@"Passing trip..."); 
    self.tripsDetailViewController.selectedTrip = [fetchedResultsController objectAtIndexPath:indexPath]; 

    //Hide the bottom bar on pushed view 
    tripsDetailViewController.hidesBottomBarWhenPushed = YES; 

    [self.navigationController pushViewController:tripsDetailViewController animated:YES]; 

} 

Any help is appreciated rất nhiều, cảm ơn :)


EDIT: Cố định

tôi nhận nó làm việc .... Có vẻ tôi đã gọi phương thức [self.tableView reloadData] trong phương thức viewWillAppear của tôi sau đó khiến bảng bỏ chọn tất cả các ô. Dưới đây là phương pháp viewDidLoad đã sửa đổi của tôi. Cảm ơn các đề xuất!

- (void)viewWillAppear:(BOOL)animated 
{ 
NSLog(@"running viewWIllAppear for TripsTable"); 

//Getting indexpath for highlighened cell, to rehighlight 
NSIndexPath *selectedIndex = [self.tableView indexPathForSelectedRow]; 

//Refreshing Table - Implement an if statement on the condition that the data has  changed 
[self viewDidLoad]; 
[self.tableView reloadData]; 

//Re select cell 
[self.tableView selectRowAtIndexPath:selectedIndex animated:NO scrollPosition:UITableViewScrollPositionNone]; 


[super viewWillAppear:animated]; 
} 
+1

Shot trong bóng tối: Có thể điều khiển giao diện trước đó được dỡ xuống vì sử dụng bộ nhớ. Điều này có thể dẫn đến hành vi này. – dasdom

Trả lời

9

Hãy thử

[tableView deselectRowAtIndexPath:indexPath animated:NO];

cho việc không chọn một hàng và

[tableView selectRowAtIndexPath:indexPath animated:NO]; 

để làm nổi bật hàng đã chọn.

1

Sử dụng đoạn mã này vào cuối của phương pháp đại biểu didSelectRowAtIndexPath của xem bảng,

[tableView deselectRowAtIndexPath:indexPath animated:NO]; 
2

rõ ràng viewWillAppear: của UITableView lớp không bỏ chọn tất cả các tế bào, thậm chí không cần tải lại các tế bào. Bí quyết được mô tả từng làm việc cho tôi:

mã từ UITablViewController tôi:

- (void)viewWillAppear:(BOOL)animated 
{ 
    NSIndexPath *selectedIndex = [self.tableView indexPathForSelectedRow]; 

    [super viewWillAppear:animated]; 

    //Re select cell 
    [self.tableView selectRowAtIndexPath:selectedIndex animated:NO scrollPosition:UITableViewScrollPositionNone]; 
}; 
+0

Đó là những gì tôi đã nghi ngờ ... Lưu ý cho bản thân mình: không bao giờ sử dụng UITableViewController một lần nữa! : p – Giovanni

+1

Điều này không chính xác và phải được rút lại. Nó được điều khiển bởi clearsSelectionOnViewWillAppear, xem câu trả lời của @ jankoen ở trên (và bình luận của tôi) –

29

nó một tính năng mà bạn có thể tắt.

Trong UITableViewController bạn có thể gọi

[ self setClearsSelectionOnViewWillAppear:NO ]; 

Đây là trực tiếp từ tập tin "UITableViewController.h". Tính năng này được ghi lại ở đó.

@property(nonatomic) BOOL clearsSelectionOnViewWillAppear __OSX_AVAILABLE_STARTING(__MAC_NA, __IPHONE_3_2); 
// defaults to YES. If YES, any selection is cleared in viewWillAppear: 
+0

Cảm ơn, điều này đã làm cho tôi! – Mike

+0

Tôi đã cố gắng tìm thuộc tính này trong tiêu đề 'UITableView':/ – rounak

+2

Lưu ý rằng kể từ Xcode 6.3/iOS 8.3, cài đặt này không tồn tại khi được đặt trong IB/storyboards/XIBs. Bạn có thể chọn hộp, nhưng nó không được khởi tạo đúng cách; nó luôn được đặt thành true. –

0
NSIndexPath *_selectedIndex ; 

sử dụng biến toàn cầu ở trên để lưu trữ trong lựa chọn trong giao diện bảng phương pháp đại biểu

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
_selectedIndex = [self.tableView indexPathForSelectedRow]; 
} 

và trong các đại biểu sau đây của phương pháp UINavigationController đại biểu

- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated 
{ 
[self.tableView selectRowAtIndexPath:_selectedIndex animated:NO scrollPosition:UITableViewScrollPositionNone]; 

} 
0

Tôi không chắc chắn nếu điều này là có liên quan nhưng có một lá cờ trong UITableViewController tôi chỉ không ticed gọi là

@property(nonatomic) BOOL clearsSelectionOnViewWillAppear NS_AVAILABLE_IOS(3_2); // defaults to YES. If YES, any selection is cleared in viewWillAppear: 

Tôi nghĩ điều này sẽ giải quyết một số vấn đề mà mọi người đang mô tả ở đây.

0

Các mã sau đây làm tác phẩm tốt

-(void)viewWillAppear:(BOOL)animated{ 
     [super viewWillAppear:YES]; 
     [self setClearsSelectionOnViewWillAppear:NO ]; 
    } 
Các vấn đề liên quan