سؤال

In my app I am having a view controller with a button on top of it. When the button is clicked a table view will animate from the left to right.below image display the content of the screen when the button is not pressed.
The right side image displays the table view when the button is pressed.

image before the button pressed enter image description here

This code runs when the button is pressed.

-(void) profileButtonClicked
{
profileTable=[[UITableView alloc]initWithFrame:CGRectMake(-220, 0, 220, height) style:UITableViewStylePlain];
profileTable.delegate=self;
profileTable.dataSource=self;
profileTable.backgroundColor=[HexColorCode colorWithHexastring:@"252020"];
profileTable.userInteractionEnabled=YES;
[self.view addSubView: profileTable];
if (profileButton.tag==1)
{

    CGPoint newLeftCenter = CGPointMake( 220.0f + self.view.frame.size.width / 2.0f, self.view.center.y);
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:1];
    self.view.center = newLeftCenter;
    [UIView commitAnimations];

    CGPoint newLeftCenterForTable = CGPointMake( 1- profileTable.frame.size.width / 2.0f, profileTable.center.y);
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:1];
    profileTable.center = newLeftCenterForTable;
    [UIView commitAnimations];
    profileTable.userInteractionEnabled=YES;
     [self.view addSubview:profileTable];
    profileButton.tag=2;

}
else
{
    profileButton.tag=1;

    CGPoint newLeftCenter = CGPointMake(  self.view.frame.size.width / 2.0f, self.view.center.y);
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:1.0f];
    self.view.center = newLeftCenter;
    [UIView commitAnimations];

    CGPoint newLeftCenterForTable = CGPointMake(  profileTable.frame.size.width / 2.0f-220, profileTable.center.y);
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:1.0f];
    profileTable.center = newLeftCenterForTable;
    [UIView commitAnimations];

}
}

My problem is that, when a press a row on the table, it is not recognized. That is. it is not entering the

didSelectrowAtIndexPath:

هل كانت مفيدة؟

المحلول

The problem is that your tableview is outside your controller's view frame. What you should keep in mind is that self.view should not move. (i've never seen a case where it was useful).

What i would recommend is having a parent view controller. You will add the tableview to your parent and it will be the delegate. You will then add a child view controller which is your current controller (the one displaying Taxi Match).

In your parent view controller :

 -(void)viewDidLoad{
    yourController = [[UIViewController alloc] init]; 
    [self addChilViewController:yourController];
    [self.view addSubview:yourController.view];

    profileTable=[[UITableView alloc]initWithFrame:CGRectMake(-220, 0, 220, height) style:UITableViewStylePlain];
    profileTable.delegate=self;
    profileTable.dataSource=self;
    profileTable.backgroundColor=[HexColorCode colorWithHexastring:@"252020"];
    profileTable.userInteractionEnabled=YES;
    [self.view addSubView: profileTable];
    ....
}

Then when you touch your button in your child view controller you should call the parent and do :

-(void)moveController{
    CGPoint newLeftCenter = CGPointMake( 220.0f + self.view.frame.size.width / 2.0f, self.view.center.y);
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:1];
    yourController.view.center = newLeftCenter;
    [UIView commitAnimations];

    CGPoint newLeftCenterForTable = CGPointMake( 1- profileTable.frame.size.width / 2.0f, profileTable.center.y);
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:1];
    profileTable.center = newLeftCenterForTable;
    [UIView commitAnimations];
    profileTable.userInteractionEnabled=YES;
     [self.view addSubview:profileTable];
    profileButton.tag=2;
}

نصائح أخرى

Please check the delegate method, I think you would have used didDeselectRow inplace of didSelectRow. Also make sure that you have connected the delegate.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top