How to set random image in TableView Cell - ios

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.

Related

how could i detect in a UITableView, what are the cells deployed in my phone screen

I am trying to figure out how to detect which UITableViewCell in a UITableView are deployed in my phone screen.
For example: My numberOfRowsInSection are 20 but i see just 3 of them on the phone screen. I want to know how do determine which three are shown?
In my case, I need to send only the publicity that the users viewed in your phone screen and not the 20 rows loaded in the UITableView.
Thanks!
let visibleIndexPaths = tableview.indexPathsForVisibleRows
var visibleCellsArray: [UITableViewCell] = []
for currentIndextPath in visibleIndexPaths! {
// You now have visible cells in visibleCellsArray
visibleCellsArray.append(tableview.cellForRowAtIndexPath(currentIndextPath)!)
}
You now have visible cells in visibleCellsArray.
Your cellForRowAtIndexPath method calls for the number of visible cells only.IF YOU DEQUEUE THEM.
If you want to do something based on your visible cell, either put some logic in cellForRowAtIndexPath or use indexPathsForVisibleRows which will give you back an array of visible cells and you can go on with your logic.
let visibleIndexPaths = self.tableView.indexPathsForVisibleRows() as? [NSIndexPath]
This gives the array of indexPath that the cell are in visible..
i hope this will help you...
To determine the NSIndexPath values for the rows displayed on the screen, consider the answer to this related SO question.
Specifically, to get an array of the visible table cells, use:
self.tableView.indexPathsForVisibleRows

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
}
}

Determine row and column of UICollectionViewCell

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;

How to access first and last cells of UITableView

I have a UITableView with custom cells,, I want to set background images of those custom cells. The background image of first and the last cell of the UITableView should be different than other cells.
I have used dequeueReusableCellWithIdentifier: to create the cell.
to set the background images of the first cell, I wrote
`
if (indexPath.row==0) {
cell.cellBackgrounfImage.image=[UIImage imageNamed:#"TopCellImage.png"];
}
`
1) Because of the reusing cell,,, this is going to set middle cell image rarther than the first cell. How can I overcome this problem
2) How to access the last cell of the UITableView to set other image
Please anyone give me a solutions for these 2 questions.
Thanks
For Step 1)
When you are scrolling tableview, it will be always reload data. So may be it will generate background image for middle one.
But, if you are set indexpath.row == 0 condition, then it will not affect any row instead of first one.
For Step 2)
You can get values for no of arrays (Array Count).
suppose int totalArr = [arr count];
Then apply condition same as first row by,
if(indexpath.row == (totalArr - 1) {
cell.cellBackgrounfImage.image=[UIImage imageNamed:#"BottomCellImage.png"]; }
So, it will work for last one also.
Thanks.

how to put different cell values if I have more than one sections?

I have two sections in my UITableView, in one view I want to show data from one NSMutableArray say AArray and in other sections I want to show the data from other NSMutableArray say BArray
how can i achieve such concept?
You look at the index path.
if (indexPath.section == 0)
// Use array A
else
// Use array B

Resources