I have a UICollectionView which allows for a user to select a cell and upon doing so view 'A' will appear. I am wondering if it is possible for the subviews of this cell, ex: UIlabel and UIImageView to provide a different functionality for when they alone are selected. For example, if the UIImageView is selected, I want to segue to view 'B' as opposed to 'A'.
I have attempted to implement a UITapGestureRecognizer for both the label and the image, however, the cell's functionality overrules and the resulting view is still 'A'. Any ideas?
Thank you in advance.
What you want to achieve is possible through delegates if you don't have custom cell make a custom cell class then inside custom cell declare your protocol
I assume you have to disable the default behavior of the collection view cells:
cell.selectionStyle = UITableViewCellSelectionStyle.none
However, if you set that and you encounter an overlap issue, please take a look at the 'cancelTouchesInView' property of the 'UITapGestureRecognizer'. Basically, by setting that to false, you allow the children to also receive touch actions.
Furthermore, do not forget each gesture recognizer should have it's own method for you to be able to segue into two different places.
Related
Im facing a problem with adding a UIImageView to a single UITableViewCell. I add the subview like this in the cellForIndexPath delegate method which is ONLY added to the cell if the self.mediaTypeArray contains the string: "Image" at the index: indexPath.row
let cell = self.timelineTableView.dequeueReusableCellWithIdentifier( NSStringFromClass(ClassicCaseCell), forIndexPath: indexPath) as? ClassicCaseCell
if self.mediaTypeArray[indexPath.row] == "Image" {
let imageView = UIImageView()
imageView.frame.size = (cell?.evidenceView.frame.size)!
imageView.frame.origin = CGPointZero
imageView.image = img
cell?.evidenceView.addSubview(imageView)
}
Which it works great however, I find that every other two cells contains that same imageView even when self.mediaTypeArray[indexPath.row] is NOT equal to "Image" and I don't understand why. I think it may have something to do with reusable tableviewcells but I still don't see why it would do this. Please help!
I'm getting the feeling that this is because of cells being re-used.
A way you can work around this is to override your ClassicCaseCell's prepareForReuse method to remove any image view from its evidenceView.
I would however recommend that you don't add the image view in this fashion (through the delegate's cellForIndexPath method). Instead, your ClassicCaseCell should hold the image view as an instance variable which you can then set in cellForIndexPath. This way, there is only at most one. You can also make sure to set the image view to be nil in prepareForReuse, making sure that it won't appear in the cell if it is not set.
First, don't use tag property as recommended elsewhere. That was a technique used a long time ago, but Apple discourages that practice now. Second, I'd suggest you simplify your life and simply don't programmatically add image views in cellForRowAtIndexPath. If you programmatically add image views, cell reuse introduces a clumsy process of determining whether (a) you need an image view; (b) whether there is an existing image view; and (c) possibly adding/removing image view and/or getting reference to existing one.
One very simple solution is to just have two cell prototypes, one with an image view and another without. Then, based upon the media type, dequeue a cell with the appropriate storyboard identifier and use it.
The other alternative is to have the image view in the cell regardless, and hide/show it as appropriate. The challenge then becomes how to best manage two sets of the constraints, one for when the image view is visible and one when it's not. You can do this with judicious choice of constraint priorities, activating/deactivating the appropriate constraints in cellForRowAtIndexPath, etc. It can be done, but this is more cumbersome than the above approach, whereby you just employ two cell prototypes.
You only need to add the UIImageView once so if the cell is re-used again, it (might) already be there. Your problem is to detect if you've already added it or not. Here are a couple suggestions:
1) ALWAYS create it (and just don't set the image, or hide it)
2) assign it a unique tag and look for the tag when you need to set it... no tag, then create it
Override prepareForReuse delegation in your tableview cell and remove imageview from there
What is the logic i should follow to integrate a like button in a tableview cell?
How do you update a text label inside the cell signaling how many likes it has in real time IE: when you click the like button it either adds a like or removes a like?
The button also is highlighted when current_like = true
and not highlighted when current_like = false
Where do I update that kind of stuff?
How can you update a cells label and display the new label from within the cell? or is it NECESSARY to reload cell for row at Index Path?
The two main problems/steps you have to achieve are:
- Update the label with the new like
- Update the datasource of the table to keep the data persistent.
So, what I would make:
Set your custom UITableViewCell as target for the Button, so the cell can know when the button was clicked. In the target function/selector you should update the label.
Now, you have to inform the datasource of your table that the cell has a new like. You can create a protocol in UITableViewCell and set the TableDataSource as its delegate. Then, when the button was clicked you can notify the delegate.
You can achieve the same behavior with NSNotificationCenter, instead delegation.
Regards ;)
To change the cell's content without reloading you need to create a pointer to that cell. You can change your cell's parameters directly using a pointer without reloading cell. So it would be something like
self.myCell.label.text = something
And to assign the pointer to your cell you must put something like this in your cell adding method:
self.myCell = yourLikeCounterCell
Hi, I am new to iOS and i am trying to have two text fields inside a custom table view cell and the cells are dynamic, so I will be having two text fields in each cell and when a cell is selected the cell will expand
How can I reposition the text field when the cell expands? One text field should be on the top and the other one on the bottom inside the tableview cell.
How can i access the uitext field delegate inside uitable view delegate
i.e.:
textFieldShouldBeginEditing inside tableView:didDeselectRowAtIndexPath:indexPath
Thanks in advance
I would suggest having two different custom UITableViewCells - one for viewing and one for editing. When didSelectRowAtIndexPath is called, replace the selected cell with the second custom cell designed for editing. When the user is finished, replace it again with the original custom cell type.
For your second question about accessing subviews of the UITableViewCell, you have several options. I'm going to assume you're using storyboards.
If you created a custom class for your UITableViewCells, you can add the UITextView as a class property and connect it as a outlet from your storyboard to your class by making it an IBOutlet. That way you can access it via self.nameOfTextView.
If you don't want to create a custom class for your UITableViewCells, you can assign the subviews tags and access them via (UITextView *)[cell.contentView viewWithTag:1]. This second option can also be used if you aren't using storyboards.
I have a Static TableViewCells on a storyboard. There are just labels in those cells. I would like to fire IBAction event upon touch. What I am doing right now is create a full-size white button and linked to IBAction. But it doesn't show highlighted color when the cell is selected because cell is behind the button.
UITableViewCells have a delegate method specifically for handling row touches -tableView:didSelectRowAtIndexPath:
If you need a custom button over each cell, subclass UITableViewCell instead of going through a storyboard. As much as they may be a timesaver, storyboards are really a limiting factor when it comes to nitty gritty iOS programming.
Try adding this in your cellForRowAtIndexPath (when you initialize each cell):
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
Does this fix it?
I created a Custom UITableCellView for my application. Each cell has two buttons. The problem I am facing is that the cell itself is selectable which leads to a confused user and poor design. How do I disable the interaction for the cell but keep the interaction enabled for the UIButtons in the cell?
Thanks!
Satyam
You can set the cell.selectionStyle = UITableViewCellSelectionStyleNone; and be sure to keep your didSelectRowAtIndexPath unimplemented. That means that your cells will not be interactive, but you can still get messages from your buttons.
Apart from that, of course I don't know what you're trying to achieve, but remember that you can use a UITableViewCellAccessoryDetailDisclosureButton as an accessory view, which is a different "button" from the cell itself. More info here.