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.
Related
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.
My UITableViewCell has a cell template which is created from another nib file. The cell has a UIlabel object. Now, once the UITableView has loaded and the text has been displayed, and if I want to change its value by clicking a button from another cell, How should I do it ?
I have updated the text of the UIlabel but how to show it on the screen? Should I reload the entire table? Kindly let me know if there is any good way to do it.
You can use KVO for this purpose. Each cell observes the model, and when it changes, update some fields.
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
I used to use static UITableView but the table is too long and overflow the memory.
I switched a dynamic prototype UITableView with 1 type of cell, which has an UISwitch in it.
One of the cell, when turning on the switch, will turn off the switch of another cell. These cells have fixed index.
The IBAction method is in my UITableViewCell subclass and I don't want to add the UITableView as a property in my UITableViewCell.
How do I achieve the above effect?
I'm planing to use an id or similar to distinguish between the cells as each cell's switch has different effects, that doesn't solve the above requirement.
Thanks,
I would add a block property to your cell which you can use to notify your controller of changes in the switch. See my answer below to a question on this:
How can I get index path of cell on switch change event in section based table view
All your logic can now be implemented in the view controller.
You are best to create a data model in the view controller which the cells simply provide views and controls onto. When you flick one switch and the block fires, update the data model and simply reload the table. Any cells affected will show the new data model positions for their switches. Avoid using one cell to adjust another. Just update the model and reload the cells.
I have a UITableViewController with a few UITableViewCells. Each of those cells have different content: labels, images, text fields, etc. I am needing to set those pieces of content based around user interaction of my UITableViewController class.
For example: I have a cell that has a UIImageView in it. When a user taps the UIImageView I want a UIActionSheet to show. The problem is that I can't set the UIImageView as a property of the UITableViewController class. It has to be a property of a subclass for that UITableViewCell. But that means if I write the code for showing a UIActionSheet in the UITableViewCell subclass it won't show in the UITableViewController class.
My question is, since all of the objects within a UITableViewCell are in their own class, how do I alert the UITableViewController class when one of those actions happens?
Also, when a UIActionSheet is displayed it will need to show up in my UITableViewController class. If I select an item from that action sheet and wanted the text of that item to propagate to setting the text of a UILabel inside of a UITableViewCell, how could I do that?
With UITableViewController you control everything through the UITableViewDataSource implementation.
Here is how you can do it: make an int field in your model class that says which row needs to show a cell with an action sheet, and set it to -1 (which means that no cell needs to show an action sheet). In the tap handler of the UIImageView call the model, and tell it that the cell to which the UIImageView belongs needs an action sheet now. At that point you tell your UITableView to reload data. This sets the whole system in motion again - the table view calls back your data source to ask for the count, and then it calls again for the cell. This is when your data source looks at the model, sees that the row needs an action sheet, and returns a subclass of the cell with the action sheet visible.
Here is a diagram showing this sequence of events.
When the user is done with the action sheet, the action sheet needs to call the model again, and tell it that the action sheet is no longer needed (i.e. set the index back to -1). After that it should call the table view again, and tell it to reload the data. The sequence will repeat again, but is time there will be no flag asking for the action sheet, so a regular cell would be returned.
well, I simply suggest to use Custom UITableView Cell class, so when you are creating custom cell pass your uitableview controller to that class as parent. Now in your Custom Class you can add singleTap on UIImageView, from tap on it you can call method in your parent class via parent object you have already passed.