ios - UICollectionview 根据图像高度设置单元格高度
<p><p>嗨,我想根据从 url 获得的图像设置 UICollectionView 单元格的高度。谁能帮我?我检查了一些库,因为它们为单元格提供了随机高度。 <a href="https://github.com/chiahsien/CHTCollectionViewWaterfallLayout" rel="noreferrer noopener nofollow">In this one</a>给单元格的随机高度。当我尝试给图像高度 <a href="http://screencast.com/t/VSeeLtEUf" rel="noreferrer noopener nofollow">like this</a>它给了<a href="http://screencast.com/t/thJfGTsFtxH" rel="noreferrer noopener nofollow">result</a> .单元格之间出现空间。</p>
<p>也试过<a href="https://stackoverflow.com/questions/21525034/dynamically-change-cell-size-according-to-image-in-a-uicollectionview" rel="noreferrer noopener nofollow">like this</a>但单元格行之间没有工作空间。 <a href="http://screencast.com/t/thJfGTsFtxH" rel="noreferrer noopener nofollow">see this</a> </p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><pre><code> Write in ViewDidLoad Method
for (int i=0; i<; i++)
{
//Declare Globally
IMGArray1=[ init];
NSURLRequest *urlRequest = ]];
NSOperationQueue *urlQue = [ init];
[NSURLConnection sendAsynchronousRequest:urlRequest queue:urlQue completionHandler:^(NSURLResponse *response,
NSData *MoblieDatadata, NSError *error)
{
if ( >0 && error == nil)
{
UIImage *image = [initWithData:MoblieDatadata];
;
}
else if ( == 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 = ;
image = ];
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 = ;
image = ];
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;
}
</code></pre></p>
<p style="font-size: 20px;">关于ios - UICollectionview 根据图像高度设置单元格高度,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/25786002/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/25786002/
</a>
</p>
页:
[1]