Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Feb 1, 2013, 12:06 PM   #1
naphatkrit
macrumors member
 
Join Date: Jul 2011
A single instance of a UIView subclass across the cells of UICollectionView

Hi,

I want to use a single instance of a subclass of UIView (so that it doesn't have to be redrawn) as the background of the cells of my UICollectionView. The way I have set it up is, in the UICollectionView, declare a variable (nonatomic, strong) of the class for my background view. In viewDidLoad, I initialize the view with the frame (0, 0, width, height). In my subclass of UICollectionViewCell, I have a variable declared for the background (nonatomic, weak). Then back in my CollectionView, in the cellForIndexPath method, I assign the initialized background view to the cell's background view and call "setNeedsDisplay" to force a redraw. This works, but only for the first cell, not any other. Is my approach correct?
naphatkrit is offline   0 Reply With Quote
Old Feb 1, 2013, 01:49 PM   #2
Duncan C
macrumors 6502a
 
Duncan C's Avatar
 
Join Date: Jan 2008
Location: Northern Virginia
 
Quote:
Originally Posted by naphatkrit View Post
Hi,

I want to use a single instance of a subclass of UIView (so that it doesn't have to be redrawn) as the background of the cells of my UICollectionView. The way I have set it up is, in the UICollectionView, declare a variable (nonatomic, strong) of the class for my background view. In viewDidLoad, I initialize the view with the frame (0, 0, width, height). In my subclass of UICollectionViewCell, I have a variable declared for the background (nonatomic, weak). Then back in my CollectionView, in the cellForIndexPath method, I assign the initialized background view to the cell's background view and call "setNeedsDisplay" to force a redraw. This works, but only for the first cell, not any other. Is my approach correct?
No, that approach is not correct. A view can only have a single superview. Adding your view instance to a second cell will remove it from the previous view.

You're going to have to create multiple instances of your view.

Alternately, you could capture a snapshot of your view to a UIImage, and install that image as the contents of multiple image views. UIImage objects CAN belong to more than one view at a time.
__________________
Regards,
Duncan Champney, WareTo.
Check out our latest iOS app, Face Dancer, available for free on the App Store.
Duncan C is offline   0 Reply With Quote
Old Feb 1, 2013, 01:52 PM   #3
naphatkrit
Thread Starter
macrumors member
 
Join Date: Jul 2011
Quote:
Originally Posted by Duncan C View Post
Alternately, you could capture a snapshot of your view to a UIImage, and install that image as the contents of multiple image views. UIImage objects CAN belong to more than one view at a time.
Which would you recommend, performance-wise?
naphatkrit is offline   0 Reply With Quote
Old Feb 1, 2013, 02:17 PM   #4
Duncan C
macrumors 6502a
 
Duncan C's Avatar
 
Join Date: Jan 2008
Location: Northern Virginia
 
Quote:
Originally Posted by naphatkrit View Post
Which would you recommend, performance-wise?
It doesn't make much difference. Views cache their contents by default, and only get redrawn if something changes. Don't call setNeedsDisplay on the view unless something changed where the view needs to be redrawn but the system doesn't do it automatically.
__________________
Regards,
Duncan Champney, WareTo.
Check out our latest iOS app, Face Dancer, available for free on the App Store.
Duncan C is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Tags
instance, single, uicollectionview

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Excel cells that attribute a score if two cells are the same ellisfella Mac Applications and Mac App Store 0 Nov 21, 2013 05:48 AM
UICollectionView Crashes on iOS 7 RagingGoat iPhone/iPad Programming 5 Nov 13, 2013 10:45 AM
Resolved: UICollectionView and UIMenuController [workaround] ChristianJapan iPhone/iPad Programming 3 Sep 29, 2013 02:47 AM
Scrolling issue with UICollectionView. zaxonus iPhone/iPad Programming 11 Aug 21, 2013 02:59 PM
Trying to subclass UIView Hans Kamp iPhone/iPad Programming 3 Mar 25, 2013 12:15 AM

Forum Jump

All times are GMT -5. The time now is 01:10 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC