内置UIControl的iOS原型单元 [英] iOS Prototype Cells with UIControls inside
问题描述
到目前为止,我只需要实现具有预定义设计(正常,副标题等)的原型单元,这不是问题。
So far I´ve only needed to implement prototype cells with pre-defined designs (normal, subtitle, etc.) and it hasn´t been a problem.
现在我需要实现原型单元,其中包含一些控件,如分段开关,开关或任何其他。问题是我无法找到触发的动作是如何实现的以及它们与控件的关系。此外,我还没有找到任何关于如何在单个UITableViewController中实现不同原型单元的示例。
Now I need to implement prototype cells which contain some controls like a segmented switch, a switch or any other. The problem is that I haven´t been able to find out how the actions triggered are implemented and how are they related to the control. Also I heven´t found any example of how different prototype cells inside a single UITableViewController are implemented.
我知道这是一个通用的问题,但我会感谢这里的一些指示。也许有人知道一些文档,教程等等。好吧,任何帮助都会这样做,
I know it´s kind of a generic question, but I´d appreciate some pointers here. Maybe someone knows about some documentation, tutorial, etc. Well, any help would do,
提前进行Thnaks。
Thnaks in advance.
推荐答案
我花了一些时间来了解如何使用原型单元格。如果要访问原型单元格内的用户界面元素,则必须创建UITableViewCell的子类并将其分配给Interface Builder中的原型单元格。
然后你必须手动定义IBOutlet属性,例如:
It took me also a while to understand how to use the prototype cells. If you want to access the user interface elements inside a prototype cell, you have to create a subclass of UITableViewCell and assign it to the prototype cell in Interface Builder. Then you have to define the IBOutlet properties manually e.g.:
@interface OptionSwitchCell : UITableViewCell
@property (weak, nonatomic) IBOutlet UISwitch* switchControl;
@end
之后,您可以通过控制拖动来连接界面元素元素到助手视图中的属性定义。
IBActions可以在拥有的View Controller中定义。您可以控制 - 从界面元素拖动到View Controller头文件并创建操作。在动作实现中,您可能想知道哪个单元格正在触发该动作。我这样做:
After that, you can connect the interface elements through control-dragging from the element to the property definition in the assistant view. The IBActions instead can be defined inside the owning View Controller. You can control-drag from a interface element to the View Controller header file and create an action. Inside the action implementation you will likely want to know which cell was been triggering the action. I do it like this:
@implementation SomeTableViewController
- (IBAction)toggleActivity:(id)sender {
OptionSwitchCell* cell = (OptionSwitchCell *)[sender superview].superview;
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
...
}
@end
查找相应单元格和索引路径的另一种解决方案(通过jrturton):
Another solution for finding the corresponding cell and index path (by 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];
...
}
虽然这有点古怪,但我还是避风港到目前为止找不到更好的解决方案。希望有所帮助。
Although this is a bit quirky, I haven't found a better solution so far. Hope that helps.
这篇关于内置UIControl的iOS原型单元的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!