Allocating UILabel in UICollectionViewCell - ios

I making app with collection view and different layout for portrait and landscape orientation and i am allocating UIlabel for every cell.
This is how i do it.
UILabel *scheduleCellLabel = [[UILabel alloc] init];
scheduleCellLabel.textAlignment = NSTextAlignmentCenter;
scheduleCellLabel.frame = CGRectMake(0, 0,scheduleCell.bounds.size.width, scheduleCell.bounds.size.height);
[scheduleCell addSubview:scheduleCellLabel];
Problem is that i reload collection view everytime the orientation changes, but the old cells stays in collection view. I tried to remove the label from cell on load, but that is not the issue.
This is how it looks:
Thanks in advance.

In my opinion, it's not good practice to put subview-adding code in cellForRowAtIndexPath unless you need to; for instance, if some cells have a subview, and some do not based on the indexPath. You should either make the cell in IB, or subclass the cell, and add any subviews in the cell's init method. That way, you don't have to remove views before you re-add them, or check if they exist before you add one; that just complicates your cellForRowAtIndexPath method.

Delete and add the label in cellForItemAtIndexPath. First ull check if label is in the cell, if yes, remove it from superview, then create new label and add it to its contentview.

Probably, you allocate your uilabel in cellForRowAtIndexPath method and it appears again and again when you reload your collectionView.
To solve your problem,just define your label as a property and allocate it in the viewDidLoad method and do what you want in cellForRowAtIndexPath method. Hope this will help.
For example, this is your header file:
#property (retain,nonatomic) IBOutlet UILabel *label;
and this is the viewDidLoad method of implemantation file:
_label = [[UILabel alloc] init];

Related

UISwitch not displaying in custom UITableView Cell

I need a custom cell with a label and a switch.
Now, the main problem is that I can't get the switch to display. I have tried several methods, including adding the switch programatically to the cell's accessoryView.
I used the IB, added the switch to the cell, connected the IBOutlet. I also tried to add the switch programatically, in cell's awakeFromNib:
- (void)awakeFromNib {
[super awakeFromNib];
if (!self.fieldSwitch) {
self.fieldSwitch = [[UISwitch alloc] init];
[self.fieldSwitch addTarget:self action:#selector(switchUpdatedValue:) forControlEvents:UIControlEventValueChanged];
self.fieldSwitch.onTintColor = [ColorManager sharedInstance].genericSwitchColor;
self.accessoryView = self.fieldSwitch;
}
}
This has had absolutely no effect; I also tried adding it as a subview to the cell's contentView then calling bringSubviewToFront:. Again, no success.
I checked, and self, accessoryView, fieldSwitch none of them were nil.
Does anyone have any idea what could be so wrong? on a side note, does anyone understand why adding a control from the IB is broken by default?
If you are using size classes you have to set a constraint for the UISwitch. For example if you are using an Any Any size class and you place the UISwitch in the cell it may actually be displaying far off to the right. (I would of posted this as a comment however not enough rep)

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

UICollectionViewCell reuse issue

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>

Modify outlet inside UITableView custom cell

I have a custom UITableViewCell that has 2 UITextFields and a UIButton. The text fields are set in the cell:ForRow:AtIndexPath: method. What I need is to change the opacity of this UIButton dynamically, without having to reload the entire tableview. I was thinking of something like cellForRowAtIndexPath:, but how to access the outlet in this cell?
Thanks
When you create the cell in cellForRowAtIndexPath: just maintain a pointer to it in code. If that button is on every cell then you can just create an array of buttons so that you can easily access the button for the cell you want.
UIButton *cellButton = [[UIButton alloc] init];
[allCellButtons addObject:cellButton];

multiple UITableViewCell cells accessoryView referencing same view fails

In a UITableViewController I am instantiating UITableViewCells where some cells are highlighted by an accessoryView. For me, this works:
// works for me
UIImageView *favoriteImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"icon"]];
[cell setAccessoryView:favoriteImageView];
It seems wasteful to me to instantiate the same view repeatedly for multiple cells; however when I attempt to re-use the same view as the accessoryView of multiple cells my app fails in a miserable way (completely black screen, no views presented) I haven't been able to debug. Whether I declare favoriteImageView as a static inside the method such as
// doesn't work for me
static UIImageView *favoriteImageView = nil;
if (!favoriteImageView)
favoriteImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"icon"]];
[cell setAccessoryView:favoriteImageView];
Or declare it as an ivar and define it in init such that I wind up with:
// doesn't work for me
[cell setAccessoryView:[self favoriteImageView]];
In these two not-working cases, when one cell has its accessoryView set, it displays properly. As soon as I mark a second row such that the accessoryView would be set to reference the same view, the whole thing hangs up.
What are the requirements for constructing a UIView and/or configuring a UITableViewCell in such a way that the same UIView may be referenced as the accessoryView of multiple UITableViewCells?
UIImageView extends from UIView. And a UIView can't be in two or more places at the same time.
Therefor, if you try to display an UIImageView in two or more cells at the same time, it won't work. You need an UIImageView for each cell on screen.
I suggest that for each cell you create the UIImageView. The tableviewcells will be reused alongside with their accessoryview, so I wouldn't worry too much about performance or memoryproblems.

Resources