我可以使用属性来确定对象的类吗?
我在 IB 中创建了一个自定义 UITableViewCell。
我的所有单元格都将具有此总体布局。然而,在一些单元中,两个白色视图将是A类,而在其他单元中,两个白色视图将是B类。 (UIView 的两个子类)。我尝试使用属性分配两个视图的特定子类。
我将两个视图设置为单元格的属性
@property (nonatomic, retain) IBOutlet UIView *leftView;
@property (nonatomic, retain) IBOutlet UIView *rightView;
,在创建表视图的视图控制器中,我尝试创建一个 subclassA 类型的对象并将其分配为 leftView,以便 leftView 的类型为 subclassA。在其他单元格中,我将创建子类 B 的对象并将其设置为左视图,以便在这些单元格中,左视图将属于子类 b。
//equationTextField is a subclass of UIView
EquationTextField *textField = [[EquationTextField alloc] initWithFrame:CGRectMake(0, 0, 100, 100)]; // arbitrary frame
cell.leftView = textField;
然而,这段代码不起作用。有没有更好的方法来分配我的观点的特定类别?
I have a custom UITableViewCell created in IB.
All my cells will have this general layout. However,in some cells, the two white views will be classA and in other cells the two white views will be classB. (Both subclasses of UIView). I tried to assign the specific subclass of the two views using properties.
I set the two views as properties of the cell
@property (nonatomic, retain) IBOutlet UIView *leftView;
@property (nonatomic, retain) IBOutlet UIView *rightView;
And in my view controller where I make the table view, I tried creating an object of type subclassA and assigning it as leftView, so that leftView will be of type subclassA. In other cells, I would create an object of subclass B and set it to leftview, so that in those cells, left view would be of subclass b.
//equationTextField is a subclass of UIView
EquationTextField *textField = [[EquationTextField alloc] initWithFrame:CGRectMake(0, 0, 100, 100)]; // arbitrary frame
cell.leftView = textField;
This code doesn't work, however. Is there a better way to assign the specific class of my views?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在做的事情很好,但您可能想要研究拥有两种不同的单元格类型(已嵌入您的自定义视图)以提高性能 - 每个单元格类型都可以具有不同的重用标识符,您可以根据索引路径进行选择。每次在 cellForRowAtIndexPath 中创建视图从来都不是一个好主意。
您甚至可以从同一单元派生这两种类型,只需使用不同的重用标识符,并仅在首次创建单元时添加正确的子类。
What you are doing is fine, but you may want to investigate having two different cell types (with your custom views already embedded) to improve performance - each could have a different reuse identifier which you would choose depending on the index path. Creating views every time in cellForRowAtIndexPath is never a good idea.
You could even derive both types from the same cell, just use the different reuse identifier and add in the correct subclasses only when first creating the cell.
如果需要,您可以检查 cell.leftView 是否是您的 EquationTextField:
You can check to see if cell.leftView is your EquationTextField if needed: