【问题标题】:Change UITableView color automatically?自动更改 UITableView 颜色?
【发布时间】:2012-04-19 04:35:26
【问题描述】:

我正在尝试根据 if 语句有条件地隐藏我的 UITableView。

现在我有以下代码,当我加载应用程序时,它会检查条件并且可以正常工作,当我按下重新加载按钮时,它会检查并且可以正常工作。

我还有一个 ViewWillAppear 语句,如果我更改视图并再次返回 TableView,它将起作用。

现在我的问题是我需要什么 void 或方法来自动执行此操作 - 即无需按下按钮或更改视图??

现在代码显示了我拥有的 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];
}

杰夫干杯

【问题讨论】:

    标签: ios uitableview show-hide


    【解决方案1】:

    我想通了

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

    无论如何谢谢:-)

    【讨论】:

    • 另外,检查获取的结果控制器委托,特别是这个方法: - (void)controllerDidChangeContent:(NSFetchedResultsController *)controller。它会通知您更改。你可以用这个替换 reloadAction。
    • 是的,我会记住这一点来检查一下,我只是用上面显示的语句进行了尝试,但它不会自动执行。我需要调查一下:-) 谢谢。
    • 祝你好运。确保将 fetchedResultsController 委托设置为实现 controllerDidChangeContent 的类......可能是“自我”。
    猜你喜欢
    • 2013-02-18
    • 1970-01-01
    • 2010-10-23
    • 1970-01-01
    • 1970-01-01
    • 2016-09-06
    • 2011-03-18
    • 2011-12-20
    • 1970-01-01
    相关资源
    最近更新 更多