【问题标题】:after adding uitableview to uiview didselectrowatindexpath not called ios将 uitableview 添加到 uiview 后 didselectrowatindexpath 未调用 ios
【发布时间】:2015-06-25 10:11:34
【问题描述】:

我创建了一个自定义 tableview displayCustomUnitTableView 和一个自定义 UIView displayView 并将 displayCustomUnitTableView 添加到 displayView。但我无法选择 tableView 中的任何行,即未调用 didSelectRowAtIndexPath

我正在使用以下代码:

displayCustomUnitTableView = [[UITableView alloc]initWithFrame:CGRectMake(52.0, 110.0, 180.0, 110.0) style:UITableViewStylePlain];
displayCustomUnitTableView.delegate = self;
displayCustomUnitTableView.dataSource = self;
displayCustomUnitTableView.tag = 2;
displayCustomUnitTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
displayCustomUnitTableView.separatorColor = [UIColor blackColor];
displayCustomUnitTableView.rowHeight = 30.f;
[displayCustomUnitTableView setBackgroundColor:[ContentViewController colorFromHexString:@"#BCC9D1"]];
[displayCustomUnitTableView setBackgroundColor:[UIColor groupTableViewBackgroundColor]];
[displayCustomUnitTableView setAllowsSelection:YES];
[displayView addSubview:displayCustomUnitTableView];

cellForRowAtIndexPath:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  static NSString *CellIdentifier = @"MyCell";
  UITableViewCell *cell= [tableView cellForRowAtIndexPath:indexPath];
  if (cell == nil)
  {
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault  reuseIdentifier:CellIdentifier];
  }
  if(tableView.tag == 1)
  {   
    cell.textLabel.text = [storeUnitList objectAtIndex:indexPath.row];
  }
  else if(tableView.tag == 2)
  {
    cell.textLabel.text = [storeCustomUnitList objectAtIndex:indexPath.row];
  }
   return cell;
 }

didSelectRowAtIndexPath:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
 if(tableView.tag == 1)
 {
    unitString = [storeUnitList objectAtIndex:indexPath.row];
 }
 else if(tableView.tag == 2)
 {
    unitString = [storeCustomUnitList objectAtIndex:indexPath.row];
 }
}

提前致谢。

【问题讨论】:

  • 你能说明你在哪里实现了代表吗?
  • 请检查我编辑了我的帖子
  • 您的代码在我的 xcode 项目中运行良好。 didSelectRowAtIndexPath 在这里工作。只需要 TableView 的 numberOfRowsInSection 委托。

标签: ios objective-c uitableview didselectrowatindexpath


【解决方案1】:

确保您在超级视图上没有任何 UIGestures。在这种情况下,UITableView 将无法获得触摸。

如果有,请使用:

- gestureRecognizer:shouldReceiveTouch: 委托方法。

【讨论】:

    【解决方案2】:

    试试这个 用表格注册一个单元格类

    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        displayCustomUnitTableView = [[UITableView alloc]initWithFrame:CGRectMake(52.0, 110.0, 180.0, 110.0) style:UITableViewStylePlain];
    displayCustomUnitTableView.delegate = self;
    displayCustomUnitTableView.dataSource = self;
    displayCustomUnitTableView.tag = 2;
    displayCustomUnitTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    displayCustomUnitTableView.separatorColor = [UIColor blackColor];
    displayCustomUnitTableView.rowHeight = 30.f;
    [displayCustomUnitTableView setBackgroundColor:[ContentViewController colorFromHexString:@"#BCC9D1"]];
    [displayCustomUnitTableView setBackgroundColor:[UIColor groupTableViewBackgroundColor]];
    [displayCustomUnitTableView setAllowsSelection:YES];
    [displayView addSubview:displayCustomUnitTableView];
    
        [displayCustomUnitTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"identifier"];
    
    }
    
        - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{return 1;}
    
    
    
        - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"i" forIndexPath:indexPath];
    
            /*
    
             Write code
    
             */
             cell.textLabel.text= @"hello";
    
    
            return cell;
    
        }
        - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
            NSLog(@"Click");
        }
    

    【讨论】:

    • 仍然没有被调用
    • 你确定你用表注册单元格
    • [displayCustomUnitTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"identifier"];
    猜你喜欢
    • 2012-03-05
    • 1970-01-01
    • 1970-01-01
    • 2015-11-22
    • 2012-09-18
    • 2019-05-09
    • 1970-01-01
    • 2016-06-19
    • 2013-12-07
    相关资源
    最近更新 更多