gpt4 book ai didi

ios - 需要帮助了解自定义 uitableviewcell 初始化

转载 作者:行者123 更新时间:2023-11-28 22:12:36 26 4
gpt4 key购买 nike

tableView:cellForRowAtIndexPath 中实现自定义 UITableViewCell 时:这两种方式有什么区别

loadNibNamed:owner:options:

SimpleTableCell *cell = [[SimpleTableCell alloc]init];

loadNibNamed:owner:options: 是否也 alloc init?如果不是,SimpleTableCell 将如何在没有 alloc init 的情况下工作?

SimpleTableCell *cell = (SimpleTableCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SimpleTableCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}

没有显式调用 SimpleTableCell *cell = [[SimpleTableCell alloc]init];

最佳答案

好的,首先。我实际上根本不打算回答这个问题。相反,我将告诉您如何创建和使用自定义 UITableViewCell 子类。你现在的做法是不对的。

让我们继续使用您使用的名称 SimpleTableCell

创建子类

创建 UITableViewCell 的子类。

SimpleTableCell.h

@interface SimpleTableCell : UITableViewCell

// if coding only
@property (nonatomic, strong) UILabel *simpleLabel

// if from nib
@property (nonatomic, weak) IBOutlet UILabel *simpleLabel;

@end

SimpleTableCell.m

#import "SimpleTableCell.h"

@implementation SimpleTableCell

// if coding only
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
//create the simpleLabel and add to self.contentView
}
return self;
}

// if from nib no need to do anything at all

// other stuff...

- (void)prepareForReuse
{
// empty the cell here.
// means you don't have to empty everything out in the controller
self.simpleLabel.text = @"";
}

@end

好的,现在我们有了一个单元类。

如果需要,创建 NIB

看起来您已经在这样做了。

创建具有相同名称的 Nib (或不是,并不重要)。

将顶级项目设为 UITableViewCell 并将子类设置为 SimpleTableCell。现在连接 socket 。在此示例中,simpleLabel 就是连接的全部内容。

用 TableView 注册子类

在拥有 TableView 的 View Controller 中。这意味着 TableView 可以处理单元格的创建和出列,而您根本不需要手动创建它们。

- (void)viewDidLoad
{
[super viewDidLoad];

// set up the other stuff...

// if coding only
[self.tableView registerClass:[SimpleTableCell class] forCellReuseIdentifier:@"SimpleCell"];

// if from a nib
UINib *cellNib = [UINib nibWithNibName:@"SimpleTableCell" bundle:[NSBundle mainBundle]];
[self.tableView registerNib:cellNib forCellReuseIdentifier:@"SimpleCell"];
}

现在您只需让表格处理单元格的创建。它跟踪重用标识符和单元队列,因此它可以正常处理所有事情。您只需要要求它使用您为子类注册的标识符为您出列一个单元格。

- (UITableViewCell*)tableView:(UITableView *)tableView cellFroRowAtIndexPath:(NSIndexPath *)indexPath
{
// This API was introduced in iOS6 and will ALWAYS return a valid cell.
// However, you need to register the class or nib with the table first.
// This is what we did in viewDidLoad.
// If you use a storyboard or nib to create a tableview and cell then this works too.
SimpleTableCell *mySimpleCell = [tableView dequeueReusableCellWithIdentifier:@"SimpleCell" forIndexPath:indexPath];

mySimpleCell.simpleLabel.text = @"Hello, World";

return mySimpleCell;
}

编辑

您可以使用...创建一个单元格(实际上是任何类)

SimpleTableCell *cell = [[SimpleTableCell alloc] init];

但这样做意味着它不关联到 TableView 或队列的一部分。

从我的回答中的方法向下一步(如果你愿意)是使用旧的 dequeuReusableCell... 方法,然后检查它是否为 nil 并像这样创建它...

- (UITableViewCell*)tableView:(UITableView *)tableView cellFroRowAtIndexPath:(NSIndexPath *)indexPath
{
// Old way, don't do this if you're targeting iOS6.0+
SimpleTableCell *mySimpleCell = [tableView dequeueReusableCellWithIdentifier:@"SimpleCell"];

if (!mySimpleCell) {
// have to use initWithStyle:reuseIdentifier: for the tableView to be able to dequeue
mySimpleCell = [[SimpleTableCell alloc] initWithStyle:UITableViewCellStyleCustom reuseIdentifier:@"SimpleCell"];
}

mySimpleCell.simpleLabel.text = @"Hello, World";

return mySimpleCell;
}

我什至不确定您是否可以从此处的 Nib 加载,因为您无法在单元格上设置重用标识符。

多个细胞亚类

好的,最后一次编辑 :D

对于多个 UITableViewCell 子类,您也可以使用它。我过去就是这样做的。例如,您可能有一个单元格用于 Post 项、一个单元格用于一个 Image 项、一个单元格用于一个 Comment 项等...他们都是不同的。

所以...

- (void)viewDidLoad
{
// the rest

// register each subclass with a different identifier
[self.tableView registerClass:[PostCell class] forCellReuseIdentifier:@"PostCell"];
[self.tableView registerClass:[ImageCell class] forCellReuseIdentifier:@"ImageCell"];
[self.tableView registerClass:[CommentCell class] forCellReuseIdentifier:@"CommentCell"];
}

为了帮助保持这个小(并且它对 NSFetchedResultsControllers 也很方便,我将单元的配置移到另一个方法。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;

if (the required cell is a post cell) {
cell = [tableView dequeueReusableCellWithIdentifier:@"PostCell" forIndexPath:indexPath];
[self configurePostCell:(PostCell *)cell atIndexPath:indexPath];
} else if (the required cell is a image cell) {
cell = [tableView dequeueReusableCellWithIdentifier:@"ImageCell" forIndexPath:indexPath];
[self configureImageCell:(ImageCell *)cell atIndexPath:indexPath];
} else if (the required cell is a comment cell) {
cell = [tableView dequeueReusableCellWithIdentifier:@"CommentCell" forIndexPath:indexPath];
[self configureCommentCell:(CommentCell *)cell atIndexPath:indexPath];
}

return cell;
}

- (void)configurePostCell:(PostCell *)postCell atIndexPath:(NSIndexPath *)indexPath
{
// get the object to be displayed...

postCell.postLabel = @"This is the post text";
postCell.dateLabel = @"5 minutes ago";
}

- (void)configureImageCell:(ImageCell *)imageCell atIndexPath:(NSIndexPath *)indexPath
{
// get the object to be displayed...

imageCell.theImageView.image = //the image
imageCell.dateLabel = @"5 minutes ago";
}

- (void)configureCommentCell:(CommentCell *)commentCell atIndexPath:(NSIndexPath *)indexPath
{
// you get the picture...
}

关于ios - 需要帮助了解自定义 uitableviewcell 初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22537848/

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