gpt4 book ai didi

ios - UITableViewCell设计的 Storyboard

转载 作者:行者123 更新时间:2023-11-29 10:23:29 28 4
gpt4 key购买 nike

我以前没有用过 Storyboard。

我想设计一个自定义表格 View 单元格,并想用界面生成器来完成。

我创建了一个 Storyboard并放置了一个 viewController(自定义类)并将自定义单元格放在它下面。

我还连接了电池标签的 socket 。

但是,当我尝试访问标签时,它始终为零。

我正在使用以下代码来实例化代码,我怀疑这是我失败的地方。

我觉得我应该以某种方式告诉我想要 Storyboard中的 View Controller ,不是吗?

 ThreadTempDatasViewController* threadTempDatasViewController = [[ThreadTempDatasViewController alloc] init];
[self.navigationController pushViewController: threadTempDatasViewController animated: NO];

** 编辑 **

我现在有

 UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"ThreadTempData" bundle:nil];
ThreadTempDatasViewController *threadTempDatasViewController = [storyboard instantiateViewControllerWithIdentifier:@"ThreadTempDatasViewController"];

[self.navigationController pushViewController: threadTempDatasViewController animated: NO];

我得到错误

Storyboard () doesn't contain a view controller with identifier 'ThreadTempDatasViewController'

那么除了在 interfacebuilder 中将 viewcontroller 放在 Storyboard中之外,我如何通知 Storyboard他有这个 viewcontroller?

** 编辑 2 **

answer> 您应该在 interfacebuilder 中为 viewcontroller 设置 Storyboard ID。

现在我回到原来的位置,单元格的标签是 nil..

** 编辑 3 **

answer> 所以当您使用 interfacebuilder 时,您不应该有 self.tableView.registerClass(ThreadTempDataTableViewCell.self, forCellReuseIdentifier: "ThreadTempDataTableViewCell")

我在 IB 中为 TableView 单元格的类字段设置了 ThreadTempDataTableViewCell 并像下面这样使用它。

 let cellIdentifier = "ThreadTempDataTableViewCell"
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! ThreadTempDataTableViewCell

最佳答案

我可以看到几个问题。因此,我将逐步指导您。我的理解是你想要一个 View Controller 来保存一个 Table View 并且在那个 Table View 里面,你想要一个自定义的 TableView Cell。

所以,除了尝试错误地实例化 ViewController 之外,我觉得还有一件事。您需要有一个 TableView 来容纳您的 TableView Cell。

因此,您需要考虑以下步骤:

第 1 步:在 Storyboard中拖动一个 View Controller 。就我而言, Storyboard的名称是“Main”。

所以,请注意如何使用 Storyboard标识符加载 ViewController-

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
ThreadTempDatasViewController *threadTempDatasViewController = [storyboard instantiateViewControllerWithIdentifier:@"ThreadTempDatasViewController"];

[self.navigationController pushViewController: threadTempDatasViewController animated: NO];

所以,回到将对象添加到您的 View Controller -现在将 UITableView 拖放到 UIViewController 的 View 中。然后将 UITableViewCell 拖放到您的 TableView 中。

现在,选择 TableViewCell 以展开并将 UILabel 拖放到 TableViewCell 的内容 View 中。

View 层次结构看起来像这样-

enter image description here

因此,您现在已经拥有了所有的对象。

第 2 步:为您的 View Controller 提供 ID“ThreadTempDatasViewController”,并从 Storyboard中的身份检查器分配其类 (ThreadTempDatasViewController)。

enter image description here

第 3 步:选择 TableViewCell 并设置它的 ReuseIdentifier,在您的情况下为“ThreadTempDataTableViewCell”,这样您就不必注册以编程方式识别的单元格。

enter image description here

另外不要忘记分配它的自定义类,在您的情况下是“ThreadTempDataTableViewCell”

enter image description here

第 4 步:选择您的自定义 TableViewCell 并将 socket 与所需的 UI 对象连接起来。因此,将必须在 ThreadTempDataTableViewCell.h 类中声明的 UILabel 的 Outlet 与单元格内 Storyboard 中的标签连接起来。

例如,如果 ThreadTempDataTableViewCell.h 有一个名为“threadTempDataLAbel”的 UILabel Outlet,它将类似于-

@interface ThreadTempDataTableViewCell : UITableViewCell

@property(nonatomic, strong) IBOutlet UILabel *threadTempDataLAbel;

@end

现在选择 Storyboard中的 ThreadTempDataTableViewCell,然后转到身份检查器中的最后一个图标,并连接到你的 socket 。

enter image description here

连接后,你会看到类似的东西-

enter image description here

为了清楚起见,您的 ViewController 中还应该有 TableView 的 IBOutlet。

#import <UIKit/UIKit.h>

@interface ThreadTempDatasViewController : UIViewController<UITableViewDelegate, UITableViewDataSource>

@property(nonatomic, strong) IBOutlet UITableView *myTableView;

@end

现在,在 Storyboard中选择 ViewController 并像之前一样转到最后一个选项卡并连接到 tableView 的 socket 。

enter image description here

好吧,这应该是您需要的所有设置。现在您可以跳转到您的代码来填充表格或访问 TableViewCell 的属性 (threadTempDataLabel)

我认为,您无法访问 UILabel 的原因是您试图在 ViewController 而不是 TableView 中设置 TableViewCell,或者如果您有 TableView,您可能忘记将数据源设置为 View Controller 。这是完整的代码。

#import "ThreadTempDatasViewController.h"
#import "ThreadTempDataTableViewCell.h"

@interface ThreadTempDatasViewController ()

@end

@implementation ThreadTempDatasViewController

- (void)viewDidLoad {
[super viewDidLoad];
self.myTableView.dataSource=self;
self.myTableView.delegate=self;
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}


#pragma mark- TableView Datasource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 5;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
ThreadTempDataTableViewCell *cell= [tableView dequeueReusableCellWithIdentifier:@"ThreadTempDataTableViewCell"];
cell.threadTempDataLAbel.text = @"test";
return cell;
}
@end

希望这对您有所帮助。

关于ios - UITableViewCell设计的 Storyboard,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33624853/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com