UIScrollview läd erst nach zweiten Aufruf

Diese Seite verwendet Cookies. Durch die Nutzung unserer Seite erklären Sie sich damit einverstanden, dass wir Cookies setzen. Weitere Informationen

  • UIScrollview läd erst nach zweiten Aufruf

    Hallo,
    und noch ein Problem quält mich.
    Ich habe ein View, dass sich in ein ScrollView und ein TableView aufteilt. Das Scrollview will aber nicht so recht. Das Scrollview enthält das titleCV, (ein normales View mit dynamischen Button).
    Ich habe darauf geachtet, vom Scrollview sowohl Contentsize als auch Frame zu setzen. Das Scrollview wird ja angezeigt, aber erst wenn die Methode buildListOfChannels das zweite Mal aufgerufen wird. Nach dem ersten Aufruf sehe ich nur ein weißes Feld.
    Habe ich eine andere Eigenart übersehen?

    Quellcode

    1. - (void)viewDidLoad
    2. {
    3. //... tab view etc..
    4. [self buildListOfChannels];
    5. }



    Quellcode

    1. -(void)buildListOfChannels
    2. {
    3. //remove views from titleCV
    4. for(UIView *subview in [self.titleCV subviews]) {
    5. [subview removeFromSuperview];
    6. }
    7. [titleCV removeFromSuperview];
    8. NSLog(@"buildListOfChannels");
    9. //button config
    10. //set Contentsize
    11. CGFloat scrollViewWidth = 0.0f;
    12. CGFloat startX = 20.0f;
    13. if (self.model.allChannelList!=nil && [self.model countOfAllChannelList]>0)
    14. {
    15. for (UIButton* view in self.titleCV.subviews)
    16. {
    17. if (!view.hidden)
    18. {
    19. CGFloat x = startX;
    20. CGFloat y = 6;
    21. CGFloat h = 37.0f;
    22. CGFloat w = ([view.titleLabel.text length])*10.0;
    23. if(w<40) w = 40;
    24. view.frame = CGRectMake(startX, y, w, h);
    25. if (x + w > scrollViewWidth) scrollViewWidth = x + w;
    26. startX = startX + w + 10.0f;
    27. }
    28. }
    29. }else scrollViewWidth=300;
    30. self.titleCV.frame = CGRectMake(0, 0, scrollViewWidth+20, 50);
    31. if (scrollViewWidth >= 320) [self.scrollview setContentSize:(CGSizeMake(scrollViewWidth+20.0f, 50))];
    32. else [self.scrollview setContentSize:(CGSizeMake(320, 50))];
    33. self.scrollview.frame = CGRectMake(0, 0, 320, 50);
    34. //add titleCV to scrollview
    35. [self.scrollview addSubview:self.titleCV];
    36. [self.scrollview setScrollEnabled:YES];
    37. }
    Alles anzeigen


    Vielen Dank für die Hilfe .-),
    Juji
  • Und dann?
    Falls in self.model nichts drin ist, dann setzte ich in zeil 39 }else scrollViewWidth=300; die breite der views auf jedenfall auf die maximal bildschirmbreite. ich hatte es auch alternativ mit einem label versucht, dass ich in das titelCV und dann Scrollview einfügte. das gleiche problem..
  • Ich verstehe den Code nicht. Erst removest Du alles subviews des titleCV und dann machst du eine Schleife über die subviews von titleCV obwohl da ja gar nichts drin ist... Die for schleife wird damit nie benutzt.

    Gruß

    Claus
    2 Stunden Try & Error erspart 10 Minuten Handbuchlesen.

    Pre-Kaffee-Posts sind mit Vorsicht zu geniessen :)
  • Da habe ich den Code wohl etwas zu stark gekürzt? Natürlich packe ich da Buttons rein...

    Quellcode

    1. //remove views from titleCV
    2. for(UIView *subview in [self.titleCV subviews]) {
    3. [subview removeFromSuperview];
    4. }
    5. [titleCV removeFromSuperview];
    6. NSLog(@"buildListOfChannels");
    7. if (self.model.allChannelList!=nil && [self.model countOfAllChannelList]>0)
    8. {
    9. Channel* current;
    10. for (int i=0; i<[self.model countOfAllChannelList]; i++)
    11. {
    12. current = [self.model.allChannelList objectAtIndex:i];
    13. UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
    14. button.tag =i+10;
    15. button.titleLabel.font =[UIFont boldSystemFontOfSize:14];
    16. [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    17. [button setTitle:[NSString stringWithFormat:@"%@", current.name] forState:UIControlStateNormal];
    18. [button setTitleShadowColor:[UIColor whiteColor] forState:UIControlStateNormal ];
    19. [button addTarget:self action:@selector(switchPage:) forControlEvents:UIControlEventTouchUpInside];
    20. CALayer *layer = button.layer;
    21. layer.cornerRadius = 8.0f;
    22. layer.masksToBounds = YES;
    23. layer.borderWidth = 1.0f;
    24. layer.borderColor = [UIColor colorWithWhite:0.5f alpha:0.2f].CGColor;
    25. [self.titleCV addSubview:button];
    26. }
    27. current = nil;
    28. }
    29. //set Contentsize
    30. CGFloat scrollViewWidth = 0.0f;
    31. CGFloat startX = 20.0f;
    32. if (self.model.allChannelList!=nil && [self.model countOfAllChannelList]>0)
    33. {
    34. for (UIButton* view in self.titleCV.subviews)
    35. {
    36. if (!view.hidden)
    37. {
    38. CGFloat x = startX;
    39. CGFloat y = 6;
    40. CGFloat h = 37.0f;
    41. CGFloat w = ([view.titleLabel.text length])*10.0;
    42. if(w<40) w = 40;
    43. view.frame = CGRectMake(startX, y, w, h);
    44. if (x + w > scrollViewWidth) scrollViewWidth = x + w;
    45. startX = startX + w + 10.0f;
    46. }
    47. }
    48. }else scrollViewWidth=300;
    49. self.titleCV.frame = CGRectMake(0, 0, scrollViewWidth+20, 50);
    50. if (scrollViewWidth >= 320) [self.scrollview setContentSize:(CGSizeMake(scrollViewWidth+20.0f, 50))];
    51. else [self.scrollview setContentSize:(CGSizeMake(320, 50))];
    52. self.scrollview.frame = CGRectMake(0, 0, 320, 50);
    53. [self.scrollview addSubview:self.titleCV];
    54. [self.scrollview setScrollEnabled:YES];
    55. }
    Alles anzeigen


    Zuerst entferne ich alles was im titelCV sein könnte. Dinge wie titleVC RemoveFromSuperView sind meine verzweifelten Versuche, da mal was anzuzeigen ;) Dann füge ich die Buttons hinzu. Und setzte die COntentsize, etc. Wie gesagt. Das geht alles soweit. Aber erst, wenn ich die Methode zum zweiten Mal aufrufe... ich hab da schon diverse Dinge mit Neuladen etc versucht. Aber finde nicht die wahre Lösung...