0

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

uiview 中的代码在 uiscrollview 中重复

    -(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"];
}
4

1 回答 1

0

固定的! 我没有将视图添加到滚动视图中的视图,而是直接将它们添加到滚动视图中。滚动视图的 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"];
于 2013-06-10T13:34:01.303 回答