0

scrollview内にカスタムビューを作成する際に問題が発生していますiOS。画面に追加される画像は1つだけです。これは最後のマップであり、タイトルは1つだけ追加されます。残りは表示されません。4つの画像と4つのテキストが必要です。

私のカスタムクラスはと呼ばれmapviewます。

私のカスタムビューは次のようになります。

ここに画像の説明を入力してください

これは、ViewController.mでscrollviewが設定される方法です

-(void)initialiseScreen
{

    NSArray *MapGalleryArray = [[Singleton sharedManager] getMapGalleryItems];
    int yOffset = 0;
    for (int counter=0; counter < [MapGalleryArray count]; counter++)
    {
        NSString *imageName = [NSString stringWithFormat:@"%@.JPG",[[MapGalleryArray objectAtIndex:counter] objectForKey:@"Image"]];
        UIImage *MapImage = [UIImage imageNamed:imageName];

        NSString *titletext = [NSString stringWithFormat:@"%@",[[MapGalleryArray objectAtIndex:counter] objectForKey:@"Title"]];

        MapView *map = [[MapView alloc] initWithImage:MapImage label:titletext];
        [MapScrollView addSubview:map];

        yOffset +=MapScrollView.frame.size.height/MapGalleryArray count];
    }
    [MapScrollView setContentSize:CGSizeMake(MapScrollView.frame.size.width, yOffset)];
}

MapView.h

  @interface MapView : UIView
    {
        UIButton    *mapViewButton;
        UILabel     *titeLabel;
    }

    @property(nonatomic,retain)     UIButton        *mapViewButton;
    @property(nonatomic,retain)     UILabel         *titleLabel;

    -(id)initWithImage:(UIImage*)image label:(NSString*)text;

MapView.m

-(id)initWithImage:(UIImage*)image label:(NSString*)text
{
    self = [super init];
    if (self) {

        self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, image.size.width/2, image.size.height/2);

        mapViewButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
        [mapViewButton setImage:image forState:UIControlStateNormal];
        [mapViewButton setImage:image forState:UIControlStateHighlighted];
        [mapViewButton addTarget:self action:@selector(ImageButtonClicked:) forControlEvents:UIControlEventTouchUpInside];

        titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, mapViewButton.frame.size.width+5, mapViewButton.frame.size.height)];
        [titleLabel setText:text];

        [self addSubview:mapViewButton];
        [self addSubview:titleLabel];
    }
    return self;
}

私が間違いをしているところでは、これについていくつかのガイダンスが必要です。カスタムビューを作成するのはこれが初めてなので、申し訳ありません。提案へようこそ。

4

2 に答える 2

0

スクロール ビューの同じ位置に MapView を追加しているため、最後に追加されたビューのみを表示できます。MapView の x/y を増やし、ur scrollView の新しい contentSize を設定します。フォローしてみてください。

for (int counter=0; counter < [MapGalleryArray count]; counter++)
    {
        NSString *imageName = [NSString stringWithFormat:@"%@.JPG",[[MapGalleryArray objectAtIndex:counter] objectForKey:@"Image"]];
        UIImage *MapImage = [UIImage imageNamed:imageName];

        NSString *titletext = [NSString stringWithFormat:@"%@",[[MapGalleryArray objectAtIndex:counter] objectForKey:@"Title"]];

        //horizontal scroll view    
        MapView *map = [[MapView alloc] initWithImage:MapImage label:titletext];
        float width = map.frame.size.width;    
         [map setFrame:CGRectmake((width*counter)+5, //x ,
             map.frame.origin.y, //y
             map.frame.size.width, //width
            map.frame.size.height)]; //height

        [MapScrollView addSubview:map];

   MapScrollView.contentSize =CGSizeMake((width+5)*10,
                                          MapScrollView.frame.size.height
               );
        yOffset +=MapScrollView.frame.size.height;
    }
于 2013-03-22T11:02:51.933 に答える
0

あなたのコードには、MapView.m へのフレーム設定に問題があります。

このコードで試してみてください...

ViewController.m

-(void)initialiseScreen
{

    NSArray *MapGalleryArray = [[Singleton sharedManager] getMapGalleryItems];
    int yOffset = 0;
    int xOffset = 0;
    for (int counter=0; counter < [MapGalleryArray count]; counter++)
    {
        NSString *imageName = [NSString stringWithFormat:@"%@.JPG",[[MapGalleryArray objectAtIndex:counter] objectForKey:@"Image"]];
        UIImage *MapImage = [UIImage imageNamed:imageName];

        NSString *titletext = [NSString stringWithFormat:@"%@",[[MapGalleryArray objectAtIndex:counter] objectForKey:@"Title"]];
        CGPoint position = CGPointMake(xOffset, yOffset);
        MapView *map = [[MapView alloc] initWithImage:MapImage label:titletext andPoint:position];
        [MapScrollView addSubview:map];

        yOffset +=MapScrollView.frame.size.height/[MapGalleryArray count];
    }
    [MapScrollView setContentSize:CGSizeMake(MapScrollView.frame.size.width, yOffset)];
}

MapView.h

@interface MapView : UIView
    {
        UIButton    *mapViewButton;
        UILabel     *titeLabel;
    }

    @property(nonatomic,retain)     UIButton        *mapViewButton;
    @property(nonatomic,retain)     UILabel         *titleLabel;

    -(id)initWithImage:(UIImage*)image label:(NSString*)text andPoint:(CGPoint)point;

MapView.m

-(id)initWithImage:(UIImage*)image label:(NSString*)text andPoint:(CGPoint)point
{
    self = [super init];
    if (self) {

        self.frame = CGRectMake(point.x, point.y, image.size.width/2, image.size.height/2);

        mapViewButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
        [mapViewButton setImage:image forState:UIControlStateNormal];
        [mapViewButton setImage:image forState:UIControlStateHighlighted];
        [mapViewButton addTarget:self action:@selector(ImageButtonClicked:) forControlEvents:UIControlEventTouchUpInside];

        titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, mapViewButton.frame.size.width+5, mapViewButton.frame.size.height)];
        [titleLabel setText:text];

        [self addSubview:mapViewButton];
        [self addSubview:titleLabel];
    }
    return self;
}

これで問題が解決します....

于 2013-03-22T11:05:27.560 に答える