How to mark multiple UITableViewCells and perform an action on marked cells? - iphone

I would like to do pretty much what the Mail Application does: that when I select Edit, instead of the usual Delete Button, Radio Buttons appear on the side that may be checked by the user, then the user may click on a Button to take an action on the marked cells(any kind of action not just delete). Is there any apple sample code that does this?, can anyone please provide some code or documentation on how to do this?. Thank you.
-Oscar

I haven't done this so all of the following comes straight from the documentation. This is how I would do it:
Overwrite your view controller's setEditing:animated: method to display one or more buttons to execute your batch action (just like Mail.app does) when the table goes into editing mode.
Use a custom UITableViewCell subclass for your cells.
The key is to overwrite willTransitionToState: in your custom cell class. In this method, add a custom subview containing your radio button to the cell.
Overwrite layoutSubviews to position the radio button and the rest of the cell's content in the cell.
In tableView:didSelectRowAtIndexPath:, differentiate between the normal and editing states. If the table is in editing mode and the user taps a cell, mark it as selected (modify your radio button subview accordingly) and keep a record of all marked cells.

Here is a good article about doing a Mail-style multiple selection:
http://cocoawithlove.com/2009/01/multiple-row-selection-and-editing-in.html

Related

How to insert button into Table view cell Swift

How do add button on each cell for table view cell so that it know which one is from.
Example:
Data have 2 products which is product A and product B, then I store them in array which is [product]. Inside the array, there have their detail which are name and price. So, I have successful making these into the table view.
The Question is how to insert button into the table view cell?
Example:
When button pressed, it know which row is pressed which is when press the 2nd row will be product B.
I'm newbie here. Need help, thanks.
You have two options:
Option one - easy, quick, bad:
create instance of the UIButton with the frame to position where you actually want the button to be placed. add tag property as well as the action. Action would be the same for the each button and you could use that tag to do the different actions.
Option two:
Create subclass of your UITableViewCell. Add all the element you want there. Create action for the button and you can set the target UITableViewCell or UITableViewController. If you choose UITableViewCell to be the target you will have to implement delegate, confront that delegate in the UITableViewController and implement that delegate function there. If you choose controller to be the target, you will have to send it to the cell. You will have to set tag as well.
I would use option two because it is scalable, easly maintained, and you have visual of the cell. Adding more components is easier as well.
I don't thing you need to put button or any other control in UITableViewCell to identify the selected cell.
All you need to do, is to implement following delegate method in to your class where you have implemented UITableView
tableView(_:willSelectRowAtIndexPath:)
tableView(_:didSelectRowAtIndexPath:)
and these methods will let you know selected UITableViewCell indexPath and you can do what you want to do on selection.
Note: do not forget to set your UIViewController as delegate to UITableView (almost like you did in case of DataSource).
Here's a Github repo you can refer, the code is not in good shape though.

Hide keyboard plus return key?

I have a view with a textfield at the top, a textview under it and two buttons below the textview.
The keyboard is configured with a "Done" button. Once the user has typed in their info, they click the save button, which is below the textview. First they click Done to hide the keyboard (and reveal the save button) then click the save button.
I need to allow carriage returns in the textview but "Return" is already taken up by Done.
How is it usually handled when you need a Return key and ability to also hide the keyboard?
Drag a button into your view, delete the text, and resize it to take up the entire view. In the document outline, select the new button and drag it to the top of the list of elements. This puts it in the background so it is not hiding the elements of your view.
Add this code to your ViewController:
#IBAction func hideKeyboard(sender: AnyObject) {
self.textField.resignFirstResponder()
}
Link the button to this action and you're all set.
If you're using a UINavigationBar or have other buttons or fields, activating any of those UIControls could be detected and used to dismiss the keyboard via resignFirstResponder(). In fact Save/Cancel/Done are UIBarButtonItems and are a standard mechanism for completing things and changing state, and create a framework for accomplishing what you want. If you don't take that approach then you have to get creative with how you do it, and also make it clear to the user what needs to be done.
In Interface Builder you can change the type of your main view from UIView to UIControl and then use addTarget() to detect touch events as a 'touch outside' area and use those actions to resign first responder as well. But you might want to consider a UINavigationBar or some other button bar or tab interface to make state transitions.
Also review iOS Human Interface Guidelines document. It's a great document for understanding how iOS is designed to handle common situations like what you are dealing with, and it can get you out of design ruts. It's well written and worth re-visiting periodically.

IPhone need design idea for tableview

i have a UITableView where i want one cell(database row) needs to be default,
So I am thinking of two options
in TableView upon clicking a cell, change accessoryType to mark just like ringtone selection in settings app
when user enters data, add an option (like radio button or segmented control) to make that cell (database row) as default one
I feel first option is good but we can implement code for that only in didSelectRowAtIndexPath but i need to jump to another view when user click on a cell.
So please give me an idea how to accomplish this
One idea iam thinking is adding an edit button but don't know whether its possible or not.
Thanks
Not sure if i understand your question correctly , but according to what i understand you want a table view in which many values will be there , and you want one value to be default which can be changed later.
According to me these ways would be pretty good,
Changing the accessorytype of the tableViewCell. (Most common way)
Changing the Highlighted property of the cell on loading the tableView.
Adding some image then setting the image as the background view of the selected cell.
Adding custom image view (such as tick or something and adding to the cell).
this code can be put in the viewDidload so your default selected value appears.
Hope this helps.
Don't know what you mean by default. I'm assuming you mean already selected. You might just want to set the accessoryType to the checkbox. That would be Apple's way of doing it.

Delete cells in table view Iphone with edit button

I have a table view with different cells and basically I would like an edit button which would then make those red circle appear in each cell and be able to delete them. I already know how to make the "Edit" button appear and also I have overridden the commitEditingStyle method for the table view so I would like to know how to link the button to the action which trigers the red circle and how to make them appear and finally how to actually delete the cells thankyou :)
The editing style on your table view cells should be set to UITableViewCellEditingStyleDelete, and then you just need to make sure to call setEditing:animated: on your table view. UITableViewController can help provide a lot of this functionality pre-baked.
That's actually done through an undocumented API (though I'm not quite sure why) - using it would probably get your app rejected.
However, lots of apps simulate this functionality with their own code - you'd basically define a custom UITableViewCell subclass which changed its background color / toggled that extra red circle image when tapped, keep track of which cells had been tapped, and finally delete them all by calling deleteRowsAtIndexPaths: on the table with the collected list of rows.

Rearranging rows in uitableview

I've an application in which each row contains an uiimageview and an uibutton. I have created them using custom cells, uitableviewcells. The button is to trigger the method, uiimagepickercontroller to pick an image from the library and show it in the imageview. I need that any user who uses the application can rearrange the rows as they wish so that they decide the order of the photos. code here
Reordering of rows in a tableview is pretty straightforward, you just need to implement a few methods of the UITableViewDataSource Protocol and your good to go. Have a look at the Table View Programming Guide for info & code.
http://developer.apple.com/iphone/library/documentation/UserExperience/Conceptual/TableView_iPhone/ManageReorderRow/ManageReorderRow.html
You could show an Edit button in the navigation bar if your using a navigation controller, which switches the table view to edit mode, causing the reordering control to be shown. This edit button could be changed to done, once the table is in edit mode of course. Don't forget to set the showsReorderControl property of the cell to YES. From then on, it's just a matter of implementing the right methods.