How to add a cell to a UITableView? - iphone

I want to be able to add 1 cell to the top of a UITableView after the user clicks on a button. How can I achieve this?
Update:
I also want to hide the cell if the user clicks another button

You can either call UITableView's insertRowsAtIndexPaths:withRowAnimation: to animate the insertion. Otherwise, you can just update your datasource without calling this method.

In order to do this you have to use the Navigation Controller, this is because once the user clicks the button your are suppose to send the user to a new view that edits and adds the new cell to the UITableView.

Related

How to add an "Add Button"- Button

I'm looking to make a button which creates new buttons.
So there are already 3 buttons created by me with ViewController.
And on the bottom should be a "create new button" button. So the user can create as many buttons as he wishes. And every new button should be stacked under each other.
https://imgur.com/ttDZQU1
Here is how I build it on ViewController.
You have some ways to achieving this result. You can create a stack view and push the buttons to it. You can programatically add buttons to the screen by creating new UIButtons every time the users click the add button, or you can create a table view, and add a new cell every time the user clicks it. This last approach already supports scrolling (For when your user clicks tens of times, you'll need to able to scroll the screen so they can see the buttons outside the visible frame). In case you choose to go with the first or second, don't forget to add an UIScrollView to your View.
The steps to get this result with a tableview (3rd approach are):
Create a UITableView in the view controller's storyboard.
Set the UITableView datasource and delegate to the view controller
Create a UITableViewCell inside the UITableView, and set its identifier to something related to cell, like "buttonCell", for example.
Add a UIButton to this newly created UITableViewCell, and pin the constraints accordingly.
in the View Controller .swift file, create an outlet for the UITableView
in the tableView delegate cellForRowAt, don't forget to use the right identifier (in our example, "buttonCell").
if you get lost somewhere in the steps listed above, check some tutorials to create a tableview, such as: https://www.ralfebert.de/ios-examples/uikit/uitableviewcontroller/

Swift Add custom cell on button tap

I have a table view with one custom cell that I have already built and hooked up. When the user taps the add cell button I want it to add a different custom cell. I don't know where to start this process. Do I build it on the storyboard then hide it? Or what is the best means?
You should create all of your custom cells in the storyboard, and then implement UITableViewDataSource methods, where you return whatever cells you want.
Check out the documentation

how to delete tablerow using uiactionsheet?

I have a tableview of items and when i click one row, I use uiactionsheet with 3 button: edit, remove and cancel. When I click button edit, I will open a modal view, so how can I do this ? what is the code to delete tablerow ?
If you want to delete a row from a UITableView, use the method named deleteRowsAtIndexPaths:withRowAnimation:. You can find all the details on the UITableView Class Reference page.
Also don't forget to remove the corresponding item from your model!
UIActionSheet has a delegate property. Add the UIActionSheetDelegate protocol to your view controller and set yourself as the delegate to the action sheet before you display it.
The action sheet will call
– actionSheet:clickedButtonAtIndex:
on it's delegate when the user selects and action.
In your implementation of this, you can do what you want, such as delete the row as fguchelaar described.
This method doesn't directly know which row the action sheet was called for, so you can either subclass UIActionSheet so it can store the indexPath, store the indexPath in your viewController, or pass the information in some other way.

creating a cell for selecting an item

so i want to achieve something like this:
when clicking on this cell, there should pop a new tableview with the items availible, by clicking one of these items, it should go back and update the cell. normal stuff i think.
but i'm not shure how to do it the best way.
first i need to subclass a uitableview cell , because there is no default one for this, right?
and the rest?
should i set an ivar to the new popped tableviewcontroller with the selected cell and update the content after an item was selected? but then i had to reload table data , don't i ? wouldn't this break my selection, the scrolled way and all this stuff? would be a bit weird while the navigation-controller goes back to this tableview.
please help me with some best practices for this.
thanks and please leave a comment if something is unclear.
That cell style is UITableViewCellStyleValue1.
I would write a custom delegate protocol that the parent controller implements so that the child controller can inform it when the user has made the selection. But you can also use a property on the child controller. Or use a notification.
To update the cell in the parent view controller, just call [tableView reloadRowsAtIndexPaths:withRowAnimation:]. No need to reload the entire table.

How can I put an editable table view in a tab bar application?

I am starting out with the tab bar application in XCode and I want to put a table view in one of the tabs. I know how to physically put the table view into a tab with interface builder, but I need to be able to edit the data in the table, so I'm not just left with blank cells.
So, how can I edit the data in the table?
Essentially, I want to put a navigation-based application inside the tab of a tab bar application.
Thanks for the help!
UITableViewCells don't, by themselves, support the ability for the user to edit their content. You can set up your UI to allow users to do so, but it'll take a little extra effort.
If what you're really looking for is for the user to be able to enter text into a table cell, I would add an Edit button to your text cells, so that the user can tap it to go into edit mode for a cell.
When a cell goes into edit mode, add a UITextField to the cell view and call its -makeFirstResponder method to bring up the keyboard.
When the user taps the Done button on the keyboard, call -resignFirstResponder on the text field to dismiss the keyboard, then update your table view's data source object (this is the object you've assigned to the UITableView's dataSource property) with the string from the text field's text property and remove the UITextField from your table cell and reload the table's data by calling its reloadData method. Or if you are keeping a reference to the edited table cell somewhere, you could just update the cell object directly instead of calling reloadData on the table.
You can't just add text to table cells in InterfaceBuilder. You'll need to hook your UITableView to a UITableViewDataSource, and have that data source provide the cells you want your table to display.
Here's a great starting point: http://developer.apple.com/library/ios/#documentation/UserExperience/Conceptual/TableView_iPhone/TableViewAPIOverview/TableViewAPIOverview.html%23//apple_ref/doc/uid/TP40007451-CH4-SW2
Give this a shot http://www.amasso.info/?p=77