2013-07-14 73 views
0

如何更改此方法以允許圖像通過數組而不是顏色進入?因爲我有mySQL數據庫,它傳遞的是在xcode中分配爲存儲在數組中的NSString的url。更改UIScrollview方法以允許使用圖像加載視圖

想法是每個視圖都從mySQL數據庫設置了背景圖像。

- (void)viewDidLoad 
    { 
     [super viewDidLoad]; 

     NSArray *colors = [NSArray arrayWithObjects:[UIColor redColor], [UIColor blueColor], 
           [UIColor greenColor],[UIColor yellowColor] , nil]; 

      for (int i = 0; i < colors.count; i++) { 

       CGRect frame; 
       frame.origin.x = self.scrollView.frame.size.width * i; 
       frame.size = self.scrollView.frame.size; 

       self.scrollView.pagingEnabled = YES; 

       UIView *subview = [[UIView alloc] initWithFrame:frame]; 
       subview.backgroundColor = [colors objectAtIndex:i]; 

       [self.scrollView addSubview:subview]; 
      } 

      self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * colors.count, self.scrollView.frame.size.height); 

    } 

我做了一個嘗試(introNames是階級其中id和圖像的名稱是從MySQL數據庫舉行,imageArray在上面的代碼中類創建和introNames NSString的被分配給它)。

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    for (int i = 0; i < imageArray.count; i++) { 

     introImages *snap = [imageArray objectAtIndex:i]; 

     imageName2 = snap.imageName; 

     CGRect frame; 
     frame.origin.x = self.scrollView.frame.size.width * i; 
     frame.size = self.scrollView.frame.size; 

     self.scrollView.pagingEnabled = YES; 

     UIView *subview = [[UIView alloc] initWithFrame:frame]; 
     UIImageView *backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imageName2]]; 

     [subview addSubview:backgroundView]; 
     [self.scrollView addSubview:subview]; 
    } 

    self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * imageArray.count, self.scrollView.frame.size.height); 

} 

這是工作數據檢索系統。

-(void) retrieveData 
{ 
    NSURL *url = [NSURL URLWithString:getDataUrl]; 
    NSData *data = [NSData dataWithContentsOfURL:url]; 

    if (data) { 

     jsonConnection = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil]; 
     imageArray = [[NSMutableArray alloc]init]; 

     for (int i = 0; i < jsonConnection.count; i++) 
     { 
      NSString *pID = [[jsonConnection objectAtIndex:i] objectForKey:@"id"]; 
      NSString *pName = [[jsonConnection objectAtIndex:i] objectForKey:@"ImagesURL"]; 

      introImages *myImages = [[introImages alloc]initWithimageID:pID andimageName:pName]; 

      [imageArray addObject:myImages]; 
     } 

    } 

    else { 
     UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:@"Something is wrong with your internet connection" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
     [alert show]; 

    } 

任何想法將是巨大的:)

+0

我說使用文件名創建字符串,並只做NSArray * colors = [NSArray arrayWithObjects:[UIImage imageNamed:imageName1],[UIImage imageNamed:imageName2],nil];等... – user2277872

+0

正在考慮,但害怕沒有靈活性的權力,遠程更改圖像。 – Evilelement

+0

那麼當您嘗試使用IntroImages類時發生了什麼? – Zen

回答

相關問題