UICollectionViewCell reuse issue - ios

I have a storyboard and one of my viewControllers has a CollectionView. I have a prototype cell that has a label inside. I created a class for that prototype cell in order to have access to the label via an IBOutlet property.
The problem is that I have many cells. Inside the initWithCoder constructor of the cell I add some cornerRadius.
When I push this viewController on the screen, it lags a lot. Without the corner radius it doesn't. I also noticed that initWithCoder gets called all the time, for each cell.
I tried to register the cell like this [self.myCollectionView registerClass:[MyCell class] forReuseIdentifier:#"MyReuseIdentifier"] but it doesn't work. I dont know how to use the registerNib method.
The reuse identifier is set in the storyboard prototype cell.
I don't know how to achieve the rounded corner effect without loss in perforrmance.

I have done my cell corner round in cellForItemAtIndexPath method like
cell.imageView.layer.cornerRadius = 10;
cell.imageView.layer.masksToBounds = YES;
and dont forget to import #import <QuartzCore/QuartzCore.h>

Related

UICollectionView display previous cell content in next cell while scroll horizontally

I am using a UICollectionView to show an array of images, horizontally and zoom in/out that image in the cell with the help of scroll view using constraints. But when I scroll horizontally it shows me previous cell content in the current cell. I am and not able to identify what is the problem.
CollectionView datasources
CollectionCell Class
try this when you reuse the Cell in cellForItem at
cell.imageView.Image = nil
Please share some code so that we can help you better. Meanwhile, make sure you have the following parts working correctly in your code:
Register correct UICollectionViewCell subclass with correct reuse identifier
Use the method [self.collectionView dequeueReusableCellWithReuseIdentifier:itemIdentifier forIndexPath:indexPath] and pass both the parameters correctly.
In UICollectionViewCell subclass, override prepareForReuse and reset the UI parameters there.
Override prepareForReuse in imageCell class, and set imageView.image to nil.
The contentSize of the scrollView should be as big as imageView.image.size, or just use MWPhotoBrowser

Adjusting the size of the UIImageView in a Right Detail style UITableViewCell

I have a simple application which contains a few UITableViewControllers in a UITabBarController. The last UITableViewController is called the More tab and it's a static UITableView. I have designed everything in Storyboard.
Within the More UITableView, I have 4 sections. Within section One, Two and Three, I have 2 basic UITableViewCells. That works well. Within section four, I have 11 basic UITableViewCells. I'm populating the cell titles within Storyboard. I have decided to slightly modify the UI so that section 4 contains a small image and the label.
To do this, I have used Right Detail as the style of the UITableViewCell because it allows you in the inspector, to specify an image.
The problem at the moment is that the image looks just too big.
I have the image in the view, but the size inspector is greyed out.
Without using a custom UITableViewCell (because everything works in this way; just the image is too big), how can I specify the size of the UIImageView? And where do I specify that?
You can create a UITableViewCell subclass and set it to your static cell within storyboard, e.g.
#interface MyCell : UITableViewCell
#end
#implementation MyCell
-(void)layoutSubviews
{
[super layoutSubviews];
//TODO: adjust self.imageView
//self.imageView.contentMode = ...
//self.imageView.frame = ...
}
#end
Or, you can click on that imageView and change it's Mode to Center from the inspector and set an image with right sizes that you want to display.

Detecting taps on Custom Cells of UITableView

So i have a custom UITableViewCell which contains three UILabel. The UITableViewCell is such that the UILabel completely cover the cell.
Now i want to detect whenever user taps on the cell. Problem is as the UILabel cover the cell I cannot use UITableView delegate method for detecting touch on the cell.
I thoughout about using gesture recoginzers on the UILabel but then i don't get the index of the touched cell. I also thought about placing a transparent button on top of the cells but here there is also the same problem that i can't get the index of the touched cell.
Can anybody guide me with an approach on how can i accomplish detecting taps with tableView didSelectRowAtIndexPath when UILabel are covering the cell.
Try to set userInteractionEnabled to false in all labels in cell. This will pass touch to the cell and then you can use UITableView delegates. Be sure that cell stays userInteractionEnabled = YES
To get the touch event besides didSelect method try this. In your custom cell class add a block like,
typedef void(^TappedCell)(CustomCell *cell);
and add a property for it,
#property (nonatomic, strong) TappedCell tappedCell;
and on transparent button action
- (IBAction)buttonTapped {
if (self.tappedCell) {
self.tappedCell(self);
}
}
And in cellForRow method
__weak type(self)weakSelf = self;
cell.tappedCell = ^ (CustomCell *tappedCell) {
//Ur actions goes here
//for getting index
NSIndexPath *indexPath = [weakSelf.tableView indexPathForCell:tappedCell];
[weakSelf someActionWithIndexPath:indexPath];
};
It doesnt matter whether UILabel hide your cell or UIImageView hide it. It will automatically detects tableView didSelectRowAtIndexPath. Check it first you wont face any problems. Did you try to implement tableview in detail first

UIScrollview in a Custom UICollectionViewCell Not Working

I'm trying to get a UIScrollView to work correctly inside of a UICollectionViewCell.
The custom cell is being loaded in via a xib file and is a subclass of UICollectionViewCell. I had problems getting other controls working, such as a button and a gesture recognizer since the UICollecitonView doesn't seem to be passing any touches to the cells, but I got around those with gesture recognizers on the UICollectionView itself. The one remaining issue I have is the UIScrollViews...
The UICollectionView scrolls horizontally, and the UIScrollView in the cells scroll vertically. I've tried using a UIPanGestureRecognizer to scroll them, but that seems to disable the UICollectionView's ability to scroll.
Anybody have any thoughts?
EDIT: Got it!
So I had converted to a collection view from a previous third party library we were using before iOS6. Turns out the problem was with the xib files we were using for the cells. With the library before, the cells were just subclasses of UIViews. I changed the classes to subclass UICollectionViewCell, and updated the Custom Class. Turns out this was not enough. In order for touches to get passed to the cells I needed to actually had to drag in a new UICollectionViewCell from the Object library, copy over all the subviews and reconnect the IBOutlets. After this, it worked!
I fixed this in my code by making sure resizing of the scroll view happens on main thread.
My collection view is using nsfetchedresultscontroller that is using block calls to refresh selected cells. On the first time a cell was selected the scrollview would not scroll. However if you clicked on another cell and clicked back it would work fine. The initial load of the cell seemed like size calc might not be where it needed to be on main thread to affect behavior.
-(UICollectionViewCell *) collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"CustomCell" forIndexPath:indexPath];
dispatch_async(dispatch_get_main_queue(), ^{
// resizeScrollViewMethod should be where scrollview content size > scroll view frame.
[cell resizeScrollViewMethod];
});
return cell;
}
- (void) resizeScrollViewMethod {
//Do your scrollview size calculation here
}
No problem with UISCrollView in UICollectionViewCell. Use StoryBoard and you'll scroll OK.
UIScrollView overlay UICollectionViewCell, so that didSelect work only when tap outside ScrollView and inside Cell (scrollView.frame < cell.frame).
If you want to implement tap or other gesture, just add it to UIScrollView in awakeFromNib
Refer code:
https://github.com/lequysang/github_zip/blob/master/CollectionViewWithCellScrollViewStoryBoard.zip

