0

我在 in 中创建自定义视图时遇到scrollview问题iOS。屏幕上只添加了一张图像,这是最后一张地图,并且只添加了一个标题。其余的不出现。我需要有 4 个图像和 4 个文本。

我的自定义类称为mapview.

我的自定义视图应如下所示:

在此处输入图像描述

这是在ViewController.m中设置滚动视图的方式

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

地图视图.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;

地图视图.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 并为您的滚动视图设置新的 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 的框架设置有问题

尝试使用此代码...

视图控制器.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)];
}

地图视图.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;

地图视图.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 回答