0
votes

I have a UIViewController with two UITableViews,one custom cell XIB tableview and another default dynamic tableview.The custom cell tableview fires its delegate but the other table view doesn't .Here is my code

@interface Question : UIViewController<UITableViewDataSource,UITableViewDelegate>

And .m

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
if(tableView == _similarTable)
    {
tableCell =(SimilarQuestion *)[_similarTable dequeueReusableCellWithIdentifier:@"similarTable"];

if(tableCell == nil)
    {
tableCell = [[[NSBundle mainBundle]loadNibNamed:@"SimilarQuestion" owner:self options:Nil]objectAtIndex:0];

    }
if(finalAskerArray!=nil && finalQuesArray.count >0)
    {
tableCell.questionText.text= [finalQuesArray objectAtIndex:indexPath.row];
tableCell.lblAsker.text=[finalAskerArray objectAtIndex:indexPath.row];        
    }
    return tableCell;
}
else
{
    NSLog(@"==>Hit");
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"categoryTable"];
    if (cell == nil)
    {
        cell = [[[NSBundle mainBundle]loadNibNamed:@"categoryTable" owner:self options:nil]objectAtIndex:0];            
    }
    if(popCategoryArray != nil && popCategoryArray.count >0)
    {
        cell.textLabel.text = [popCategoryArray objectAtIndex:indexPath.row];
    }
    return cell;
}
}

And I call the default table view on click of a button ,the code on button click as below

-(IBAction)catBtnPressed:(id)sender{if(categoryArray.count !=0)    {
popCategoryArray = categoryArray;
    categoryView = [[UIView  alloc]initWithFrame:CGRectMake(0.0, 0.0, 320 , 548)];
    categoryTable.delegate   = self;
    categoryTable.dataSource =self ;
            [UIView transitionWithView:categoryView duration:2.0
                       options:UIViewAnimationOptionTransitionFlipFromTop
                    animations:^{
                        [self.view addSubview:categoryView];
                    }     completion:NULL];

    categoryTable  = [[UITableView alloc]initWithFrame:CGRectMake(0.0, 0.0, 320, 240)];
       [categoryTable reloadData];
    [categoryView addSubview:categoryTable];
    [self.view addSubview:categoryView];

}

}

The [categoryTable reloadData] never calls the delegate. Where am I going wrong?

3
did you set categoryTables delegate? - vikingosegundo
yes check the -(IBAction)catBtnPressed - rootcoder
is catBtnPressed: wired up? - vikingosegundo

3 Answers

1
votes

In your code, you are setting categoryTable.delegate before you set categoryTable. It's hard to figure out what was meant, but this doesn't seem right.

Taking some excerpts from your code, it's not clear what categoryTable is supposed to be set to here, where you set the delegate:

categoryTable.delegate   = self;

then a few lines later you set categoryTable to a new object and add it to the view:

categoryTable  = [[UITableView alloc]initWithFrame:CGRectMake(0.0, 0.0, 320, 240)];
   [categoryTable reloadData];
[categoryView addSubview:categoryTable];

so for that instance of UITableView you haven't set the delegate at the point that reloadData is called.

0
votes

Before call categoryTable.reload() ,first make sure your categoryTable's delegate property and dataSource property refer to your controller,for example :

categoryTable.delegate = self ;
categoryTable.dataSource = self ;

self is the UIViewController whose view contains categoryTable, in you code , self is Question controller.

0
votes

You should set dataSource and delegate of UITableView its init method.

-(IBAction)catBtnPressed:(id)sender{if(categoryArray.count !=0)    {
popCategoryArray = categoryArray;
    categoryView = [[UIView  alloc]initWithFrame:CGRectMake(0.0, 0.0, 320 , 548)];
    [UIView transitionWithView:categoryView duration:2.0
                       options:UIViewAnimationOptionTransitionFlipFromTop
                    animations:^{
                        [self.view addSubview:categoryView];
                    }     completion:NULL];

    categoryTable  = [[UITableView alloc]initWithFrame:CGRectMake(0.0, 0.0, 320, 240)];
    categoryTable.delegate = self; //move from above
    categoryTable.dataSource = self; //move from above
    [categoryTable reloadData];
    [categoryView addSubview:categoryTable];
    [self.view addSubview:categoryView]; //You have addSubview:categoryView in above UIView animation, maybe should not add here

}