logo
down
shadow

UICollectionView CustomCell ReUse


UICollectionView CustomCell ReUse

By : Airwolf
Date : November 22 2020, 09:00 AM
I think the issue was by ths following , Reusable cells are reused by the collection view when they are scrolled outside the view to prevent a lot of memory use. When a certain cell gets scrolled outside of the view the collection view will mark them as reusable. Reusable cells will be reused by the collection view instead of creating new ones. This will significantly reduce memory use. Why would you keep 1000 cells in memory if only 20 of them fit on the screen at the same time.
because cells are reused, they will still contain the content of previous cell, this means that the cell still has the cameraButton as a subview. Same story with the images, you need to manually remove them at from the cell and make sure al old content gets replaced or removed.
code :
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! GridViewCell


    if indexPath.row == 0 {
        // Show the title becouse it's the first cell.
        cell.titleLabel.hidden = false

        // Title needs to be set in storyboard
        // Default image for camera roll needs to be set in storyboard

    } else {
        // All the other cells.
        cell.titleLabel.hidden = true

        // Clear old content
        cell.imageView.image = nil

        // I don't exactly know what all this code does behind the screen, but I assume it's a method that downloads the image and add it to the imageView when it's done.
        let asset = assetsFetchResults[indexPath.item - 1] as! PHAsset
        cell.representedAssetIdentifier = asset.localIdentifier

        imageManager.requestImageForAsset(asset, targetSize: AssetGridThumbnailSize, contentMode: PHImageContentMode.AspectFill, options: nil) { (result: UIImage?, info: [NSObject : AnyObject]?) -> Void in
            //print(result)
            if cell.representedAssetIdentifier.isEqualToString(asset.localIdentifier) {
                if let imageResult = result {
                    cell.imageView.image = imageResult
                }
            }
        }
}

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {

    // Cell that needs to open the camera roll when tapped)
    if indexPath.row == 0 {

        // Do something that opens the camera roll

    }

}


Share : facebook icon twitter icon
iPhone - UITableViewCell - Change NIB CustomCell by another NIB CustomCell when select a row?

iPhone - UITableViewCell - Change NIB CustomCell by another NIB CustomCell when select a row?


By : Jian
Date : March 29 2020, 07:55 AM
Does that help You can use set a "selected" iVar somewhere else, then reload the cell. if it's selected, load the selected nib. You are probably better off having both views in the same cell and swap them with a fade transition though.
UICollectionView not to reuse cell

UICollectionView not to reuse cell


By : Guillaume Néry
Date : March 29 2020, 07:55 AM
I hope this helps you . You can cached your heavy cell object in other instance, just at the UICollectionViewDataSource collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath function, to load the cell by your self. Do not use the dequeueReusableCellWithReuseIdentifier or dequeueReusableSupplementaryViewOfKind .But All memory management ,you should care.
so in your case ,you cache the "AD cell" in the other cached class , in your UICollectionViewDataSource delegate just use the cached cell object from your cached class.
How to show an UIActivityindicator in UICollectionview customcell untill the image downloads at server side?

How to show an UIActivityindicator in UICollectionview customcell untill the image downloads at server side?


By : DoodlerX
Date : March 29 2020, 07:55 AM
Hope this helps In cellForItemAtIndexPath you should set up your activity indicator. Then start loading your image for the cell in the background. When the image have loaded apply it to the cell on the main thread.
code :
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    __block UICollectionViewCell* cell = [cv dequeueReusableCellWithReuseIdentifier:@"cell"
                                                                       forIndexPath: indexPath];

    // Placeholder text --
    UILabel* label = [[UILabel alloc] initWithFrame:cell.bounds];
    label.text = @"Downloading...";
    [cell.contentView addSubview:label];

    // Load image in background  --
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{

        NSURL* url = [NSURL URLWithString: [NSString stringWithFormat:@"http://example.com/img/img%02lu.png", (long unsigned)indexPath.row]];

        // Load and decode image --
        NSData * imageData = [NSData dataWithContentsOfURL:url];
        UIImage *image = [UIImage imageWithData:imageData];

        // Apply image on the main thread --
        dispatch_sync(dispatch_get_main_queue(), ^{
            UIImageView* iv = [[UIImageView alloc] initWithImage:image];
            [cell.contentView addSubview:iv];
        });
    });

    return cell;

}
UICollectionView Static CustomCell reuse

UICollectionView Static CustomCell reuse


By : user3428149
Date : March 29 2020, 07:55 AM
will help you Think it's reusing another cell (the balloon in this case) and is not setting anything for the first index cell. If you make an else statement to create a new camera cell, hopefully it will reappear.
code :
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    GalleryCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];

    if (indexPath.item != 0) {
        [cell setCollectionItem:[collectionData_ objectAtIndex:indexPath.row - 1]];
    } else {
        // Set camera item here
    }
    return cell;
}
UICollectionView CustomCell- Not able to deselect already Selected Cell

UICollectionView CustomCell- Not able to deselect already Selected Cell


