2015-01-02 28 views
-2

好吧,所以我已經2 ViewController失去價值。一個是根,另一個是在根視圖控制器上應用一些過濾器的filterView。 FilterView上有一個TableView多種選擇選項。當我輕觸根視圖的UIButton,它示出了具有表中的過濾視圖和我選擇多個選項形成它,當我駁回filterView它成功地關閉它,並通過使用一個委託方法成功地修改了RootViewController
看到下面的圖片有一個更清晰的圖片。左側的按鈕(Three Horizo​​ntal Bars)將顯示過濾器視圖控制器,同樣,向下按鈕再次顯示根視圖。 Image defines the two views I have
我的問題是,當我上的按鈕再次輕按它一遍加載整個表。沒有選定的值。我希望它保存選中的值,以便用戶可以取消選中它們並檢查新的過濾器值。

當用戶點擊這裏過濾按鈕是我使用的代碼:的UITableViewController不應該解僱其視圖控制器

- (IBAction)filterButton:(id)sender 
{ 
    FilterViewController *arvc = [self.storyboard instantiateViewControllerWithIdentifier:@"FilterView"]; 
    [self presentViewController:arvc animated:YES completion:nil]; 
} 

以及有關FilterView後退按鈕我使用的是這樣的:

[self dismissViewControllerAnimated:YES completion:nil]; 

所以,我應該怎麼寫入返回按鈕,以便它應該保留多個檢查行並顯示用戶是否再次查看它。或者將其隱藏在背景中並在需要時顯示的東西。

+0

設置TableviewCell accessoryType在您的TableView中進行檢查標記。並將其存儲在NSUserDefault中。 –

+0

@iOSDeveloper如何做到這一點?你能提供一個示例代碼嗎?謝謝 –

+0

我爲它提供了一個代碼。 –

回答

0

第1步:
在你.h文件分配一個變量FilterViewController *fvc;
第2步:
在你.m文件寫入這一行ViewDidLoad方法。
fvc = [self.storyboard instantiateViewControllerWithIdentifier:@"FilterView"];
第3步:
複製/下面的代碼爲您- (IBAction)filterButton:(id)sender粘貼 -

(IBAction)filterButton:(id)sender 
{ 
    [self presentViewController:fvc animated:YES completion:nil]; 
} 

這就是它!

+0

偉人!再次感謝:D它完美地工作... –

0

當你按你的過濾器按鈕,你正在創建一個全新的FilterViewController,這有爲什麼它不會從以前的任何一個選擇或任何東西。如果你想有持久性,您將需要始終呈現相同的一個(這樣做instantiateViewControllerWithIdentifier一次,並在屬性周圍保持它,或者創建後,並提出之前,一些數據傳遞給它,所以它可以恢復其狀態(請參閱Passing Data between View Controllers

-1

您可以使用NSUserDefaults保留選定的行,創建一個indexPaths數組,並且當您選擇/取消選擇tableview單元格時,將/從數組中添加/刪除indexPath。 tableView只是檢查indexPath是否存在於數組中然後保持它被選中

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view. 

    _indexPathArray = [[NSMutableArray alloc] init]; 
    _indexPathArray = [[NSUserDefaults standardUserDefaults] objectForKey:@"selectedRows"]; 

} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath]; 
    cell.textLabel.text = [NSString stringWithFormat:@"Row %ld", (long)indexPath.row]; 

    if ([_indexPathArray containsObject:indexPath]) { 
     cell.accessoryType = UITableViewCellAccessoryCheckmark; 
    } else { 
     cell.accessoryType = UITableViewCellAccessoryNone; 
    } 

    return cell; 

} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

    if ([_indexPathArray containsObject:indexPath]) { 
     [_indexPathArray removeObject:indexPath]; 
    } 
    else { 
     [_indexPathArray addObject:indexPath]; 
    } 

    [[NSUserDefaults standardUserDefaults] setObject:_indexPathArray forKey:@"selectedRows"]; 
    [[NSUserDefaults standardUserDefaults] synchronize]; 
    [self.tableView reloadData]; 
} 
+0

代碼請:/這樣的示例代碼將是很好的。 –

+0

我添加了代碼 – suhit

1

首先設置NSMutableArray裏爲

NSMutableArray *SelectedRows; 

現在對你的看法有沒有負載設定NSUserDefault作爲

NSUserDefaults *userDef = [NSUserDefaults standardUserDefaults]; 
SelectedRows = [NSMutableArray arrayWithArray:[userDef objectForKey:@"SelectedRows"]]; 

而像作爲

static NSString *[email protected]"Cell"; 
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) 
{ 
    //yourCode 
} 
NSNumber *obj = [NSNumber numberWithInteger:indexPath.row]; 
if ([SelectedRows containsObject:obj]) 
{ 
    cell.accessoryType = UITableViewCellAccessoryCheckmark; 
} 
else 
{ 
    cell.accessoryType = UITableViewCellAccessoryNone; 
} 
return cell; 

和你didSelectRowAtIndexPath看起來像你的TableView cellForRowAtIndexPath方法寫爲

NSNumber *obj = [NSNumber numberWithInteger:indexPath.section]; 
if ([SelectedRows containsObject:obj]) 
{ 
    [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryNone; 
    [SelectedRows removeObject:obj]; 
    [tableView reloadData]; 
} 
else 
{ 
    [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark; 
    [SelectedRows addObject:obj]; 
    [tableView reloadData]; 
} 

NSUserDefaults *userDef = [NSUserDefaults standardUserDefaults]; 
[userDef setObject:SelectedRows forKey:@"SelectedRows"]; 
[userDef synchronize]; 

希望它能幫助你。

+0

不要使用userDefaults在viewControllers之間傳遞數據... –

+0

@FabioRitrovato然後給你的解決方案放置NSUserDefault。 –

+0

@iOSDeveloper我做過 –

相關問題