2012-08-17 58 views
2

我有一個UITableViewController,根據用戶的先前選擇動態填充UITableViewController。在某些情況下,用戶應該能夠在表中選擇多行,但在其他情況下,他不應該。我想在這兩種情況下自動選擇第一行,但我發現它只在我有allowsMultipleSelection = YES時才起作用。在-(void)viewDidLoadselectRowAtIndexPath僅在allowsMultipleSelection爲YES時纔有效

switch(self.field) 
{ 
    case people: 
     self.options = (NSArray *)[data objectForKey:@"People"]; 
     self.tableView.allowsMultipleSelection = YES; 
     enter code here break; 
    case place: 
     self.options = (NSArray *)[data objectForKey:@"Places"]; 
     self.tableView.allowsMultipleSelection = NO; 
     break; 
    case reason: 
     self.options = (NSArray *)[data objectForKey:@"Reasons"]; 
     self.tableView.allowsMultipleSelection = NO; 
     break; 
} 

[self.tableView reloadData]; 
NSIndexPath *zeroPath = [NSIndexPath indexPathForRow:0 inSection:0]; 
[self.tableView selectRowAtIndexPath:zeroPath animated:NO scrollPosition:UITableViewScrollPositionNone]; 

這工作正常的「人」的情況下,但不是爲別人

下面的代碼被調用。我曾嘗試將allowMultipleSelection設置爲YES,並解決了選擇的問題,但就應用程序邏輯而言這是不可接受的。

我錯過了關於這些功能的工作原理嗎?我如何以編程方式在單個選擇表中選擇一行?

+0

在禁用'self.tableview.allowMultipleSelection'後,您是否嘗試添加'self.tableview.allowSelection = YES;'? – Oyashiro 2012-08-17 12:36:59

+0

是的。它沒有幫助。 – Baruch 2012-08-17 12:51:59

回答

8

修正了從模板如下:

// Uncomment the following line to preserve selection between presentations. 
self.clearsSelectionOnViewWillAppear = NO; 

還沒確定到底爲什麼沒有工作之前。

1
for (NSIndexPath *indexPath in [self.tableView indexPathsForSelectedRows]) { 
    [self.tableView deselectRowAtIndexPath:indexPath animated:NO]; 
} 
[self.tableView selectRowAtIndexPath:zeroPath animated:NO scrollPosition:UITableViewScrollPositionNone]; 
相關問題