How to Use Custom UIButton in UITableViewCell? - ios

I am trying to figure out the best way to apply UIButtons in tableViewCell, I've used tag method. like cell.customButton.tag = indexPath.row, then giving it a target.
But it din't work fine for me if the tableViewCell has may things to show with the same button in all the cells.
There are two options to show the title of the button , just like if we follow a user and unfollow, so after deleting or clicking on any button , it changes the title of some other button also , So, tag method isn't a good option I guess.. Any help would be appreciated.. Thanks!

Using a protocol in a custom table view cell class is what i would suggest:
check out this gitHub commit or download zip for the full project
Adding a Subclass and protocol to a UITableViewCell
I shall have this in the form of an answer later. let me know if you have any questions about the project

Related

How to have DidSelectRow activate keyboard?

Goal
I want to have a keyboard (custom) to show when I click on a cell in my tableview and have said keyboard edit a label in the selected cell.
What I have read and tried
Stack overflow and other searched threads/tutorials
A Swift example of Custom Views for Data Input (custom in-app keyboard)
How to make custom keyboard only for my app in Swift?
iOS 8: Creating a Custom Keyboard in Swift
Along with other results and searches (also the recommended readings within these threads), these were great for me getting the keyboard the way I want it (which is app-specific, I don't want to have the user install the keyboard to use the app), however, none explain how I could "activate" the keyboard without a textfield.
My thought process is this: I will have a keyboard with a textfield in place in order to receive the input from the keys pressed. This input would then be sent to the label that is in the selected cell. The problem is in the keyboard showing without anything to call it...
Why I don't want a textfield in the cell: Because I think it is more smart/elegant to have the tableview cell activate the keyboard. I have seen this in other apps and I can't figure out how it is done.
Any help on this matter is much appreciated. I am completely new to programming, am using Swift (so have no clue Obj-C or the like).
Thank you!
Well after a lot of discussions around other websites, it is a shame to say it but what I wanted was actually impossible (like stated by Duncan). For some reason I thought that in programming that word did not exist, but alas it is there. Well, the way that I did work around this limitation was as replied here in the comments as well as from the resource materials I already read:
I have 2 views:
tableView - this will have the list of items that I would like to edit a value. Has multiple labels where one of them would be edited by the user.
keyboardView - custom keyboard that would allow user input.
For the keyboard I used the delegate method as described in "A Swift example of Custom Views for Data Input". This worked perfectly and would send the data over to the tableView under the keyWasTapped function.
Next was the tableView. I had 3 labels in the custom cell, where one was called valueLabel. I had to add a textField to my storyboard hidden behind the tableview with which the user will interact and in the didselectrow I included the command to summon the keyboard:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
textField.becomeFirstResponder()
}
From there I would grab the text from the textfield and update the UILabels in my custom cell. At least this way I can select a cell and not the textfield. So considering that the answer that would fit what I wanted is combination of the replies, I thought it would be best to say it here.
Thanks to Sandeep and Larme for their time.
TVDN,
Have a textField in cell not the label :) Create a custom cell create an IBOutlet to that class ffrom textField in cellForRowAtIndexPath
let tableCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as! myCell
tableCell.yourTextField.resignFirstResponder()
and in didSelectRowAtIndexPath
let tableCell = self.tableView.cellForRowAtIndexPath(indexPath)
self.tableView.dequeueReusableCellWithIdentifier("cell") as! myCell
tableCell.yourTextField.becomeFirstResponder()

How to make a menu (multiple button?) iOs

I am new in the development of iOS with swift, for the moment everything goes well but there is one thing that I blocked.
How this kind of menu?
How do you call this ? Because I do not even know what to look for because I do not know his name: /
Sorry for my bad english and thank you for your help :)
This can be made by using click on button open a view or open a tableview inside the table view put the label on each row and after that use didselectrowatindexpath method of tableview for click on each row open the new page or viewcontroller.
You can use table view to show dropdown menu. Show tableview on click and hide when select row. this is the basic concept. and if you not want to code your self then there many third party libraries available on github like DropDownMenu or dropdownment etc.
Note : standard way in ios for this kind of stuff(drop down or selection from multiple option) is UIPickerview, so you can use this also it very easy. so if not required to use hardcoded dropdown then pickerview is best option.
hope this will help :)

How to enable both swipe to delete and a delete button made in edit mode in UITableView

By default edit mode is on, that's why swipe on cell, it will delete like this, and when click on edit following method use to delete the data
Right now it works correctly but either one of them is going to use. I want to use both method for delete cell.
Please help me to find some code snippet or tutorial link
The natural iOS behaviour for Edit mode in a UITableView is just showing the '-' button for deletion, thats the way I recommend you to use, because of the users are use to deleting on that way.
With this library, you can assign custom buttons and actions for both Swipe directions. Maybe with it you can achieve what you want.
https://github.com/MortimerGoro/MGSwipeTableCell
I would try to add a custom right UIBarButton with a custom action, and after it get pressed, you can try to force swiping the cell to both directions, and showing the buttons you want.

Add an action to a custom UITableViewCell

I am building a simple app with a table view filled with custom view cells and using a storyboard. I want to add a actions on the cell each time the user tap it.
So far, I tried to create an IBOutle to link my cell to my tableViewController and add the action manually in the code but each time I try to do it I get an error message saying "Illegal Configuration - cannot have a prototype object as its destination".
The only quick fix I found is to add a UIButton with a transparent background and no title which fills in the whole cell and attached the action to it.
Is there any more elegant way to do it?
Not only is there a more "elegant" solution, but there is also a correct way to do this.You should be using the didSelectRowAtIndexPath method for your cells.
You should read what the Apple Docs have to say
And also, here is a tutorial on how to use row selection in your tableView.
The elegant solution is to use your table view's delegate method tableView:didSelectRowAtIndexPath: and put all your action code there.

TableView editing style

I've got a tableView with entries which the user should be able to edit. The special thing is, the tableView has a "right detail" style (a number) and I want this right detail to be replaced (animated if possible) with the standard disclosure indicator accessory, when the user hits edit.
How can I do this? Thank you!
Update: Thank you for the answers so far but could you maybe give me a code example for the part where the number gets replaced by the picture? Thank you!
You can make use of the UITableViewDelegate methods for editing such as tableView:willBeginEditingRowAtIndexPath: to perform your changes. Use cellForRowAtIndexPath: to get the UITableViewCell object.
You can use a custom cell here and do all those works. Here you can use the same cell and add a subview to that cell where you want that change to happen. Later depending on the state i.e; isEditing = True/ false you can display what you want.

Resources