简体   繁体   English

一个UIViewController中的自定义单元格tableview和默认的UITableView

[英]Custom cell tableview and default UITableView in one UIViewController

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 我有一个带有两个UITableViews的UIViewController,一个自定义单元格XIB表格视图和另一个默认动态表格视图。自定义单元格表格视图会激发其委托,但另一个表格视图则不会。这是我的代码

@interface Question : UIViewController<UITableViewDataSource,UITableViewDelegate>

And .m 和.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. [categoryTable reloadData]从不调用委托。 Where am I going wrong? 我要去哪里错了?

In your code, you are setting categoryTable.delegate before you set categoryTable . 在代码中,先设置categoryTable.delegate然后再设置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方法:

categoryTable.delegate   = self;

then a few lines later you set categoryTable to a new object and add it to the view: 然后几行,您将categoryTable设置为新对象并将其添加到视图中:

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. 因此,对于该UITableView实例,您尚未在调用reloadData设置委托。

Before call categoryTable.reload() ,first make sure your categoryTable 's delegate property and dataSource property refer to your controller,for example : 在调用categoryTable.reload()之前,首先要确保categoryTabledelegate属性和dataSource属性引用了您的控制器,例如:

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

self is the UIViewController whose view contains categoryTable , in you code , self is Question controller. self是UIViewController,其视图包含categoryTable ,在您的代码中, selfQuestion控制器。

You should set dataSource and delegate of UITableView its init method. 您应该设置dataSource和UITableView的init方法的委托。

-(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

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM