【问题标题】:Xib to ViewControllerXib 到 ViewController
【发布时间】:2017-06-08 07:03:49
【问题描述】:

我有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 中的另一个视图控制器。

【问题讨论】:

标签: ios objective-c xib


【解决方案1】:

如下实现 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;

【讨论】:

  • 我可以使用情节提要来创建该视图控制器以从 xib 移动到该视图控制器吗?我不清楚
  • 实际上,当我单击 xib 中的 TableViewCell 时它不起作用,这意味着在 didSelectRowAtIndexPath 中,如果我打印 indexPath 它也不起作用。
  • 但是您尚未在共享的代码中提及它。顺便说一句,如果我的回答有帮助,那么您可以给我一个赞成票。谢谢
  • 但我在另一个应用程序中看到他们使用滑动手势但也可以单击单元格。所以我的问题并不正确,我也必须使用那个手势。那我该怎么做呢
  • 您可以为您的问题设置另一个问题 didSelectRowAtIndexPath 不起作用。它与这个问题无关,伙计,顺便说一下,我认为你是 iOS 的新手,而且堆栈溢出,所以你无法理解我的帮助。谢谢。
【解决方案2】:

如果您想将自定义单元格注册为 xib,请在 viewDidLoad 中执行以下操作

[self.tableView registerNib:@"YourNibName" forCellReuseIdentifier:@"YourCellId"];

您的Xib 名称:

你的Cellid:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-06-02
    • 2023-04-06
    • 2016-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-11
    • 2023-03-21
    相关资源
    最近更新 更多