嗨,我想根據從url獲取的圖像設置UICollectionView單元格的高度。誰能幫我?我已經檢查了一些圖書館,他們給了細胞隨機高度。 In this one給予細胞的隨機高度。當我嘗試給圖像高度like this它給予result。空間來自細胞之間。UICollectionview基於圖像高度設置單元格高度
並且還嘗試like this但單元行之間沒有工作空間。 see this
嗨,我想根據從url獲取的圖像設置UICollectionView單元格的高度。誰能幫我?我已經檢查了一些圖書館,他們給了細胞隨機高度。 In this one給予細胞的隨機高度。當我嘗試給圖像高度like this它給予result。空間來自細胞之間。UICollectionview基於圖像高度設置單元格高度
並且還嘗試like this但單元行之間沒有工作空間。 see this
Write in ViewDidLoad Method
for (int i=0; i<[imageURLArray count]; i++)
{
//Declare Globally
IMGArray1=[[NSMutableArray alloc] init];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:[imageURLArray objectAtIndex:i]]];
NSOperationQueue *urlQue = [[NSOperationQueue alloc] init];
[NSURLConnection sendAsynchronousRequest:urlRequest queue:urlQue completionHandler:^(NSURLResponse *response,
NSData *MoblieDatadata, NSError *error)
{
if ([MoblieDatadata length] >0 && error == nil)
{
UIImage *image = [[UIImage alloc]initWithData:MoblieDatadata];
[IMGArray1 addObject:image];
}
else if ([MoblieDatadata length] == 0 && error == nil)
{
NSLog(@"Nothing was downloaded.");
}
else if (error != nil)
{
NSLog(@"Error happened = %@", error);
dispatch_async(dispatch_get_main_queue(), ^{
});
}
}];
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
examplemycell *myCell = [collectionView
dequeueReusableCellWithReuseIdentifier:@"cell"
forIndexPath:indexPath];
UIImage *image;
int row = [indexPath row];
image = [UIImage imageNamed:IMGArray1[row]];
myCell.imageView.image = image;
return myCell;
}
#pragma mark -
#pragma mark UICollectionViewFlowLayoutDelegate
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
UIImage *image;
int row = [indexPath row];
image = [UIImage imageNamed:IMGArray1[row]];
return image.size;
}
//For setting spacing between collectionview cells
//use the delegate method like
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionView *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
return 10; // This is the minimum inter item spacing, can be more
}
And Use this for verticle line spacing between cells
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
return 5;
}
可以設置高度可變
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
return CGSize(width: 100, height: myHeight)
}
}
如果你的鏡像下載,請致電: MyCollectionView.collectionViewLayout.invalidateLayout()
的問題是老了,但還是實際
您已經嘗試這是因爲它不起作用給我錯誤NSInvalidArgumentException',原因:' - [UIImage length]:無法識別的選擇器發送t o實例0xa781820 – Nisha 2014-09-11 12:47:48
嗯,我沒有用過這個,請給我time.Or你可以從這個想法:) – 2014-09-11 12:51:29
我有問題說我已經試過這個,它不工作 – Nisha 2014-09-11 12:53:22