1

I have created a custom tableview displayCustomUnitTableView and a custom UIView displayView and adding displayCustomUnitTableView to displayView. But i am not able to select any row in the tableView i.e., didSelectRowAtIndexPath is not called.

I am using the following code:

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

Thanks in advance.

3
  • Can you show where you have implemented the delegates ?
    – GoodSp33d
    Jun 25, 2015 at 10:29
  • Please check i edited my post
    – VJVJ
    Jun 25, 2015 at 10:42
  • Your code working perfectly in my xcode project. didSelectRowAtIndexPath is working here. only required numberOfRowsInSection delegate of TableView.
    – Om Prakash
    Jun 25, 2015 at 10:52

2 Answers 2

2

Make sure you don't have any UIGestures on the superviews. In that case UITableView will be not getting the touches.

If you have any, handle it using :

- gestureRecognizer:shouldReceiveTouch: delegate method.

0
0

try this Regiseter a cell class with tabel

- (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");
    }
3
  • still its not getting called
    – VJVJ
    Jun 25, 2015 at 10:54
  • are you sure you register cell class with tabel Jun 25, 2015 at 11:08
  • [displayCustomUnitTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"identifier"]; Jun 25, 2015 at 11:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.