UICollectionViewCell will not find Custom Cell to load image - xcode

I am using a UICollectionView with a custom Cell containing one image and one label. I load the data via XML, but I am using a multithread to bind the data of the image in the cells. I am not setting the image to load on the cellForItemAtIndexPath, but I am loading the description from here.
My problem is that the images do not load initially. When I scroll, it does load because I have a method in scrollView:
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
if (!decelerate)
{
[self loadImagesForOnscreenCells];
}}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
[self loadImagesForOnscreenCells];
}
I use the following to load the description:
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath; {
Cell *cell = [cv dequeueReusableCellWithReuseIdentifier:kCellID forIndexPath:indexPath];
cell.label.text = xmlPhotosVideosRecordPointer.title;
This is the loadImagesForOnscreenCells:
- (void)loadImagesForOnscreenCells
{
if ([xmlPhotosVideosRecords count] > 0)
{
NSArray *visiblePaths = [newsTable indexPathsForVisibleItems];
for (NSIndexPath *indexPath in visiblePaths)
{
xmlPhotosVideosRecord *xmlPhotosVideosRecordPointer = [self.xmlPhotosVideosRecords objectAtIndex:indexPath.row];
if (!xmlPhotosVideosRecordPointer.thumbImage)
{
[self startIconDownload:xmlPhotosVideosRecordPointer forIndexPath:indexPath];
}
}
}
}
That being said, is there a way to delegate when the UICollectionView didLoad? This way, I could use
[self loadImagesForOnscreenCells]; to refresh the images on screen.

I figured it out and it works! Use:
- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath

Related

UicollectionView on scroll shows duplicate images

I was using UICollectionView for Images grid appearance.while scrolling i found duplicate images appearing there. I was using NYXProgressiveImageView for downloading and displaying images. I Have implemented several methods found on search..
This is my code
- (MyCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
MyCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"CELL" forIndexPath:indexPath];
if (cell == nil)
{
cell = [[MyCell alloc]init];
[cell.imgv setImage:nil];
}
NSMutableDictionary *d=[[NSMutableDictionary alloc]initWithDictionary:[arrSubCatAndItems objectAtIndex:indexPath.item]];
[cell.imgv setImage:nil];
cell.imgv.cakeIndex=indexPath.item;
cell.cellLabel.text = [d objectForKey:#"Name"];
return cell;
}
Where as Mycell is Custom UICollectionViewCell class having ImageView and label init
On scrolling iam loading visible images like this
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
_isDragging=NO;
if (!decelerate)
{
_isDragging=YES;
[self loadimages];
}
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
CGPoint contentOffset = testscroll.contentOffset;
CGRect contentFrame = testscroll.bounds;
contentFrame.origin = contentOffset;
if(!_isDragging)
[self loadimages];
}
-(void)loadimages
{
for (UICollectionViewCell *cell in [collectionview visibleCells]) {
NSIndexPath *indexPath = [collectionview indexPathForCell:cell];
MyCell *cell1 = (MyCell*)[collectionview cellForItemAtIndexPath:indexPath];
NSMutableDictionary *d=[[NSMutableDictionary alloc]initWithDictionary:[arrSubCatAndItems objectAtIndex:indexPath.item]];
[cell1.imgv loadImageAtURL:[NSURL URLWithString:[[d objectForKey:#"ImageUrl"]stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];
}
}
Main problem if i scroll collection view on loading image in Imageview it is appearing in below cells
I don't Know where I am going wrong....New to UICollectionView...Please help me ..Tnx in advance

Animating a UICollectionViewCell: Scroll To And Resize At The Same Time

I'm trying to get a UICollectionViewCell to do an animated resize and at the same time have the collection view scroll the cell to the top of the collection view's viewport.
When I do
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
[self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionTop animated:YES];
[collectionView performBatchUpdates:nil completion:nil];
}
with
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [self.collectionView cellForItemAtIndexPath:indexPath];
if (cell.isSelected) {
return self.collectionView.frame.size;
}
else {
return CGSizeMake(300, 100);
}
}
the scroll happens before the resize animation.
And if I call scrollToItemAtIndexPath inside of performBatchUpdates like
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
[collectionView performBatchUpdates:^{
[self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionTop animated:YES];
} completion:nil];
}
it doesn't work most of the time.
Thanks!
-Eric
The best way to achieve an effect like this is to simply subclass UICollectionViewFlowLayout and tweak cell layout attributes based on the selected cell.

Copy UIImage to clipboard on tap

I've got a UICollectionView which displays as many cells as the amount of images passed through.
I want to make it so that when you tap an image, it copies that image to the clipboard.
Any help would be greatly appreciated.
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
CollectionViewCell *Cell = [collectionView
dequeueReusableCellWithReuseIdentifier:#"ImagesCell"
forIndexPath:indexPath];
UIImage *image;
int row = indexPath.row;
//set image of this cell
image = [UIImage imageNamed:localImages.imageFiles[row]];
Cell.imageCell.image = image;
//enable touch
[Cell.imageCell setUserInteractionEnabled:YES];
Cell.imageCell.tag = row;
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapGestureRecognizer:)];
[tapGesture setNumberOfTapsRequired:1];
[Cell.imageCell addGestureRecognizer:tapGesture];
return Cell;
}
- (void)tapGestureRecognizer:(UITapGestureRecognizer *)sender
{
NSInteger string = (sender.view.tag);
NSLog(#"this is image %i",string);
UIImage *copiedImage = I don't know what to put here..
[[UIPasteboard generalPasteboard] setImage:copiedImage];
}
It's actually easier if you just implement the delegate method of UICollectionView - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath and just do this:
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
CollectionViewCell *cell = (CollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
UIImage *copiedImage = cell.imageCell.image;
[[UIPasteboard generalPasteboard] setImage:copiedImage];
}
Hope this helped you out!
Edit: In case you have different type of cells with images and text and you only want to enable copying under images, you will have to implement that UITapGestureRecognizer part and in the method called you just need to do this:
- (void)tapGestureRecognizer:(UITapGestureRecognizer *)sender
{
UIImageView *imageView = (UIImageView *)sender.view;
UIImage *copiedImage = imageView.image;
[[UIPasteboard generalPasteboard] setImage:copiedImage];
}
Since your UITapGestureRecognizer is attached to the UIImageView, it is the sending view and you can extract the image directly from it and copy to the pasteboard.

Table view not working smoothly with SDWebImage

Why isn’t my table view not working smoothly? I use SDWebImage to set the image from a URL. I'm trying to draw 3 images in each row of the table view.
It works fine when arrList is small, but when I increase arrList, it becomes slower and slower (look like a little vibration when I scroll the table view).
This is my code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"CustomCell";
CustomCellTable *cell = (CustomCellTable*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray * nib = [[NSBundle mainBundle] loadNibNamed:#"CustomCellTable" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
[cell setBackgroundColor:brgColor];
// item 1
if ((indexPath.row*3) > arrList.count-1) return cell;
vod *Item = [arrList objectAtIndex:(indexPath.row*3)];
[cell.image1 setHidden:NO];
[cell.image1 setImageWithURL:[NSURL URLWithString:Item.imageURl] placeholderImage:[UIImage imageNamed:#"poster_default.png"]];
[cell makeRating:[Item.rating floatValue] positionOf:14];
// item 2
if ((indexPath.row*3 +1) > arrList.count-1) return cell;
Item = [arrList objectAtIndex:(indexPath.row*3+1)];
[cell.image2 setHidden:NO];
[cell.image2 setImageWithURL:[NSURL URLWithString:Item.imageURl] placeholderImage:[UIImage imageNamed:#"poster_default.png"]];
[cell makeRating:[Item.rating floatValue] positionOf:118];
// item 3
if ((indexPath.row*3 +2) > arrList.count-1) return cell;
Item = [arrList objectAtIndex:(indexPath.row*3+2)];
[cell.image3 setHidden:NO];
[cell.image3 setImageWithURL:[NSURL URLWithString:Item.imageURl] placeholderImage:[UIImage imageNamed:#"poster_default.png"]];
[cell makeRating:[Item.rating floatValue] positionOf:225];
return cell;
}
Look into down sampling the images so you're not storing/loading the large versions of the images while scrolling.
https://github.com/toptierlabs/ImageCacheResize
This library combines SDWebImage with UIImage+Resize. Using it instead of just SDWebImage might help. Specifically use the following method to load the images:
- (void)setImageWithURL:(NSURL *)url andResize:(CGSize)size withContentMode:(UIViewContentMode)mode;

Using segues to display a UIView from UITableViewController

I am trying to connect a UITableViewController to a UIView using storyboards. I have a TabBarController that connects to a Navigation Controller that displays a UITableViewController, displaying a small array. I want to click on a row and that trigger a simple UIView with a label, still displayed in the UINavigationController so I can navigate back to the UITableViewController
I can get the UITableViewController to display OK, with the array, but I can't get the row to trigger when I select it.
This is my UITableViewController (AtoZController.m) file:
#import "AtoZController.h"
#import "StoreDetailsView.h"
#interface AtoZController ()
#end
#implementation AtoZController
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = NSLocalizedString(#"A to Z", #"An A to Z List of Stores");
AtoZArray = [[NSMutableArray alloc] init];
[AtoZArray addObject:#"Apple"];
[AtoZArray addObject:#"Boots"];
[AtoZArray addObject:#"Topman"];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
} else {
return YES;
}
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [AtoZArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell...
NSInteger row = [indexPath row];
cell.textLabel.text = [AtoZArray objectAtIndex:row];
return cell;
}
I have a segue connected, named storeDetailsSegue, from the UITableViewController to a UIView. I am attempting to use the prepareForSeque method to trigger the next view but have had no luck with the following code.
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
NSIndexPath *path = [self.tableView indexPathForSelectedRow];
StoreDetailsView *detail = [self detailForIndexPath:path];
[segue.destinationViewController setDetail:detail];
}
As it keeps saying there is an error as there is 'No visible #interface for "AtoZController declares the selector 'detailForIndexPath'". I may be doing something completely wrong as I have never used storyboards before. What I want is for a simple UIView to display a label that dynamically changes to display the row number that has been selected.
I don't know if the UITableViewController should infact be a UITableView but as I have never used segues or storyboards I have no idea what could be causing the problem.
Any advice at all would be greatly appreciated.
UPDATE:
So I managed to get the UIViewController to display but still can't get the label to update before I display the view.

Resources