自动更改UITableView颜色?

时间:2012-04-19 04:35:26

标签: ios uitableview show-hide

我试图根据if语句有条件地隐藏我的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 个答案:

答案 0 :(得分:0)

我想通了

我添加了if语句到定义了+符号的位置以及在代码中设置delete方法的位置: - )

非常感谢: - )