UIScrollview的奇怪行为

时间:2012-10-31 12:14:22

标签: iphone objective-c ios xcode uiscrollview

我需要在UIScrollView上为此使用MKHorizion菜单(Scroll视图的子类)绘制动态图像。现在我在scrollview上添加Subview图像。 Loop非常适合在scrollview上添加子视图。现在在我的父类中,我需要触摸滚动视图以获取更新的数据。如果我没有触摸那么它没有显示更新的数据。下面是代码

-(void) reloadData
{
    [self deleteAlliTem];
    self.itemCount = [dataSource numberOfImagesForMenu:self];
    self.backgroundColor = [dataSource backgroundColorForImage:self];

    UIFont *buttonFont = [UIFont boldSystemFontOfSize:15];
    int buttonPadding = 0;

    int tag = kButtonBaseTag;    
    int xPos = kLeftOffset;

    for(int i = 0 ; i < self.itemCount; i ++)
    {
      NSLog(@"*************************************************************");
        NSMutableDictionary *dictData=[dataSource horizMenuImage:self dictForItmeAtIndex:i];

        NSString *title=[dictData valueForKey:@"name"] ? [dictData valueForKey:@"name"] :  @"";

        UIImage* imageItem= [UIImage imageWithData:[Base64 decode:[dictData valueForKey:@"img"]]];
        int buttonWidth = 95;

         UIButton *customButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [customButton setTitle:title forState:UIControlStateNormal];
        customButton.titleLabel.font = buttonFont;
        [customButton setBackgroundImage:[UIImage imageNamed:@"addFriendStrip.png"] forState:UIControlStateNormal];
        [customButton setBackgroundImage:[UIImage imageNamed:@"addFriendStrip.png"] forState:UIControlStateSelected];
         customButton.tag = tag++;
        [customButton addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
         customButton.frame = CGRectMake(xPos, 70, buttonWidth + buttonPadding, 25);
        customButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        customButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;

        customButton.titleEdgeInsets =UIEdgeInsetsMake(0, 10,0, 0);

        customButton.titleLabel.font = [UIFont fontWithName:@"Overlock-Bold" size:16];
        [customButton setTitleColor:[UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:1] forState:UIControlStateNormal];
        [customButton setTitleColor:[UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:1] forState:UIControlStateSelected];

         UIImageView* itemImageView = [[UIImageView alloc] initWithImage:imageItem];
        itemImageView.tag = 200 + customButton.tag;
        [itemImageView setFrame:CGRectMake(xPos, 0, buttonWidth + buttonPadding, 95)];
        [self addSubview:itemImageView];
        [itemImageView release];
        itemImageView = nil;
        [self addSubview:customButton];

        xPos += buttonWidth;
         xPos += buttonPadding;
        if (i != self.itemCount-1){
            xPos += 2.5; //5;  // Richa
        }
    }
    self.contentSize = CGSizeMake(xPos, 95);
     NSLog(@"############################################################################ - %d",[[self subviews] count]);
    [self scrollRectToVisible:CGRectMake(1, 0, self.frame.size.width, self.frame.size.height) animated:YES];
    }

请帮我解决这个问题。为什么我需要触摸滚动视图?我需要覆盖其他方法吗?

2 个答案:

答案 0 :(得分:1)

你检查它是在主线程上吗?可能是你正在使用GCD队列,这就是为什么在触摸之前它不会更新。

答案 1 :(得分:0)

只是尝试将您的滚动视图放在首位。我没有正确地回答您的问题,但它可能有所帮助。 让我知道它是否正常工作.. !!!

快乐编码。!!!!