2012-04-19 160 views
0

我想基於if語句有條件地隱藏我的UITableView。自動更改UITableView顏色?

現在我有下面的代碼,當我加載應用程序時,它檢查條件,這個工程,當我按下重新加載按鈕,它會檢查,它的工作原理。

我也有一個ViewWillAppear語句,如果我改變視圖並再次返回到TableView,它將工作。

現在我的問題是什麼無效或方法,我需要使這種自動 - 即無需按下按鈕或更改視圖?

現在,代碼顯示了我擁有的3種方法,並且它們或多或少具有相同的代碼,以顯示迄今爲止我所擁有的功能。

- (void)viewDidLoad 
{ 
[super viewDidLoad]; 
// Do any additional setup after loading the view, typically from a nib. 
//self.navigationItem.leftBarButtonItem = self.editButtonItem; 

UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(insertNewObject:)]; 
self.navigationItem.rightBarButtonItem = addButton; 

// Change Color of TableView if Empty 
if (![[self.fetchedResultsController fetchedObjects] count] > 0) { 
    //I know there are two statements here that can work individually from each other, but either of them would be a relevant matter. 
    [_tableView setAlpha:1.0]; 
    [self.tableView setBackgroundColor:[UIColor purpleColor]]; 
} 
else if ([[self.fetchedResultsController fetchedObjects] count] > 0) { 
    //I know there are two statements here that can work individually from each other, but either of them would be a relevant matter. 
    [_tableView setAlpha:1.0]; 
    [self.tableView setBackgroundColor:[UIColor yellowColor]]; 
    NSLog (@""); 
} 
} 

- (void)viewWillAppear:(BOOL)animated { 
[super viewWillAppear:animated]; 

// Change Color of TableView if Empty 
if (![[self.fetchedResultsController fetchedObjects] count] > 0) { 
    //I know there are two statements here that can work individually from each other, but either of them would be a relevant matter. 
    [_tableView setAlpha:1.0]; 
    [self.tableView setBackgroundColor:[UIColor purpleColor]]; 
} 
else if ([[self.fetchedResultsController fetchedObjects] count] > 0) { 
    //I know there are two statements here that can work individually from each other, but either of them would be a relevant matter. 
    [_tableView setAlpha:1.0]; 
    [self.tableView setBackgroundColor:[UIColor yellowColor]]; 
    NSLog (@""); 
} 
} 




-(IBAction)ReloadAction{ 

// Change Color of TableView if Empty 
if (![[self.fetchedResultsController fetchedObjects] count] > 0) { 
    //I know there are two statements here that can work individually from each other, but either of them would be a relevant matter. 
    [_tableView setAlpha:1.0]; 
    [self.tableView setBackgroundColor:[UIColor purpleColor]]; 
} 
else if ([[self.fetchedResultsController fetchedObjects] count] > 0) { 
    //I know there are two statements here that can work individually from each other, but either of them would be a relevant matter. 
    [_tableView setAlpha:1.0]; 
    [self.tableView setBackgroundColor:[UIColor yellowColor]]; 
    NSLog (@""); 
} 
[_tableView reloadData]; 
//[_tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO]; 
} 

乾杯傑夫

回答

0

我想通了

我補充說,如果聲明,其中+符號定義,其中刪除方法是在代碼中設置:-)

無論如何:-)

+1

另外,檢查提取的結果控制器委託,特別是這個方法: - (void)controllerDidChangeContent:(NSFetchedResultsController *)控制器。它通知你一個變化。你可以用這個替換reloadAction。 – danh 2012-04-19 04:45:59

+0

是的,我會記住檢查出來,我只是用我上面顯示的聲明來嘗試它,它不會自動執行。我需要看看:-)謝謝。 – 2012-04-19 04:52:22

+0

祝你好運。確保你將fetchedResultsController委託設置爲實現controllerDidChangeContent的類...可能是'self'。 – danh 2012-04-19 05:14:20