Passing an index path through an action sheet - iphone

I have a table view controller and when you select a row an action sheet appears. Then the user would select an option, followed by -(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex been called. But by that point I don't know which table view cell was selected to bring up the action sheet in the first place. Is there a way to pass an index path through an action sheet?

As UIActionsheet is child of UITableView you can use tag property to store the indexPath.Row.
I am assuming that your table has only one section. But if your table has more than one section then you can either make 2 class level variable to preserve the indexPath.row and indexPath.column OR you can customize UIActionSheet and assign respective value to properties of your action sheet class.
customizing action sheet will help you even when your tableViewDelegate and action sheet delegate are different.
Please post if you need help in writing code.

Save the index path while displaying action sheet and then click on clickedButtonAtIndex of your action sheet
[yourTableView selectRowAtIndexPath:savedIndexPath animated:YES scrollPosition:UITableViewScrollPositionBottom];
which will select your row.
Here savedIndexPath is your last selection of your table view

Related

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.

Trouble adding cells to a tableView

I have a page with a button on it. When the button is pressed, i want the table view on the next page to add a cell with a label that has the text of 1 and i want there to be a picture also, but i know how to do this. I just want to add a cell to a table view when a button is pressed on the page before the table. How can i do this? Ive tried this code:
FinalCartViewController * viewController = (FinalCartViewController
*)self.parentViewController;
custom.customCellLabel.text = #"1";
[viewController addObject:[NSMutableDictionary dictionaryWithObject:
custom.customCellLabel.text forKey:#"name"]];
custom is an ivar that is created from the FinalCartViewController.
This code was in the method for the button that i want to use to add the cell. And i used [myTalbleView reloadData]; in the tableView's viewDidLoad. But my app crashes when i press the button to add the cell. Could somebody please help me? Thanks.
FinalCartViewController * viewController = (FinalCartViewController
*)self.parentViewController;
If the FinalCartViewController (I assume is the Table View) is the NEXT page, how can it be the PARENT view? The Parent view refers to the previous view.
You need to initialise the FinalCartViewController and save reference to it, so you can add objects to it. Although, the better practice would be to have a shared datasource that you add to and then pass to the FinalCartViewController.

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

How to add a cell to a UITableView?

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.

UINavigationController Adding Rows Implementation

I was wondering if with a UINavigationController, can you have a "+" sign, like the Contacts app, that adds a row to the main root view and has a default name as "Setup", then you can click on that row to go to one level below, change a value in a UIPickerview in the one level below UIViewController, and then press the back button and have that value from the UIPickerView be the name of the new row that was created?
Yes (though it's not very Apple-like UI behavior!)
You'd have to put some special logic in your table view data source to add the "Setup" row and go to the picker when that row was selected. The root view controller should implement the delegate from the picker to receive the new row's name, then call [self.tableView reloadData] to update the view.
A better practice would be to model the Contacts app and let the "+" button present a modal view controller from the bottom for your picker view. Look at the addButton implementation in SQLiteBooks or CoreDataBooks.