简体   繁体   English

如何在drawRect方法中将UIView添加到自定义UITableViewCell

[英]How to add UIView to custom UITableViewCell in drawRect method

In my project need to add uiview in UITableViewCell. 在我的项目中需要在UITableViewCell中添加uiview。 I can add UIView using following code in UITableViewCell class. 我可以使用UITableViewCell类中的以下代码添加UIView。

    -(void)drawRect{
       [cell.contentView addSubview:myView];
    } 

Is this a efficient way to show when large amount of table cell are displaying. 这是显示大量表格单元格时的一种有效显示方法。 Is there other way to add UIView using method like. 还有其他方法可以使用类似方法添加UIView。

    drawInRect

您将需要在- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath ,它是UITableViewDataSource协议的一部分。

Don't use drawRect method that way. 不要那样使用drawRect方法。 That method is using for drawing some parts of the ui-element and may call several times per a sec. 该方法用于绘制ui元素的某些部分,并且每秒可能调用几次。 You should add a subview in cellForRowAtIndexPath method ( UITableViewDataSource protocol) or in - (void)awakeFromNib . 您应该在cellForRowAtIndexPath方法( UITableViewDataSource协议)或- (void)awakeFromNib添加一个子视图。 Don't forget to remove that subview in - (void)prepareForReuse method of your UITableViewCell subclass. 不要忘记在UITableViewCell子类的- (void)prepareForReuse方法中删除该子视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM