I'm trying to change the text of a UILabel in a UICollectionViewCell after the UICollectionViewCell has loaded, when tapping a button. But the label doesn't update on the screen. The console shows that the text property of the label is updated, but it seems the label isn't redrawn with the new text.
Is this a known problem with UICollectionViewCells? Some kind of caching issue, perhaps? Do I need to reload the cell, or the entire collection view for the update to show?
I had a similar situation where a UILabel in the collectionView cell was updating fine. Then after making some modifications to the storyboard, the UILabel would not change when updating the value programmatically. As noted in the initial question the value stayed the default value even though the debug terminal showed that the connected UILabel attribute text was being updated.
Here is how I resolved the issue. In the storyboard,
Deleted the referencing outlet for the UILabel (in the "Show the connections Inspector")
Deleted the UILabel from the storyboard
Re-added the UILabel
Reconnected the referencing outlet to the swift file code attribute
And somehow, everything started working fine again.
As a side note: The swift 3.1 versions of Radwa's answer are:
collectionView.reloadItems(at: [indexPath]) // single cell in the collection view
collectionView.reloadData() // entire collection view
Because the cell is already loaded, no change in cell will happen until it's reloaded, so you can either reload the entire collection view
[self.collectionView reloadData];
Or just one/multiple cell(s) that got affected with that change of data
[self.collectionView reloadItemsAtIndexPaths: indexpathArray];
But make sure that you change the data properly before reloading the cells
Related
I am using a UISearchController in my application following this tutorial:
Now having set up a custom UITableViewCell. The UISearchController messes with my cells.
It seems like the UISearchController is not using the same custom UITableViewCell and it's view bounds differ from my own UITableView. Is there a way to change this back, when I press the cancel button?
Edit:
Though, nobody has answered, I found a solution:
make sure, your cell is connected to the actual tableView, not your primary only. ( I have used an Outlet and set only the cells of my outlet )
make sure you ALWAYS address the outlets in your custom cell. Since I used this
cell.textLabel?.text = "Hello"
my cell will automatically be cast to the default cell.
I seem to be having a very strange problem with my UITableview cells and autolayout. So my tableview is set up as follows:
I have a UITableView within a UIViewController that I created via Interface builder using Autolayout
My tableview is using UITableViewAutomaticDimension so that the cells can resize based on the text within them.
Each cell has 2 subviews within it. A front view and a back view. The back view contains the buttons for my slide-able cells
I am using NSFetchedResults controller to update the tableview
The problem occurs when I tap on a cell and am taken to the next view. Then I create a core data item within that view and save so that it is inserted into my tableview (now behind the current view controller).
What happens is that the newly inserted cell seems to lose all autolayout constraints and all view elements are on top of each other in the top left of the cell. It also looks like they are not even contained within the front view for the cell (or the front view has a width and height of 0).
Example:
It gets even stranger. If I refresh the tableview by pulling down and the cells are reloaded it corrects itself. However, if I refresh the tableview again then the problem appears again but on a different cell. This bug does not happen consistantly which is rather frustrating.
Any help would be appreciated as I only have a few remaining hairs on my head to pull out at this stage.
Thanks in advance!
So I found out what the issue was. The issue was that I was using size classes in Interface builder but I was only using the iPhone Portrait size class and did all my work in there instead of the "any" size class. Disabling size classes and setting the project to iPhone only fixed all these problems.
Weird I know
I have a UITableView and one of my table cells is a UICollectionViewController subclass that contains a UICollectionView of displayed email addresses. When a user adds an email the UICollectionView and it’s cell in the table view should get taller.
I’m currently attempting to do this setting my collectionView height constraint to collectionView.contentSize.height in the LayoutSubviews method of my collection controller/cell class. My issue is that the cell in the UITableView is not changing size when this happens.
I am assuming that this is because there is nothing telling the table view that the height of the email entry cell has changed. I am currently using dynamic cell sizing - or trying to anyway. Even, if I call tableView.reloadData() this still does not work. I’m wondering if someone could give me a high-level idea of how they would set this up.
UPDATE:
I had a broken constraint issue that was part of my problem, but this is still not solved. While have proven that I can update the height constraint and that will update the size of the collection view, it's out of sync. It's always one update behind. Here's an image showing the 'UICollectionView' in green and you can see in the logs that I'm updating the constraint (yes, multiple times) each time after adding a new item to the collection, but it is not updating the bounds of the view instance. In this example, if I were to add a new item to the collection, the next time I inspect bounds.height it would be at 149.5. What am I missing!?
i had answered this question in another tableview inside tableview cell that will work for collection view too ,i explained it in this thread
ask me if you faced any problem
It seems you want to set the height of your table view cell dynamically. This detailed tutorial walks you through making a custom cell class that will have dynamic height. Unfortunately, this tutorial only walks you through how to do this with auto layout constraints and a storyboard. In the tutorial you'll notice that the height of the cell depends on the constraints put on a title label within the cell. Try doing the same thing, but applying the constraints on the content in your cell (the collection view cell).
http://www.raywenderlich.com/73602/dynamic-table-view-cell-height-auto-layout
I have a static UITableView with various cells in it. I need to hide/show some of those cells, so I've implemented heightForRowAtIndexPath and return 0 when appropriate in order to hide the right cells. This works great for sighted users, but for those who use VoiceOver those elements are still highlighted and accessible when they should not be. How can I ensure those UITableViewCells are no longer accessible when I change their height to 0?
I've tried setting the cell to not be an accessible element as well as setting the elements to be hidden but this has no effect on it. The cell has not been subclassed - it's just a UITableViewCell. I have not set anything in regards to accessibility on the cell nor the cell's contents (textLabel, detailTextLabel).
Doesn't do the trick:
self.cellToHide.isAccessibilityElement = NO;
self.cellToHide.accessibilityElementsHidden = YES;
Update Cell VoiceOver Elements By Reloading Cell(s)
After reading about a UITableView taking control over Accessibility elements and observing apps that have similar features, I figured that a TableView must update its Accessibility information upon loading or reloading a cell. I tried forcing the cell to reload after changing its Accessibility properties and that solved the problem. VoiceOver information was updated.
The following is an example of code that runs when the cell in question is tapped. Alternatively, it could run when some other event requires that VoiceOver elements are updated.
// Make changes to accessibility properties such as
cell.isAccessibilityElement = false
cell.accessibilityElementsHidden = true
// reloadRows() allows VoiceOver to update its element list for the related cell(s)
// "indexPath" is for the desired row
// reloadRows() expects an array of IndexPaths so an array of one is created inline
tableView.reloadRows(at: [indexPath], with: .automatic)
// Calling UIAccessibilityPostNotification() is not necessary to realize the VoiceOver changes in the TableViewCell
Background
I wrestled with this problem for a while before finding a solution. In my case the TableView cells are created in code. There are no storyboards or nibs involved. However, this solution should work regardless of how the TableView was constructed.
I have custom, subclassed TableView cells with view hierarchies built in code and added as a subview of the UITableViewCell's contentView. I assumed I could modify the isAccessibilityElement and/or accessibilityElementsHidden properties of various subviews and call UIAccessibilityPostNotification() to realize the VoiceOver changes as I have done outside of TableView's. These changes were not recognized by VoiceOver, only the accessibility state the cell was in when it was loaded was recognized.
For the cell I wrestled with, the height dynamically changes to accommodate a DatePicker that is shown and hidden on cell tap. I only want the DatePicker visible to VoiceOver when it is visible on the screen. I try to avoid reloading the TableView, Sections or Rows to make dynamic changes if at all possible. If I have to reload, I try to make it as isolated as possible (reload one cell or one section not the whole TableView). In this case I did not need to reload anything to make the cell expand to reveal the DatePicker so it did not occur to me try reloading the cell for Accessibility updates.
Related Information: UIAccessibility API Reference on Apple's web site
Try adding below code after you set accessibilityElementHidden.
UIAccessibilityPostNotification(UIAccessibilityScreenChangedNotification, yourTableView);
When I am developing iOS8 Today Extension, I set a UITableViewController as the
main view controller.And I met this problem:
When I init the UITableViewCell using initWithStyle:UITableViewCellStyleDefault
in cellForRowAtIndexPath, the whole area of one cell can trigger didSelectRowAtIndexPath
normally. However when set UITableViewCellStyle to UITableViewCellStyleValue1,
UITableViewCellStyleValue2 or UITableViewCellStyleSubtitle, I can only trigger
didSelectRowAtIndexPath by click in detailTextLabel or imageView's area. Click in other blank
areas will get no respond.
I've tried to set tableview's delegate to self, set detailTextLabel and imageView's
setUserInteractionEnabled:false in cellForRowAtIndexPath, and all these do not work. Does
anyone have any idea about this?
P.S. I am not using storyboard or xib file,just code.
I've found a same problem on Android:
I'd like to click anywhere (blank area) in a list view
I encountered this myself while trying to display UITableView inside the TodayView. Although I don't know why is this happening, I found out that this only occurs when the UITableViewCell background is clearColor. If you set the background to some other color, didSelectRowAtIndexPath will be called. However, here's a quick workaround - put UILabel to stretch on whole area of the cell's contentView and then the delegate method will be called every time. Anyway, it seems like a bug.