简体   繁体   English

在uitableviewcell中创建可重用的uiview

[英]Create reusable uiview in uitableviewcell

I have 1 UIView which will be added to my UITableViewCell like this. 我有1个UIView ,它将像这样添加到我的UITableViewCell Although it will be called only 1 time, I feel like it is expensive to do. 尽管只能调用1次,但我觉得这样做很昂贵。 (At first time, it lag. After scroll up and down, it is fast since it is already created) How can I create that view so that it can be reused again and again for other cell as well (no need to init again)? (第一次,它滞后。在向上滚动和向下滚动之后,由于已经创建,因此速度很快)如何创建该视图,以便可以一次又一次地将其用于其他单元格(无需再次初始化) ?

- (void)awakeFromNib 
{
    // Initialization code
    self.vwHeaderWithUserAction = [HeaderWithUserActionInListing loadFromNib];
    self.vwHeaderWithUserAction.delegate = self;
    self.vwHeaderWithUserAction.viewTypeInUserAction = ViewTypeInUserActionInListing;
    [self.contentView addSubview:self.vwHeaderWithUserAction];
}

In cellForRowAtIndexPath method check whether that view is existing if not create new one otherwise use existing one. 在cellForRowAtIndexPath方法中,如果不创建新视图,则检查该视图是否存在,否则使用现有视图。

- (UITableViewCell *)tableView:(UITableView *)tableViewLocal cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (!myview)
     {
         // Initialize myView
      }
  // Do Necessary things 
   [cell.contentView addSubview:myview];
}

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

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