2012-11-07 66 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

相關問題