2015-02-04 2 views
0

나는 동적 인 사진 배열을 가지고 있습니다. 이것을 가로 UIScrollView에 넣으려고하면 사진 순서가 손실됩니다. UIScrollView는 UITableViewCell에 있습니다. 코드가 함수 아래에서 실행 중입니다.일련의 사진이 혼합되어 있습니다.

(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 

for (int i = 0; i < [self.attachments count]; i++) 
{  [newArr setDictionary:[self.attachments objectAtIndex:i]]; 

    if ([[newArr objectForKey:@"type"] isEqualToString:@"video"]) { 

     [typeVideo setDictionary:[newArr objectForKey:@"video"]]; 
     self.photoID   = [NSString stringWithFormat:@"%@", [typePhoto objectForKey:@"id"]]; 

     self.urlPhotoString = [NSString stringWithFormat:@"%@", [typeVideo objectForKey:@"photo_75"]]; 

    } else { 

     [typePhoto setDictionary:[newArr objectForKey:@"photo"]]; 
     self.urlPhotoString = [NSString stringWithFormat:@"%@", [typePhoto objectForKey:@"photo_75"]]; 
     self.photoID   = [NSString stringWithFormat:@"%@", [typePhoto objectForKey:@"id"]]; 

    } 

// self.imageView=[[UIImageView alloc]initWithFrame:CGRectMake(scrollWidth,0,50,40)]; 

// Get image from URL this code works cool but frozen 
    // NSURL *url = [NSURL URLWithString:self.urlPhotoString]; 
//  NSData *data = [NSData dataWithContentsOfURL:url]; 
//  self.img = [[UIImage alloc] initWithData:data]; 

    self.btn =[[UIButton alloc]initWithFrame:CGRectMake(scrollWidth,0,50,40)]; 

    [SDWebImageDownloader.sharedDownloader downloadImageWithURL:[NSURL URLWithString:self.urlPhotoString] 
                 options:0 
                 progress:^(NSInteger receivedSize, NSInteger expectedSize) 
    { 
     NSLog(@"donwloading..."); 
    } 
                 completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) 
    { 
     if (image && finished) 
     { 
      self.img = image; 

     } 
    }]; 

    scrollWidth=scrollWidth+80; 

//  self.imageView.image= self.img; 
//  self.imageView.backgroundColor = [UIColor blackColor]; 
//  self.imageView.contentMode = UIViewContentModeScaleAspectFit; 
     NSNumberFormatter * f = [[NSNumberFormatter alloc] init]; 
     [f setNumberStyle:NSNumberFormatterDecimalStyle]; 
     NSNumber * myNumber = [f numberFromString:self.photoID]; 

    [self.btn setTag: [myNumber longLongValue]]; 

    NSString* postID = [NSString stringWithFormat:@"%@#%d", [[self.items objectAtIndex:indexPath.row] objectForKey:@"id"], i]; 

    self.btn.titleLabel.text = postID; 
    self.btn.titleLabel.hidden=YES; 

    [self.btn setBackgroundImage:self.img forState:UIControlStateNormal]; 
    [self.gallery addSubview: self.btn]; 

    [self.btn addTarget:self action:@selector(nextPhoto:) forControlEvents:UIControlEventTouchUpInside]; 

} 

[self.gallery setContentSize:CGSizeMake(scrollWidth, 30)]; 

[postCell.contentView addSubview: self.gallery]; 

사진을 동 기적으로 얻으려고 할 때 필요한 항목을 얻었지만 정지되었습니다.

답변

1

뷰를 가로로 스크롤하려면 UICollectionView를 사용하는 것이 좋습니다. https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UICollectionView_class/index.html

+0

이 경우에는 UICollectionView를 사용해야하지만 UICollection에는 문제가 있습니다. 나는 초급이다. TableViewCell에서 UICollection으로 작업하는 방법을 이해할 수 없습니다. –

+0

이미지가 많으면 메모리에 악몽이됩니다. CollectionView는 TableView와 매우 유사하게 동작합니다. UICollectionView를 사용하는 가장 빠른 방법 http://www.raywenderlich.com/22324/beginning-uicollectionview-in-ios-6-part-12 –

+0

ty. 나는 UICollectionView에 문제가있다. numberOfItemsInSection을 동적으로 변경하는 방법? 예를 들어 self.attachments.count가 있지만 cellForRowAtIndexPath에 첨부 파일이 있습니다. 이 코드를 작성할 때 정적 itemsInSection이 있습니다. return [self.attachments count]; –

관련 문제