【问题标题】:Custom cell tableview and default UITableView in one UIViewController一个 UIViewController 中的自定义单元格 tableview 和默认 UITableView
【发布时间】:2014-03-02 11:23:19
【问题描述】:

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

@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] 从不调用委托。我哪里错了?

【问题讨论】:

  • 你设置 categoryTables 委托了吗?
  • 是的,检查 -(IBAction)catBtnPressed
  • catBtnPressed: 接线了吗?

标签: ios iphone objective-c uitableview


【解决方案1】:

在您的代码中,您在设置categoryTable 之前设置categoryTable.delegate。很难弄清楚是什么意思,但这似乎不对。

从您的代码中摘录一些片段,不清楚 categoryTable 应该在此处设置什么,您在哪里设置委托:

categoryTable.delegate   = self;

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

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

所以对于那个 UITableView 实例,您没有在调用 reloadData 时设置委托。

【讨论】:

    【解决方案2】:

    你应该设置 UITableView 的 dataSource 和 delegate 的 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
    
    }
    

    【讨论】:

      【解决方案3】:

      在调用categoryTable.reload() 之前,首先确保您的categoryTabledelegate 属性和dataSource 属性引用您的控制器,例如:

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

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

      【讨论】:

      • 检查我已经声明的 -(IBAction)catBtnPressed 方法
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多