By : Shravani
Date : March 29 2020, 07:55 AM
With these it helps If you want to change image on selection of cell and if cell is already selected and you want to deselect it, then you can change your code like this
First Create one instance property selectedIndexPath like this
code :
@property NSIndexPath *selectedIndexPath;
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    BICollectionCell *cell = (BICollectionCell *)[self.collectionView dequeueReusableCellWithReuseIdentifier:@"BICollectionCell" forIndexPath:indexPath];
    [cell setSelected:YES];
    [self.collectionView selectItemAtIndexPath:indexPath animated:NO scrollPosition:UICollectionViewScrollPositionNone];
    if (self.selectedIndexPath == indexPath || ([objCustVehiclesList.modelName isEqualToString:self.str_ModelName] && _isCalledFromDetailVC) || (indexPath.row == self.indexPathToBeSearch.row && self.isCalledFromVehicleVC)) {
        [cell.imgSelectedImage setImage:[UIImage imageNamed:@"vs_tick.png"]];
    }
    else {
        [cell.imgSelectedImage setImage:nil];
    }
    <----Label Values--->
    return Cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (self.isCalledFromVehicleVC || self.isCalledFromDetailVC)
    {
     self.isCalledFromVehicleVC = NO;
     self.isCalledFromDetailVC = NO;
    }
    if (self.selectedIndexPath == indexPath) {
        [collectionView deselectItemAtIndexPath:indexPath animated:YES];
        self.selectedIndexPath = nil;
    }
    else {
        self.selectedIndexPath = indexPath;
    }        
    [self.collectionView reloadData];
}
Related Posts Related Posts :
  • Adding subview to scrollview
  • Use existing string as valueForKey value
  • How to set border color of multiple UIView in tableViewCell when the colors are from an NSArray
  • IOS Swift finishing a function when a button is pressed
  • Convert NSString to NSDate is not working
  • Xcode 7.1 with iOS 9.2 Error (Could not find Developer Disk image)
  • What will happen by calling [super alloc] in a subclass' factory method?
  • Why there is no warning while casting with viewWithTag?
  • Can I start my iOS app on the 2nd View of My Storyboard and use the Unwind Segue?
  • How to resize a UIImageView and force relayout of the parent UITableViewCell
  • Logically determine shortest path
  • autolayout problems by code?
  • Using componentsSeparatedByString with more than one separator string?
  • iOS : application crashing for null values while displaying scrolling tableview
  • How to Programmatically Segue to a New ViewController in a New Storyboard
  • Interpolation in texture3D in OpenGL ES 3.0 on iOS
  • How do I remove new line characters from a string?
  • Grouped UITableView from array of objects
  • How to write this answer in Swift? popToRoot on one tab when the user is in a different tab?
  • Collection View Loads Choppy
  • How to trigger certain segue for a selected nib cell in didSelectRowAtIndexPath
  • Filter users based on their specified age ranges. Parse and Swift
  • Audio queue recording error: AudioConverterNew from AudioQueueNew returned 'fmt?'
  • no registered handler for URL scheme itms-apps - Rate button Simulator
  • Escaping parameters in iOS Apple App Store Download Link
  • Resizing UISearchBar in UINavigationBar titleView doesn't work
  • Button that will generate a random background color - Xcode - Swift
  • Subclassing SCNScene and SceneKit Editor
  • iOS: get result of 'currentUserNotificationSettings' alert dialog
  • Delete user in iOS using Parse login
  • iPad Pro reporting wrong screen scale
  • Solving math using DDMathParser in Objective-C
  • Why does Acknowledgements.plist from CocoaPods look messed up on iOS 9 and what can you do about it?
  • reloadRowsAtIndexPaths interfere with deleteRowsAtIndexPaths
  • How to do core data fetch predicate for NSDate in swift? i want to filter it by months
  • Creating a centered View with Labels
  • Singleton Created, Errors upon use in ViewController?
  • Error using performSegueWithIdentifier Swift 2.1
  • I've created a singleton class, how can I call it from my ViewControllers?
  • Alamofire won't build in empty Swift project using Cocoapods
  • Set first annotation in annotations
  • Swift 2.1 New Way to Initialize String Array?
  • Swift - time based UUID - how to sort them
  • Swift: How can I have a listener that reports when connection is lost and when it comes back?
  • Xcode fails to generate Adhoc IPA
  • Apple Watch - how do I schedule a complication update to be in synch with Push notification?
  • UICollectionView will not display cells in custom popupview
  • iOS: Repeat animation block until an event completes
  • Set NavigationBar for Destination ViewController Only
  • ios9 Image Download
  • Accessing Value of NSDictionary within array
  • Change Data with Switch on UITableViewCell
  • ios9 - Xcode 7 - Swift - Keyboard shows correctly in Simulator but not on an actual Device
  • Sizing for UITabBar images?
  • Sharing a Realm Database between a user's devices
  • Use of @dynamic in objectiveC
  • PFGeoPoint to save something on map permanently?
  • Xcode Table Cell not Fully Highlighted
  • Parse crash reporting 1.9.2 not working
  • Over the air distribution stopped working XCode7.1.1
  • shadow
    Privacy Policy - Terms - Contact Us © animezone.co