UIScrollView中的UIButtons在3行后停止工作

时间:2013-06-10 13:02:44

标签: objective-c uiscrollview

我有一个UIScrollView,里面有UIViews列表。有点像花哨的tableview。我遇到的一个问题是滚动视图中的前3个'行的按钮工作正常,但是当我向下滚动时,没有任何按钮响应。看起来它对于视图加载时在屏幕上显示的按钮工作正常,但是当我滚动时,任何进一步向下的按钮现在都会响应......

在uiscrollview中重复的uiview中的

代码

    -(void)addButtons
    {
        UIButton *visiteWebSite = [UIButton buttonWithType:UIButtonTypeCustom];
        [visiteWebSite addTarget:self
                          action:@selector(visitSite:)
         forControlEvents:UIControlEventTouchDown];
        [visiteWebSite setTintColor:[UIColor colorWithRed:247 green:143 blue:30 alpha:1.0]];

        visiteWebSite.frame = CGRectMake(440.0, 10.0, 120.0, 26.0);
        if(![self IsPhone5]) {
            visiteWebSite.frame = CGRectMake(350.0, 10.0, 120.0, 26.0);
        }
        //visiteWebSite.backgroundColor = [UIColor orangeColor]; //[UIColor colorWithRed:247 green:143 blue:30 alpha:1.0];
        [visiteWebSite setBackgroundImage:[UIImage imageNamed:@"orangeBG"] forState:UIControlStateNormal];
        [visiteWebSite setTitle:@"VISITE WEBSITE" forState:UIControlStateNormal];
        visiteWebSite.titleLabel.font = [UIFont fontWithName:@"arial" size:12];

        [self addSubview:visiteWebSite];

        UIButton *getDirections = [UIButton buttonWithType:UIButtonTypeCustom];
        [getDirections addTarget:self
                          action:@selector(getDirections:)
                forControlEvents:UIControlEventTouchDown];

        [getDirections setTitle:@"GET DIRECTIONS" forState:UIControlStateNormal];
        getDirections.titleLabel.font = [UIFont fontWithName:@"arial" size:12];
        getDirections.frame = CGRectMake(440.0, 46.0, 120.0, 26.0);
        if(![self IsPhone5]) {
            getDirections.frame = CGRectMake(350.0, 46.0, 120.0, 26.0);
        }
        [getDirections setBackgroundImage:[UIImage imageNamed:@"orangeBG"] forState:UIControlStateNormal];

        [self addSubview:getDirections];
    }

包含UIScrollView的父视图中的代码

-(void)performSearch
{
    [self.loadinglabel removeFromSuperview];
    NSString* searchTerm = txtSearch.text;
    searchTerm = [searchTerm stringByReplacingOccurrencesOfString:@" " withString:@""];

    NSData *urldata = [NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://www.collectioncosmetics.co.uk/storelocatorapi?store=%@",searchTerm]]];
    NSString *json = [[NSString alloc] initWithData:urldata encoding:NSUTF8StringEncoding];
    SBJsonParser *jsonParser = [[SBJsonParser alloc] init];
    NSArray *jsonObjects = [jsonParser objectWithString:json];
    float y = 0;
    float height = 84;
    if([jsonObjects count] < 1) {
        UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:@"NO RESULTS" message:@"There are no stores near the postcode you searched" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
        [alertView show];
        [self back:nil];
        return;
    }

    for (int i = 0; i < [jsonObjects count]; i++)
    {
        NSDictionary *dict = [jsonObjects objectAtIndex:i];
        CARStoreResult* result = [[CARStoreResult alloc] initWithFrame:CGRectMake(0, height*i, tv.frame.size.width, height)];
        result.name = [dict objectForKey:@"name"];
        result.street = [dict objectForKey:@"street"];
        result.area = [dict objectForKey:@"area"];
        result.county = [dict objectForKey:@"County"];
        result.postcode = [dict objectForKey:@"PostCode"];
        result.distance = [dict objectForKey:@"distance"];
        result.usersPostCode = searchTerm;
        result.y = y;
        result.num = i;
        y = y + height;
        [result build];
        [tv addSubview:result];
    }
    [tv setFrame:CGRectMake(tv.frame.origin.x, tv.frame.origin.y, tv.frame.size.width, height*[jsonObjects count])];
    [self.scrollView setContentSize:tv.frame.size];

    [Flurry logEvent:@"Store Locator"];
}

1 个答案:

答案 0 :(得分:0)

<强>固定! 而不是将视图添加到scrollview中的视图,我将它们直接添加到scrollview。使用scrollView.canCancelContentTouches设置为NO的滚动视图。

    //[tv addSubview:result];
    [self.scrollView addSubview:result];
}
//[tv setFrame:CGRectMake(tv.frame.origin.x, tv.frame.origin.y, tv.frame.size.width, height*[jsonObjects count])];
[self.scrollView setContentSize:CGSizeMake(320, height*[jsonObjects count])];
//[self.scrollView setContentSize:tv.frame.size];

[Flurry logEvent:@"Store Locator"];