Check if image is empty for reused cells - ios

Each cell has an image.
I want to be able to check if the image is empty.
I am using tableView's reuseIdentifier.
I have tried to do the following
if (!cell.imageView.image) {
}
and
if (CGSizeEqualToSize(cell.imageView.image.size, CGSizeZero)) {
}
But none of these approaches work, as the cell thinks the image exists, because previous cell's images exist (due to reused cells)
How do I get around this problem? How do I check if a cell's image is empty or not in a reused cell?

Technically if the cell is reused, the image won't be nil (unless you didn't set it). So if you want to not reuse the images from your cells you can use -prepareForReuse method from UITableViewCell, this method is called right before the cell is reused, so in this method you can make the image nil.

Related

Edit only the first cell in a UITableView

I am trying to add a gradient to the first cell in my UITableView.
Here is the if statement I use to determine if the cell is the first in the table.
if (indexPath.row == 0 && indexPath.section == 0) {
This code does not seem to be working as there are a few cells with the gradient, notably the first and last cell along with sometimes the second cell. This is rather odd as the information I am grabbing in the array using indexPath.row is getting the information from the correct position.
Why is my if statement not working? Even though I am grabbing information from the correct location in the array.
You are probably not implementing prepareForReuse in your custom UITableViewCell subclass. It looks like you are correctly targeting the first cell, so perhaps when that cell is later reused for other index paths you aren't clearing the gradient you added earlier.
Make sure you have an else condition to go along with your if. Because table view cells are reused, you need to make sure to remove the gradient if it is in a cell other than the first one.
if(first cell)
add gradient
else
remove gradient if it exists
If you're using custom cells, set the custom cell gradient variable to clear colour on all but the first cell.
If you're using standard uitableview cells, add the gradient to every cell as default and then if it's your first cell change the gradient colours to whatever you need it to be, and clear colour as default.
Alternatively clean all subviews from the cell after allocating it like this:
for (UIView * sub in cell.subviews){
[sub removeFromSuperview];
}
Then add your gradient for the first cell.
Option 1 is the cleanest.

iOS (Swift) - How to change any graphic (layout, size of UIView and etc.) in reusable UITableViewCell

The situation is pretty simple (as pretty common as I thought). I have an UITableView in my application that has lets say 3 rows (all are visible on the screen). Next I tap on one of them and this action changes something in all three cells (e.g. change scale of some UIView that is in this every cell) and everything is ok.
This is ok until I have only visible cells on the screen. I know that cellForRowAtIndexPath works only for visible cells. So how can we change such kind of properties (not data properties) for all the cells?
cellForRowAtIndexPath is where you prepare the cell for display. You should be doing the changes in here.
How are you modifying the view scale inside the cells when the user taps on a cell?
You should be triggering a reloadData() on the tableview which will reload the tableview and trigger the cellForRowAtIndexPath calls. You can also reload specific indexes inside the tableview if needed.
Inside your cellForRowAtIndexPath you should use some logic or state variable to control how you configure your cell. Dont dump all the code inside that function, pass arguments to your cell in a function e.g configureCell(subviewScale: CGFloat) and let the cell configure itself.
Dont forget to reset the scale of the subview in prepareForReuse() on your custom cell so that when the cell is about to be dequeued and reused, it gets its properties reset.

Dequeued UICollectionViewCell not refreshed

I have an application with a UICollectionView with horizontal scroll of cells (Only one cell is displayed at a time). Each cell hosts a UITableView which displays content (URL links) in each of the UITableViewCell.
The problem is that the first two cells of UICollectionView display the proper content, but starting from the third cell, the contents are displayed from the dequeued cells. The correct content on the dequeued cell's UITableView is only displayed if user scrolls down and then scrolls up to the top.
For example, the contents of the third cell are the content from the first cell.
The cell is a custom UICollectionViewCell that hosts the UITableView. The custom cell implements the UITableViewDataSource, UITableViewDelegate.
I have searched through different forums and sites and found the solution of refresh.
I tried adding the [collectionView reload] to the -cellForItemAtIndexPath: method, but it goes into constant loop. I know that is not the right place, but i do not have any other methods that cause the refresh (only scrolling through the cells loads the next cell's content).
Can somebody please help me to resolve the issue?
You are missing some facts about dequeuing.
When you 3d custom UICollectionViewCell is dequeued, it isn't allocated like when it's displayed for the first time. Instead, your collection view "reuses" 1st cell for displaying 3d. The best moment to clear content in your custom cell class is -(void)prepareForReuse. If you override it and clear it's content, the -cellForItemAtIndexPath: will dequeue "clear" cell which you can further customize.
However, i don't exactly know how to "clear" the tableView, not sure whether it's possible. You have to reloadData on your tableView in -cellForItemAtIndexPath:. Example:
- (UICollectionViewCell*)collectionView:(UICollectionView*)collectionView cellForItemAtIndexPath:(NSIndexPath*)indexPath
{
MyCVCell *cell = [collectionView dequeueReusableCellWithIdentifier:#"MyCVCell" forIndexPath:indexPath];
cell.arrayOfURLsForDisplay = self.arrayOfArraysOfURLs[indexPath.row];
[cell.tableView reloadData];
return cell;
}
Here i mean you have some arrayOfArraysOfURLs in your viewController and you dislay URLs in your custom cell from some arrayOfURLsForDisplay
I had similar problem, instead of using [cell.tableView reloadData] inside the cellForItemAtIndexPath, I called collectionView.reloadData in the prepareForUse method that is overwritten in your custom UITableviewCell

How to avoid cells background image from hiding the separator line between rows

In my table view I am using custom image for the UITableView cell's background. It works perfectly except for the fact it hides the row separator. And now there is no row visible.
Is there a way to increase the distance so that cells will get separated.
Please help!!!
Thanks,
Without seeing what you are seeing, it's a bit hard to tell. That said, you could try adding the separator to the image so that it shows up in the cell.
If you want to change the height of the cell, implement the -tableView:heightForRowAtIndexPath: method in the table view delegate. Just return the correct height for the row at indexPath. I believe there is also a way to set the rowHeight in the table view properties in interface builder if all cells will be the same, but I tend to stay away from IB when possible.
As far as my knowledge you can add a line image to the cell as a footer.So that you can see the separator
I believe you have to override the -setFrame: method in your custom cell and return the same frame with height minus 1 pixel.
Also be sure to call [super layoutSubviews]; in your -layoutSubviews method if you have overridden this method with some custom implementation.
LineSeparator may be invisible to you because of your background image in your cell. Try to change the style of your separator.
Alternative Way:
Otherwise You have to add a one pixel line to the bottom of your Custom cell background image by your designer and if it's still not visible in your tableView, then you have to increase a height for your cell in heightForRowAtIndexPath: method.
Make sure your view if you have one that is inside your Cell is exactly the same size as your cell's size.
Make sure:
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
is setting your cell heights correctly for all cells.

Why is my UITableViewCell imageView image disappearing?

I have an image in a cell's imageView, but If I scroll the uitableview up so that that particular cell goes off of the screen, then let it come back down, the imageView disappears.
How can I fix this?
The left column is the tableview untouched, the middle column is the tableview pulled up, the right column is the tableview released
As Kevin mentioned, your problem may be in the way you set the image. Are you creating an imageView and assigning it to the cell's imageView property or the image inside the property? without looking at the code, I would suggest checking what happens when the cell is created. in the cell == nil case. Hope it helps.

Resources