Xib到ViewController

时间:2017-06-08 07:03:49

标签: ios objective-c xib

我有TableViewCell.xib个文件,其中每个单元格中都可以包含名称,联系人等数据摘要。现在,如果我触摸单元格,它将转到另一个ViewController,其中将有详细信息单元格的数据。(ViewController显示的内容并不重要)。我想从这个xib文件的单元格触摸中转到另一个ViewController。我怎样才能做到这一点。

编辑:

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

#import "CallHistoryVC.h"
#import "CallHistoryTableViewCell.h"

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

}
@property (nonatomic, strong) NSMutableArray<RecentCall> *sortedEventArray;
@property (weak, nonatomic) IBOutlet UITableView *tView;

- (void)viewDidLoad {
[super viewDidLoad];
appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
//---add one contact---//
[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);
}

一切正常,但在didSelectRowAtIndexPath中,indexPath未显示表示触摸不起作用。通过单击我想要转到stroyboard中的另一个viewcontroller的单元格。

2 个答案:

答案 0 :(得分:2)

实施 didSelectRowAtIndexPath ,如下所示

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

在xib文件的表格视图单元格中将单元格ID名称设置为 CallHistoryTableViewCell 。 现在,在 viewDidLoad 中注册您的自定义tableView单元格

[self.tableView registerNib:@"CallHistoryTableViewCell" forCellReuseIdentifier:@"CallHistoryTableViewCell"];

并将以下代码放入 cellForRowAtIndexPath

CallHistoryTableViewCell *cell = (CallHistoryTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"CallHistoryTableViewCell"]; 
return cell;

答案 1 :(得分:0)

如果要将自定义单元格注册为xib,请在viewDidLoad

中执行以下操作
[self.tableView registerNib:@"YourNibName" forCellReuseIdentifier:@"YourCellId"];

<强> yourXibname:

enter image description here

YourCellid:

enter image description here

顺便说一下,在你的cellForRowAtIndexPath中:这样做。

yourCustomCell *cell = (yourCustomCell *)[tableView dequeueReusableCellWithIdentifier:@"yourCellId"];