简体   繁体   English

向headerview添加子视图:添加到错误的部分

[英]Adding subviews to headerview: added to a wrong section

I have a tableview with 2 sections 我有2个部分的表格视图

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
  switch (section) { 
    case 0: return @"s1"; 
    case 1: return @"s2"; 
  } 
}

I wanted to add a label to section 0. 我想在第0部分添加标签。

-(void) tableView:(UITableView *)tableView willDisplayHeaderView(nonnull UIView *)view forSection:(NSInteger)section {
  if (section == 0) {
    for (UIView *subView in view.subviews) {
      if (subView.tag == 99) {
         [subView removeFromSuperview];
         break;
      }
    }

    UITableViewHeaderFooterView *headerView = (UITableViewHeaderFooterView *)view;
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake([[UIScreen mainScreen] bounds].size.width - 150 - headerView.textLabel.frame.origin.x, headerView.textLabel.frame.origin.y, 150, hederView.textLabel.frame.size.height)];

    label.text = @"test";
    label.tag = 99;
    [headerView addSubview:label];
  }
}

It works ok for initial display. 初始显示正常。

Then I use presentViewController to pop up a camera Image Picker, but whenever I dismiss the image picker, and return back to the current view, the "test" label will be in both seciton 0 and section 1. 然后,我使用presentViewController弹出相机图像选择器,但是每当我关闭图像选择器并返回到当前视图时,“测试”标签将同时显示在第0部分和第1部分中。

Meaning that there will be 2 "test" in both two sections. 这意味着在两个部分中都将有2个“测试”。

If I use 如果我用

if (section == 1) {
  for (UIView *subView in view.subviews) {
    if (subView.tag == 99) {
      [subView removeFromSuperview];
      break;
    }
  }
}

This will remove the extra "test" from section 1. 这将从第1节中删除多余的“测试”。

Is there any possible reasons for that? 有什么可能的原因吗? I tried to debug and cannot see anything useful on why this happens, because after the if (section == 0) block finished running, it will go into assembly mode, if I continue running from the break, section 1 will show up "test", and I don't know where it comes from. 我尝试调试,但是看不到任何有用的信息,这是因为如果if(section == 0)块运行完之后,它将进入汇编模式,如果我从中断处继续运行,则第1部分将显示“ test ”,但我不知道它的来源。

I am not asking for solution (I had one, just to remove the tag 99 label from section 1), I am asking for reason and any place that I did wrong. 我不是在寻求解决方案(我有一个方案,只是从第1部分中删除标签99标签),我是在寻求原因以及我做错的任何地方。

Any suggestions will be appreciated. 任何建议将不胜感激。 Thank you very much. 非常感谢你。

The first time you instantiate the UITableViewController class, the TableView is reloaded with two different views for both your sections. 你第一次instantiateUITableViewController类中, TableView重新加载为您的部分两种不同的观点。 When you return to the same UITableViewController a new instance is not instantiated. 当您返回相同的UITableViewController不会实例化新实例。 But probably you have a reloadData() call in viewWillAppear or viewDidAppear . 但是可能您在viewWillAppearviewDidAppear有一个reloadData()调用。 Now the TableView is reloaded with the previous views reused for your Headers . 现在,将TableView重新加载,并将先前的views重用于Headers The view1 for section 1 in previous instantiation becomes the view for section 0 now.(at this point you add the UILabel as subView for this view1) The view0 of previous instantiation becomes the view for section 1 now.(it already has label as subView from previous instantiation). 在先前的实例化部1厂景成为部分0,现在的看法。(在这一点上,你添加UILabel作为subView这个厂景)以前实例的view0成为目前第1节的看法。(它已经有标签的subView来自先前的实例化)。 Thus when you return to this same TableViewController and since it is not instantiated again, both the views for header has label as subView 因此,当您返回到同一TableViewController且由于没有再次实例化它时,标头的两个视图都将标签标记为subView

You can avoid the reloadData() call from view will appear and it looks fine or follow the best practice of overriding the method 您可以避免在视图中出现reloadData()调用,并且看起来不错,或者遵循overriding该方法的最佳实践

func tableView(tableView: UITableView, viewForHeaderInSection section: Int) ->     UIView?

You can add header in following way.. 您可以按照以下方式添加标题。

add two function 增加两个功能

func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { func tableView(tableView:UITableView,heightForHeaderInSection部分:Int)-> CGFloat {

            return 40.0
    }



func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let vw_header : UIView = UIView()
    vw_header.frame = CGRectMake(10, 0, ScreenSize.SCREEN_WIDTH, 40)
    vw_header.backgroundColor = Validation.getColorByRGB(227, G: 227, B: 227)
    vw_header.layer.borderWidth = 1
    vw_header.layer.borderColor = UIColor.darkGrayColor().CGColor


    let txtHeaderText : UILabel = UILabel()
    txtHeaderText.frame = CGRectMake(5, 0, ScreenSize.SCREEN_WIDTH, 40)
    txtHeaderText.textColor = appTextPurpleColor
    txtHeaderText.textAlignment = NSTextAlignment.Left
    txtHeaderText.text = “Simple Header”

    vw_header.addSubview(txtHeaderText)

    return vw_header
}

Now u can customize the header section view according ur requirement 现在,您可以根据自己的要求自定义标题剖视图

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

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