Changing the contentView when an Accessory button is tapped

I've subclassed UITableViewCell to display a UIImage and two UILabel subviews. In the view controller for the table view, in the method cellForRowAtIndexPath: I've enabled an accessory view via setAccessoryType:UITableViewCellAccessoryDetailDisclosureButton.
Cells display correctly.
When I tap on the accessory disclosure button I want to replace the two label subviews with two different label subviews. My approach was the following:
In the subclassed UITableViewCell, inside layoutSubviews, create the
CGRects for the "alternate" labels, position them in the same
places as the two "original" label and hide them via setAlpha:;
When the disclosure button is tapped swap out the set of two
label by adjusting their respective alphas.
The problem is I can't figure out what logic in layoutSubviews I'd use to know whether the accessory button has been tapped. I see that in the view controller accessoryButtonTappedForRowWithIndexPath: is called when that button is tapped and from there it seems like I would call layoutSubviews but can't figure out how to use that fact to accomplish what I'm trying to do.
Am I going about this all wrong? Instead of hiding/showing CGRects with alpha should I simply be creating another subclass of UITableViewCell?
When I tap on the accessory disclosure button I want to replace the two UILabel subviews with two different UILabel subviews.
I'll do the following. Create a public method in your UITableViewCell subclass like the following:
- (void)changeContentForCell;
In this method you could set the contentView as you prefer.
Then in your view controller implement
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
CustomCell* cCell = (CustomCell*)[tableView cellForRowAtIndexPath:indexPath];
[cCell changeContentForCell];
}
This is a simple example for change the content. In my opinion you don't have to use layoutSubviews to add views.
Leave this logic in changeContentForCell and then call setNeedsLayout to change your layout. Maybe you could have a variable that tracks the state for your cell: normal state or modified state.
Hope it helps.

Resources