I want to create a calender with use of only 3 collectionViewCell.
I have achieved this by 12 UICollectionViewCell for 1 year:
.
but now I want to make it work by just 3 collectionViewCell.
I want to implement below logic. but not getting idea how to do.
For example, current scenario is:
indexPath.item 0 - cell for previous mointh(JAN),
indexPath.item 1 - cell for current month(FEB),
indexPath.item 2 - cell for next month(MAR).
Now,
when user scrolls to next month(MAR), at that time I want to remove indexPath.item = 0(JAN), and shift remaining cells on left.
after that add next month cell(APR) at indexPath.item = 2
so it will be like this after one scroll:
indexPath.item 0 - cell for previous mointh(FEB),
indexPath.item 1 - cell for current month(MAR),
indexPath.item 2 - cell for next month(APR).
Related
I have a UICollection View and am creating a custom animation when a user taps on a cell.
There are three columns in the collection view. Is there a quick way to figure out, given a index path, the column that was tapped?
If you have a single section, you can just use modulo arithmetic on the item of the index path.
let column = indexPath.item % 3 // gives a value from 0 to 2
Yes. Assuming you setup the columns as sections in the collection view, you should just be able to check the section of the indexpath.
An IndexPath is primarily made of two parts, a row, and a section. If you have 3 sections than indexPath.section == 0 is the first column, indexPath.section == 1 is the second, and indexPath.section == 2 is the third
I have a UICollectionView which consists of 12 cells from Jan to Dec as shown in the screenshot below:
How can I make the current month to always to be at the center of the collection view? For example, Dec shall be shown on the center for this month.
One of the solutions is to scroll automatically to the next page and center the 12th cell but how can I achieve that?
You can use the cellForItem function to define how the cells are going to appear and the UICollectionView function reloadData() to update all the cells always you think it's necessary.
If December should be the cell in the middle, make sure that 5 or 6 cells come before December, you should be able to do this with simple conditions inside cellForItem.
For example, as you have 12 cells, you can get the current month as and integer (12 for December), and you will know that the first month will be 12 - 6 = 6 (June), so you can do this calculation for each indexPath.
To simplify your job:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = ...
let month = GetCurrentMonthAsInteger()
var cellMonth = month - 6 + indexPath.row //or section if you have 12 sections
if(cellMonth < 1) {cellMonth = 12 + cellMonth}
else if(cellMonth > 12) {cellMonth = cellMonth - 12}
cell.setInformationForMonth(cellMonth)
...
return cell
}
You can try making min spacing to zero for cells and lines
I have a collection view written programmatically with swift 3. It has 1 section and 6 vertical items in it. The default minimum line spacing for each item is 10 I assume, but I want to set different line spacing for each item. For example, the space between item 0 and 1 become 50 but space between item 1 and 2 become 0. How can I do that? I searched a lot but couldn't find solution. Thanks in advance.
It is directly not possible by setting some property of your collection view but you can do one trick I think,
set your minimum line spacing and minimum interitem spacing to 0 like,
layout.minimumInteritemSpacing = 0
layout.minimumLineSpacing = 0
so your collection view cell will not have any space at any side!
now keep your cell's background color as clear color!
and add UIView (your content) in your cell with size(less than cell size) that shows spacing between two cell. So, you can add different size of view in every cell and it will display like different space between every cell!
Or
If you don't want to resize your inner view of cell then you can return different size for your cell from delegate method sizeForItemAtIndexPath for different items!
For example,
I am writing objective c code for example,
your sizeforitem,
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.item == 1 || indexPath.item == 6 || indexPath.item == 7 || indexPath.item == 9 || indexPath.item == 15) {
return CGSizeMake(screenWidth/3 -20, screenWidth/2);
}
return CGSizeMake(screenWidth/3, screenWidth/3);
}
and result is
An idea could be to consider the lines spacing as "other kind of cell", like blank cell, you see? And in the method heightForRowAtIndexPath, when the indexPath correspond to those blank cell, you return their custom height, otherwise you return the row height of your "real" cells.
(This imply to return those blank cell in the cellForRow, and add their number to your current numberOfRowInSection count too)
I have big problem with my data in tableView.
I added image in indexPath.row == 0 using code:
let business = self.results[indexPath.row]
if indexPath.row == 0 {
cell.timeImage.image = UIImage(named: "greenTime.png")
}
My image is shown when indexPath.row is equal to 0, 9, 17, 25 ...
My business array.count = 40
Can someone tell where is the problem?
EDIT
I can see that my image is added to another indexPath.row when I move my table height.
As #Akhilrajtr wrote, you should provide an else-block. Reason for that being that iOS reuses cells. If you set the image for one cell, that cell moves off-screen and is therefore being reused and the image is not unset it will persist even though you did not explicitly set the image for this indexpath:
if indexPath.row == 0 {
cell.timeImage.image = UIImage(named: "greenTime.png")
} else {
cell.timeImage.image = nil
}
In short, every if-block should have an else-block where you undo the changes made in the if-block.
I have to create a UITableView that has sections within sections however I have never done this before and am not even sure if this is possible.
This is what it needs to look like
Header 1
Sub Header 1
cell 1
cell 2
cell 3
Sub Header 2
cell 1
cell 2
Header 2
Sub Header 1
cell 1
Sub Header 2
cell 1
cell 2
All fields are dynamic, so there could be 0 or more Header sections; would a UITableView be the best way to go about this? if so how would I approach this?
There is no easy way to do it, you have to plan cleverly.
From numberOfSectionsInTableView return the number of "Header".
For each section in numberOfRowsInSection return Sub Header + Cell in each sub header.
From cellForRowAtIndexPath just chek if it is a sub header or a cell. If its is a sub header the return a cell which has a Label near to left side, if it is a cell then return a cell that has the label in more far away from left border.
You can use same cell just changing the frame of the cell's label.