Check which column a UICollectionView Cell is in? - ios

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

Related

Custom Calendar By 3 UICollectionViewCell

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).

Different line spacing for each item at UICollectionView - swift 3

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)

ios - how to determine if a cell indexPath changed in uitableview

I have two questions :
1- Is it possible to know if a cell indexPath.row has changed in a UITableView ? for example if I have an element at index (0) and then I add another one, the first element will be at index (1). Is it possible to detect this change ?
2- Is it possible to fnd the index of an element by the cell title ? for example I have X,Y,Z element in the tableview and I want to know in which cell 'X' is placed ?
thanks guys,
Generally speaking, it's common practice to isolate model and view. What I mean by this, is separate the storage of the data and the view that's rendering it.
For example, you may be creating a table view of color names. Therefore, you could store an array of color names as a property of your view controller
class UIColorViewController: UITableViewController{
let colors = ["Black", "Blue", "Yellow"]
As I'm sure you're doing, you can assign the view controller as the data source of the table view so you can tell it how many sections you want, the number of rows you want in a section, and what to render onto a cell.
In this elementary example, the number of sections could be 1 and the number of rows in this section would be the length of the colors array. Now when rendering data onto the cell, you simply index the colors array by the index path.
fun tableView(UITableView, cellForRowAt: IndexPath){
var color = colors[indexPath.row]
cell = //dequeue cell
cell.textLabel?.text = color
return cell
}
So by separating the data from the view, if you want to find what cell a particular value is at, you can simply search through the array where the color is at and the index returned would be equivalent to what cell this color is or will be at depending on if that cell has been rendered onto the screen yet.
For example using the colors array, if I were looking for "Blue"
for (index, color) in self.colors.enumerated{
if color == 'Blue'{
print("Found Blue at index \(index)")
}
}
When you want to add a cell or delete a cell, you simply modify the colors array which is storing the model data, perform the appropriate table view related action, and the invariant remains that the index of the color in the array would be equivalent to the index of the cell in the table view.

Section within a section for UITableView

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.

scrollup uitablviewcell partially visible

I need some help.
That i want to scrollup uitalbeview cell which is partially
displaying.See screen shot.
Here my tableview has a three section and u can see that in morning
section the row is displaying partially.I just want to move up that
kind of row.
I tried content offset but not getting any idea that how to get first
visible row offset so,i can calculate and move it up or down
.
Note: not for bottom row only top rows.
This is the default behavior of a UITableView. The section headers will stick at the top of the screen until the next section header will reach it. One easy work around is to not use headers at all and simply use two cells. You would then have to add code in your viewForCellAtIndexPath:
if(indexPath.row == 0) {
//setup a header cell
} else {
//setup a data cell
}

Resources