Determine row and column of UICollectionViewCell - ios

I have a UICollectionView displaying many items in a single section, wrapping over several rows, using UICollectionViewFlowLayout.
Is there a simple way to determine the actual row and column as displayed of a given UICollectionViewCell?
In the example image below, I'm looking for the row & column:
This is just an example - in the actual app I don't know how many columns there are. I realise I could calculate by knowing the cell width, collection view insets and spacing, but I was hoping there was another method that would give me the answer.

you can retrieve the indexPath calling:
NSIndexPath *path = [myCollectionView indexPathForCell: cell];
if each row contains the same number of items
NSInteger itemsPerRow = 2;
NSInteger row = path.item / itemsPerRow;
NSInteger column = path.item % itemsPerRow;

Related

Load cells in UITableView

I have UITableView with cells, i use footer(height = 10pt) for each sections, header (height = 64pt) for each sections and cell (hight = self.view.frame.size.hight - 64 - 10).
TableView loads only first cell, when i launch app (because second cell is not visible on screen), but i want to load first and next cell. And when i scroll to second row i want to load third cell.
How can i achieve this?
UITableView delegate methods provide you several ways to track which cell just appeared on screen (i.e. tableView:willDisplayCell:forRowAtIndexPath:).
One way to do it is to track which cell was displayed, and load +1 and/or -1 indexes manually.
Note: you shouldn't load the UITableViewCells directly but the content they are supposed to show (according to M.V.C).
lazy-loading suit for you, which means first is visible, when scroll down second item should be ready.
//Store all items
NSArray *allItems = #[].mutableCopy;
//Load first two and store into allItems
NSDate *first, *second;
allItems = #[first,second];
//Check which cell is visible when scroll down and if the data has been ready
1) path.section is the visible cell
NSIndexPath *path = [tableView indexPathForCell:aCell];
2) allItems count is the number of data you have loaded
allItems.count = path.section + 1;
3) If allItems.count == path.section, you should load the next data, which is next of the current visible one.
By these two conditions the data should be ready all the time.
After each load [tableView reload] to display.

How can I get index path of cell on switch change event in section based table view

I have custom cell having switch in few cells at right side. what I want is to store value of specific cell on switch change event. Table view has number sections so I can't set tag for switch because I need section as well as row to obtain index path.
Any suggestion any alternative but I have to use UISwitch in section based table view.
Thanks
In your custom cell add properties which help you identify the information the cell represents. Index path, indexes for your data model etc...
Then add a block property to the cell which you can call to tell a UITableView or any other piece of code when a cell switch changes. e.g.
#property (nonatomic,copy) void (^onSwitchChange)(UITableViewCell *cell);
Inside your custom cell code, add an action handler for the UISwitch. When it fires, call self.onSwitchChange(self) which will notify the code which registered an onSwitchChange block that a switch has changed and on which cell.
In your table view when you create the cell, set the onSwitchChange block as follows:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath (NSIndexPath *)indexPath
{
<snip>
YourUITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:yourCellIdentifier forIndexPath:indexPath];
cell.onSwitchChange=^(UITableViewCell *cellAffected){
// Add code to deal with the swicth switch using properties of cellAffected
... Your handler code here ...
}];
<snip>
}
This lets you handle all the changes in the table view controller. Hope this helps.
The answer from #Jageen works. I had to find out which superview the cell is, mine was one more level higher.
UITableViewCell *cell = (UITableViewCell*)[sender superview].superview.superview;
NSIndexPath *indexPath = [self.myTableView indexPathForCell:cell];
NSLog(#"Section %ld Row : %ld",(long)indexPath.section,(long)indexPath.row); // print row section and index
You can still create tags even if you have sections if you have some idea of about max rows in a largest section. For example if you think there will be 1000 rows in a section then you can create tag using following formula.
tag = section * 1000 + row;
later in your IBAction of switch you can find out the indexpath (section and row) using following:
section = tag/1000;
row = tag%1000;
If you have no idea of how many rows your section will have you can find out the cell using sender.superview.superview (be careful if have added any other views in hierarchy).
Rory McKinnel's answer is still the cleanest solution for your problem.

Capture the index value of a cell but only from the visible cells on the screen

I am trying to trigger some code based on if the cell I selected becomes the first or last cell on the users screen. I'm not trying to capture the index value of the data array. Just the index value of the cell that is visible on the screen. I'm sure this to create an array of visible cells.
NSArray *indexPathsForVisibleRows = [myTableView
indexPathsForVisibleRows];
But I keep hitting a dead end trying to then capture an index value based on that array.
I tried to use a CGPoint and convert that, but I keep getting an error. Any insight would be most helpful!
As per the documentation for the return value of that method:
An array of NSIndexPath objects each representing a row
index and section index that together identify a visible row in the
table view. Returns nil if no rows are visible.
The array returned from that method contains NSIndexPaths.
NSArray *indexPathsForVisibleRows = [myTableView indexPathsForVisibleRows];
for(NSIndexPath *eachIndexPath in indexPathsForVisibleRows)
{
NSInteger row = eachIndexPath.row;
NSInteger section = eachIndexPath.section;
UITableViewCell *cell = [myTableView cellForRowAtIndexPath:eachIndexPath];
if(cell.isSelected)
{
// this is our selected cell
}
}

How to set random image in TableView Cell

I would like to randomly show 5 different images in basic table view cells. I am using dynamic cells. Anyone have idea how to do it?
Thank you very much!
First create an array with your image names:
NSMutableArray *array = [[NSMutableArray alloc]initWithObjects:#"1.png",#"2.png",#"3.png",#"4.png",#"5.png",nil];
Next in your cellForRowAtIndexPath:
int random = arc4random() % array.count;
cell.imageView.image = [UImage imageWithName:[array objectAtIndex:random]];
Create a cell
Generate random number from 1 to 5
Retrieve image with this index and insert into cell
Return cell
Jokes aside, your question is way too generic. You need to show us what have you tried and where exactly you got stuck.

Retrieving label text from a custom static cell in multi-section UITableView

I have a UITableView with 6 sections and custom static cells in each - all created within IB (using Storyboard).
Three of these sections have multiple cells with nothing more than a label in them. The user will select one or more of the cells in each of these 3 sections. I want to capture the text of each selected cell's label.
I have used [self.tableview indexPathsForSelectedRows] to create an array of indexPaths. I can get the selected section and row by iterating through the array with indexPath.section and indexPath.row.
I do not know how to get the label in the selected cell. Can this be done or should I just create another array with all possible text items and use the indexPath.section and indexPath.row to pick out the desired text?
Can't you just do the following for each indexPath?
UITableViewCell* cell = [tableView cellForRowAtIndexPath:myIndexPath];
NSString* myText = cell.textLabel.text;

Resources