gpt4 book ai didi

内部带有 UIControl 的 iOS 原型(prototype)单元格

转载 作者:可可西里 更新时间:2023-11-01 03:40:11 25 4
gpt4 key购买 nike

到目前为止,我只需要用预定义的设计(正常、字幕等)实现原型(prototype)单元,这一直不是问题。

现在我需要实现原型(prototype)单元,其中包含一些控件,例如分段开关、开关或任何其他控件。问题是我一直没能弄清楚触发的 Action 是如何实现的,以及它们与控件有什么关系。此外,我还没有找到任何示例说明如何在单个 UITableViewController 中实现不同的原型(prototype)单元格。

我知道这是一个普遍的问题,但我很感激这里的一些指示。也许有人知道一些文档、教程等。好吧,任何帮助都可以,

提前致谢。

最佳答案

我也花了一段时间才了解如何使用原型(prototype)单元。如果您想访问原型(prototype)单元格内的用户界面元素,您必须创建 UITableViewCell 的子类并将其分配给 Interface Builder 中的原型(prototype)单元格。然后您必须手动定义 IBOutlet 属性,例如:

@interface OptionSwitchCell : UITableViewCell
@property (weak, nonatomic) IBOutlet UISwitch* switchControl;
@end

之后,您可以通过控制从元素拖动到助手 View 中的属性定义来连接界面元素。IBActions 可以在拥有的 View Controller 中定义。您可以按住控制键从界面元素拖动到 View Controller 头文件并创建一个 Action 。在操作实现中,您可能想知道哪个单元触发了操作。我这样做:

@implementation SomeTableViewController

- (IBAction)toggleActivity:(id)sender {
OptionSwitchCell* cell = (OptionSwitchCell *)[sender superview].superview;
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
...
}

@end

另一种查找对应单元格和索引路径的方案(jrturton):

- (IBAction)toggleActivity:(id)sender {
CGPoint hitPoint = [sender convertPoint:CGPointZero toView:self.tableView];
NSIndexPath *hitIndex = [self.tableView indexPathForRowAtPoint:hitPoint];
OptionSwitchCell* cell = (OptionSwitchCell *)[self.tableView cellForRowAtIndexPath:hitIndex];
...
}

虽然这有点古怪,但到目前为止我还没有找到更好的解决方案。希望对您有所帮助。

关于内部带有 UIControl 的 iOS 原型(prototype)单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10258082/

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