2012-11-07 67 views
0

我需要在某些情况下显示/隐藏UITableView。但它有时运行绝对正常。但有时tableView未被隐藏。在iPhone中隐藏/显示UITableView?

如果数组数据存在我填写它在UITableView.But如果数组为空我显示一个标签文本“数据未找到”..这是完美的工作,但如果我反复这样做然后是越来越显示我的TableView的数据标签文本不隐藏tableView ..Couldn't明白的地方我要去错了..

这是我正在写的代码...

if([arr count]==0) 
{ 
    [email protected]""; 
    lblError = [[UILabel alloc] init]; 
    [lblError setFrame:CGRectMake(0,390,320,200)]; 
    lblError.textAlignment=UITextAlignmentLeft; 
    lblError.backgroundColor = [UIColor clearColor]; 
    lblError.text = @"Results not found"; 
    lblError.textColor = [UIColor redColor]; 
    lblError.shadowColor = [UIColor blackColor]; 
    lblError.font = [UIFont fontWithName:@"Verdana-Italic" size:15];  
    [self.view addSubview:lblError]; 
    tableView.hidden=YES; 
    [tableView removeFromSuperview]; 
    tableView=nil; 

} 
else 
{ 
    sections=[[NSMutableArray alloc] init]; 
    for(int s=0;s<1;s++) 
    { 
     NSMutableArray *section=[[NSMutableArray alloc] init]; 
     for(int i=0;i<[arr1 count];i++) 
     { 
      Item *item=[[Item alloc] init]; 
      NSString *name=[[arr objectAtIndex:i]objectForKey:@"Name"]; 
      item.Name=name; 
      [section addObject:item]; 

     } 
     [sections addObject:section]; 

    } 
    tableView=[[UITableView alloc]initWithFrame:CGRectMake(0,430,320,200) style:UITableViewStylePlain]; 
    tableView.delegate = self; 
    tableView.dataSource = self; 
    [self.view addSubview:tableView]; 
    [tableView release]; 

} 

回答

0

拳头我已经解决了它分配的tableView的委托和数据源到零,然后创建它再次

tableView.delegate = nil; 
tableView.dataSource = nil; 

if([arr count]==0) 
{ 
[email protected]""; 
lblError = [[UILabel alloc] init]; 
[lblError setFrame:CGRectMake(0,390,320,200)]; 
lblError.textAlignment=UITextAlignmentLeft; 
lblError.backgroundColor = [UIColor clearColor]; 
lblError.text = @"Results not found"; 
lblError.textColor = [UIColor redColor]; 
lblError.shadowColor = [UIColor blackColor]; 
lblError.font = [UIFont fontWithName:@"Verdana-Italic" size:15];  
[self.view addSubview:lblError]; 
tableView.hidden=YES; 
[tableView removeFromSuperview]; 
tableView=nil; 

} 
else 
{ 
sections=[[NSMutableArray alloc] init]; 
for(int s=0;s<1;s++) 
{ 
    NSMutableArray *section=[[NSMutableArray alloc] init]; 
    for(int i=0;i<[arr1 count];i++) 
    { 
     Item *item=[[Item alloc] init]; 
     NSString *name=[[arr objectAtIndex:i]objectForKey:@"Name"]; 
     item.Name=name; 
     [section addObject:item]; 

    } 
    [sections addObject:section]; 

} 
tableView=[[UITableView alloc]initWithFrame:CGRectMake(0,430,320,200) style:UITableViewStylePlain]; 
tableView.delegate = self; 
tableView.dataSource = self; 
[self.view addSubview:tableView]; 
[tableView release]; 

} 
0

你为什么分配您的TableViewelse方法中?您应该简单地在ViewDidLoadViewWillAppear中分配TableView。然后根据您的需要隐藏或取消隐藏它。也不要删除TableView

+0

我做到了,因为你说过。但仍然没有运气 – Honey

+0

你在哪里wri上面的代码? – IronManGill

+0

这个概念是我必须从DatePicker中选择一个日期,如果数据存在,我将在tableview中输入它。否则我将显示一个简单的消息“Data not found”。所以我在这里写它 - (void)DatePickerDoneClick :(id)发送者 {}。它工作绝对正常4至5次。但如果我重复多次,然后tableView不会隐藏和消息显示在tableView上,即一个在另一个..所以我应该怎么做? – Honey

0

试试这个在你想隐藏

tableName.hidden = YES; 

尝试一下本作取消隐藏状态/显示

tableName.hidden = NO; 
+0

他已经完成了,请详细参阅他的代码.... – IronManGill

相关问题