0

I have TableViewCell.xib file where in each cell there can be summary of data like name, contact etc. Now if I touch the cell it will go to another ViewController where there will be details of that cell's data.(it does not matter what this ViewController shows). I want to go to another ViewController from this xib file's cell touch. how can I do that.

CallHistoryTableViewCell.h

@interface CallHistoryTableViewCell : UITableViewCell
@property(nonatomic, strong) IBOutlet UILabel *contactName;
@property(nonatomic, strong) IBOutlet UILabel *contactNumber;
@end

this class has CallHistoryTableViewCell.xib file.

CallHistoryVC.h

@interface CallHistoryVC : UIViewController
@end

CallHistoryVC.m

@interface CallHistoryVC ()<UITableViewDelegate, UITableViewDataSource,CallingViewControllerDelegate> {
AppDelegate *appDelegate;

}

@implementation CallHistoryVC

- (void)viewDidLoad {
[super viewDidLoad];
appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;

[self defaultViewSetting];
_tView.delegate = self;
_tView.dataSource = self;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 75;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return _sortedEventArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"CallHistoryTableViewCell";

return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UIStoryboard *mainStoryBoard = [UIStoryboard         storyboardWithName:@"MainStoryboard" bundle:nil];
DetailsOfHistory *storyViewController = [mainStoryBoard instantiateViewControllerWithIdentifier:@"DetailsOfHistory"];
//[self presentViewController:storyViewController animated:YES completion:nil]; //  present it
[self.navigationController pushViewController:storyViewController animated:YES];// or push it

NSLog(@"%ld",(long)indexPath.row);
}

Everything works fine but in didSelectRowAtIndexPath indexPath.row is not showing that means touch not working. By click on the cell I want to go to another viewcontroller in stroyboard.

sazid008
  • 175
  • 2
  • 14

2 Answers2

0

Place a button inside the cell. For the button's action for the IndexPath, use the code below:

var buttonPosition: CGPoint = sender.convertPoint(CGPointZero, toView: tableview)
let indexpath = tableview.indexPathForRowAtPoint(buttonPosition)
LinusGeffarth
  • 27,197
  • 29
  • 120
  • 174
shirish
  • 11
  • 6
0

Don't add the UISwipeGestureRecognizer to Cell. Add it to UITableView and handle the like given below. In that action method you will get the indexpath of user touch area.

func handleSwipe(sender: UISwipeGestureRecognizer){
    if longPress.state == UIGestureRecognizerState.Began {
        let touchPoint = longPress.locationInView(self.view)
        if let indexPath = yourTableView.indexPathForRowAtPoint(touchPoint) {
            // your code here, get the row for the indexPath or do whatever you want
        }
    }
}

In this scenario didSelectRowAtIndexPath will call without fail

Bala
  • 1,224
  • 1
  • 13
  • 25