I am in the process of creating an App to take care of Maintenance Planning for Harley Davidson enthusiasts.
The planned target is the iPad series at this point.
I wish do display a grid that shows detail for each fuel purchase. The following graphic hopefully shows what I am trying to achieve.
To get this, I’m using the UICollectionView as I don’t think there is another View that gives me what I am after.
The code below illustrates how I have done this – but I believe it’s a pretty chunky way of doing it:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
FuelCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"Cell" forIndexPath:indexPath];
cell.backgroundColor = [UIColor lightTextColor];
cell.cellLabel.text = [NSString stringWithFormat:#" %# %# %# %#",self.fuelDetailsForSelectedBike[indexPath.row][0],self.fuelDetailsForSelectedBike[indexPath.row][1],self.fuelDetailsForSelectedBike[indexPath.row][2],self.fuelDetailsForSelectedBike[indexPath.row][3]];
return cell;
}
What I would prefer is have a cell for each piece of information but unsure how I would achieve this using the UICollectionView.
You already have created a custom cell FuelCollectionViewCell which is a good starting point.
Probably you have a prototype cell in your Storyboard with subclass FuelCollectionViewCell right?
In this cell, add 4 UILabel aligned in one row as your design and 'link' them to your subclass .h file with a simple drag+alt (as you've done with cellLabel).
And update your cellForItemAtIndexPath method with something like:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
FuelCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"Cell" forIndexPath:indexPath];
cell.backgroundColor = [UIColor lightTextColor];
cell.dateLabel.text = self.fuelDetailsForSelectedBike[indexPath.row][0];
cell.litresLabel.text = self.fuelDetailsForSelectedBike[indexPath.row][1];
cell.distanceLabel.text = self.fuelDetailsForSelectedBike[indexPath.row][2];
cell.costLabel.text = self.fuelDetailsForSelectedBike[indexPath.row][3];
return cell;
}
Related
I show several cells in a collectionview, some of those cells are paired (stereo audio channels) and i want them to be merged together as seemingly 1 button, rather than having the generic collectionview padding other cells have.
Is there a way I can remove the padding between 2 specific cells to merge into 1 larger cell? Perhaps removing the right hand padding of the first cell in the pair? Or is this beyond the scope of the collectionview?
Here is my delegate method:
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
CDCBusSelectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"cell" forIndexPath:indexPath];
NSString *busNameString = [self.busNamesRecieved objectAtIndex:indexPath.row];
cell.busNameCell.text = busNameString;
NSNumber *busStereoState = [self.busStereoStateRecieved objectAtIndex:indexPath.row];
NSInteger busIntState = [busStereoState integerValue];
if (busIntState == 1) {
// stereo -- need to merge cells or something
[cell setBackgroundColor:[UIColor colorWithRed:255 green:90 blue:127 alpha:1.0]];
}
return cell;
}
I am able to load the collection view but unable to load the cell.below is my code I'm using.
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier = #"reuseCell";
[self.view_dashboard registerClass:[UICollectionViewCell class]
forCellWithReuseIdentifier:#"reuseCell"];
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
recipeImageView = (UIImageView *)[cell viewWithTag:100];
recipeImageView.image= [UIImage imageNamed:[dashBoard_img objectAtIndex:indexPath.row]];
return cell;
}
There doesn't seem to be anything particularly wrong with your code.
Loading a cell in a collection view requires more work than just this single method.
I would start explaining further, but I would just be reading the docs to you. Instead, have a read of the Apple Documentation for yourself.
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UICollectionView_class/index.html#//apple_ref/doc/uid/TP40012177-CH1-SW4
I've got an issue with my UICollectionView where my cells are always initiated blank/in a default state with no data.
The data only appears in the cells after scrolling them in and out of view.
Code:
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"EquipmentCell";
APInventoryCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
if (!cell) cell = [[APInventoryCollectionViewCell alloc] init];
//set cell data...
cell.label.text = [arrayOfStrings objectAtIndex:indexPath.row];
return cell;
}
Your problem is that you probably set the arrayOfStrings somewhere in the viewDidLoad method of your view controller, the problem with that is that the collectionview datasource calls are done before that.
What you should do in your viewDidLoad method just call [collectionview reloadData]; an you will be fine
I have an UICollectionView with images, now I want to add a sticker to all images from 5th image to the end. I use the following code, and It works, but when I scroll down to the end, then scroll it back, all stickers appears on ALL IMAGES.
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier = #"Cell";
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];
recipeImageView.layer.borderWidth = 0.3;
recipeImageView.layer.borderColor = [UIColor blackColor].CGColor;
recipeImageView.image = [UIImage imageNamed:[wallPack objectAtIndex:indexPath.row]];
// add Paid stickers
NSString *stickName = [NSString stringWithFormat:#"stickerImage.png"]; /// image name
UIImage *stickImage = [UIImage imageNamed:stickName];
stickView = [[UIImageView alloc] initWithImage:stickImage];
stickView.contentMode = UIViewContentModeScaleAspectFit;
if (indexPath.row >= 5) {
[recipeImageView addSubview:stickView];
}
return cell;
}
where is the problem? why before scrolling down everything appears as I want, from 5th to the end, but when I scroll it back, the sticker image puts on ALL images.
The cells are reused. You must add the sticker to the ones you want to add it to if it is not there, but also remove it from the ones where you don't want want it if it is there.
I would suggest creating UICollectionViewCell subclass wherein you can add a property for stickImage and set it (based on your condition).
Moreover in prepareForReuse function you can default it to whatever you want, it will also make your
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
a bit more cleaner.
I am creating a new background view each time a new cell is requested as follows:
-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"MyCell" forIndexPath:indexPath];
...
cell.backgroundView = [[MyCellBackgroundView alloc] initWithFrame:CGRectZero];
return cell;
}
Should I worry about so may instances being alloc'ed and dealloc'ed?
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"MyCell" forIndexPath:indexPath];
This line of code handles the alloc / init of each cell and their subviews for you. Memory management it taken care of at this point.
If you're still curios - while running the app, open the debug navigator and it will show you CPU and memory usage.