自动更改UITableView颜色?

问题描述:

我想基于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]; 
} 

干杯杰夫

我想通了

我补充说,如果声明,其中+符号定义,其中删除方法是在代码中设置:-)

无论如何:-)

+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