gpt4 book ai didi

ios - 如何获取原型(prototype)单元格的 nibWithNibName?

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

我想在我的应用中使用 registerNib: forCellReuseIdentifier: 方法

当我使用 xib 文件 制作自定义单元格时很好。 (例如:SwitchTableViewCell2.xib)因为文件名是 xib 名称。

运行良好的代码:

[self.tableView registerNib:[UINib nibWithNibName:@"SwitchTableViewCell2" bundle:nil] forCellReuseIdentifier:@"xibcell"];

但是当我在 mainStoryboard 的 tableview 上制作原型(prototype)单元时没有工作。

如何获取storyboard上prototypecell的xibname?

任何人都可以测试此链接:github.com/Kernelzero/test_nav


在此处添加图片文件

enter image description here

这个 Controller 名称:'PushSettingController'

这个 Controller 的 Storyboard id:'pushSetting'

property of tableViewController

enter image description here

最佳答案

如果使用原型(prototype)cell,Identifier名称必须与dequeueReusableCellWithIdentifier名称相同

注意:如果使用prototype cell,则不需要注册cell,因为Prototype cell不是CustomCell

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"switchcell"];
if(cell == nil)
{
cell =[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = @"";
return cell;
}

但如果您使用自定义单元格,请按照以下步骤操作

1.在viewDidLoad中你必须注册自定义cell

UINib *cellNib = [UINib nibWithNibName:@"SwitchTableViewCell2" bundle:[NSBundle mainBundle]];
[self.tableView registerNib:cellNib forCellReuseIdentifier:@"switchcell"];

2.OR 在 cellForRowAtIndexPath 中你可以这样写下面的代码

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *customTableIdentifier=@"switchcell";
SwitchTableViewCell2 *cell=(SwitchTableViewCell2*)[tableView dequeueReusableCellWithIdentifier:customTableIdentifier];
if (cell==nil)
{
NSArray *nibs=[[NSBundle mainBundle]loadNibNamed:@"SwitchTableViewCell2" owner:self options:nil];
cell=[nibs objectAtIndex:0];
}
cell.yourSwitchTableViewCell2Label.text = @"";
cell.yourSwitchTableViewCell2TextField.text = @"";
cell.yourSwitchTableViewCell2Button.text = @"";
return cell;
}

关于ios - 如何获取原型(prototype)单元格的 nibWithNibName?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34172474/

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