didSelectRowAtIndexPath in TableViewCell 不起作用



我有TableViewCell.xib文件,每个单元格中都可以有姓名,联系人等数据的摘要。现在,如果我触摸单元格,它将转到另一个视图控制器,其中将有该单元格数据的详细信息。(此视图控制器显示什么无关紧要(。我想从这个 xib 文件的单元格触摸转到另一个视图控制器。我怎么能这样做。

CallHistoryTableViewCell.h

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

此类具有 CallHistoryTableViewCell.xib 文件。

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

一切正常,但在didSelectRowAtIndexPathindexPath中没有显示这意味着触摸不起作用。通过单击单元格,我想转到stroyboard中的另一个视图控制器。

在单元格内放置一个按钮。对于 IndexPath 的按钮操作,请使用以下代码:

var buttonPosition: CGPoint = sender.convertPoint(CGPointZero, toView: tableview)
let indexpath = tableview.indexPathForRowAtPoint(buttonPosition)

不要将UISwipeGestureRecognizer添加到单元格。将其添加到UITableView并处理下面给出的类似内容。在该操作方法中,您将获得用户触摸区域的索引路径。

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

在这种情况下,didSelectRowAtIndexPath将无故障调用

最新更新