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

时间:2014-03-02 11:23:19

标签: ios iphone objective-c uitableview

我有一个带有两个UITableView的UIViewController,一个自定义单元格XIB tableview和另一个默认动态tableview。自定义单元格tableview触发其委托,但另一个表格视图没有。这是我的代码

@interface Question : UIViewController<UITableViewDataSource,UITableViewDelegate>

和.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;
}
}

我点击按钮调用默认表格视图,按钮上的代码点击如下

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

}

}

[categoryTable reloadData]从不调用委托。我哪里错了?

3 个答案:

答案 0 :(得分:1)

在您的代码中,您在设置categoryTable.delegate之前设置了categoryTable。很难弄清楚它的含义,但这似乎不对。

从您的代码中摘录一些内容,不清楚应该将categoryTable设置为何处设置委托:

categoryTable.delegate   = self;

然后几行后你将categoryTable设置为一个新对象并将其添加到视图中:

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

因此,对于UITableView的实例,您没有在调用reloadData的位置设置委托。

答案 1 :(得分:0)

在致电categoryTable.reload()之前,请先确保您的categoryTable delegate属性和dataSource属性引用您的控制器,例如:

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

self是UIViewController,其视图包含categoryTable,在您的代码中,selfQuestion控制器。

答案 2 :(得分:0)

您应该将UITableView的dataSource和委托设置为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

}