具有自定义初始化程序的UITableViewCell dequeueReusableCellWithIdentifier(UITableViewCell dequeueReusableCellWith

编程入门 行业动态 更新时间:2024-10-28 10:27:52
具有自定义初始化程序的UITableViewCell dequeueReusableCellWithIdentifier(UITableViewCell dequeueReusableCellWithIdentifier with custom initializer)

我正在使用[UITableView registerClass: forReuseIdentifier:]和[UITableView dequeueReusableCellWithIdentifier:]来排队和出列UITableViewCells。

例如,在viewDidLoad中:

[self.storeTableView registerClass:[StoreLineGraphCell class] forCellReuseIdentifier:@"StoreLineGraphCellIdentifier"];

在cellForRowAtIndexPath中:

StoreLineGraphCell *cell = (StoreLineGraphCell*)[self.storeTableView dequeueReusableCellWithIdentifier:@"StoreLineGraphCellIdentifier"];

在这样做时,为initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier调用initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier初始值设定项。 问题是我需要使用自定义初始化程序来创建具有必要选项的单元格。 例如,能够做这样的事情:

StoreLineGraphCell *cell = [[StoreLineGraphCell alloc] initWithReuseIdentifier:@"StoreLineGraphCell" isLocked:YES isUpcoming:YES];

registerClass和dequeue模式似乎不可能这样。 我想将它保存在初始化程序中,因为它应该只运行一次,而不是每次单元格出列时。 有没有正确的方法来实现这一目标?

I'm using [UITableView registerClass: forReuseIdentifier:] and [UITableView dequeueReusableCellWithIdentifier:] in order to queue and dequeue UITableViewCells.

For example, in viewDidLoad:

[self.storeTableView registerClass:[StoreLineGraphCell class] forCellReuseIdentifier:@"StoreLineGraphCellIdentifier"];

And in cellForRowAtIndexPath:

StoreLineGraphCell *cell = (StoreLineGraphCell*)[self.storeTableView dequeueReusableCellWithIdentifier:@"StoreLineGraphCellIdentifier"];

In doing this, the initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier initializer is called for the UITableViewCell. The problem is that I need to use a custom initializer in order to create the cell with necessary options. For example, the ability to do something like this:

StoreLineGraphCell *cell = [[StoreLineGraphCell alloc] initWithReuseIdentifier:@"StoreLineGraphCell" isLocked:YES isUpcoming:YES];

This doesn't seem possible with the registerClass & dequeue pattern. I'd like to keep it in an initializer as it should only be run once, not every time the cell is dequeued. Is there a proper way to accomplish this?

最满意答案

当你遵循通常的单元格重用模式时(就像你使用注册类和出队一样),我看不到一种易于实现的方法。

如果我是你,我会创建一个额外的初始化方法(不遵循通常的obj-c init模式)或简单地设置并在dequeueReusableCellWithIdentifier调用之后调用它。

StoreLineGraphCell *cell = (StoreLineGraphCell*)[self.storeTableView dequeueReusableCellWithIdentifier:@"StoreLineGraphCellIdentifier"]; [cell furtherInitWithLocked:YES andUpcoming:NO]; // ... or so

While you follow the usual pattern for cell re-usage (as you do with register class & dequeue), I do not see an easy to implement way of doing that.

If I were you I would create an additional initialization method (not following the usual init pattern of obj-c) or simply setters and call that following the dequeueReusableCellWithIdentifier call.

StoreLineGraphCell *cell = (StoreLineGraphCell*)[self.storeTableView dequeueReusableCellWithIdentifier:@"StoreLineGraphCellIdentifier"]; [cell furtherInitWithLocked:YES andUpcoming:NO]; // ... or so

更多推荐

本文发布于:2023-08-07 22:16:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1466180.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   初始化   程序   UITableViewCell   custom

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!