2011-12-06 90 views
0

我用的UITableView在UIViewController..I工作使用下面的委託方法我.....調用UITable查看委託方法

-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 

一旦我加載視圖我發現所有代表被調用。 ....但我需要調用這些方法中的一個按鈕,點擊.....
我這樣試過....

[self tableView:table numberOfRowsInSection:rows]; 
[self tableView:table cellForRowAtIndexPath:path]; 

我發現,這些方法被調用,但什麼也沒發生(這些代表內部的代碼不起作用)....我不知道爲什麼?有什麼建議麼???

+0

你不應該直接調用這些委託方法,而是實現它們,以便表視圖可以調用它們來詢問它需要顯示的信息。請定義什麼「不起作用」 - 你看到你期望看到什麼? – jrturton

回答

0

寫的按鈕點擊函數裏面的代碼,它會調用的實現代碼如下

[self.tableView reloadData]; 

或完整代碼解決方案的所有代表是: -

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 


    static NSString *CellIdentifier = @"Cell"; 

    customCell *cell = [[customCell alloc]init]; 
    if (cell == nil) { 
     cell = [[[customCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; 
    } 

    // Configure the cell 
    cell.selectionStyle = UITableViewCellSelectionStyleBlue; 


    NSArray *topLevelObjects=[[NSBundle mainBundle] loadNibNamed:@"customCell" owner:self options:nil]; 
    cell = (customCell *)[topLevelObjects objectAtIndex:0]; 


    int count=0; 
    for(id currentObject in topLevelObjects) 
    { 
     count++; 
     if([currentObject isKindOfClass:[customCell class]]) 
     { 

      cell= (customCell *)currentObject; 

      UILabel *sName = (UILabel *)[cell.contentView viewWithTag:1]; 

      //Checking the flag that is set in the btnClicked event 
      if (flag) { 
        sName.hidden=YES; 
       } 

       else{ 
       sName.text = [arrayResult objectAtIndex:indexPath.row];    
       } 

     } 
    } 

    return cell; 
    [topLevelObjects release]; 
    [cell release]; 

} 

-(IBAction) btnClicked:(id) sender{ 
flag=YES; 
[self.tableView reloadData]; 
} 

試試這個它會工作。

+0

感謝它工作正常 – Icoder

+0

多一個查詢....在表格的每個單元格內我有一個標籤..當我重新加載表格時,所有標籤都不會被刪除... Hw可以刪除所有標籤點擊 – Icoder

+0

我認爲你有一個自定義單元格...在重新加載表之前應用一個布爾標誌= YES;並在您的cellAtRowAtIndexPath委託的uitableview檢查標誌,如果是隱藏標籤。 –