drag and drop in uitableview - iphone

I am currently developing an application in which a user is displayed a list in the form of a tableview. when the user selects a particular row, he/she can drag it to another row. When that happens an alert is displayed giving some specific information.Any idea as to how it can be done.?
What I am doing is that I am using gesture recognizers.When a particular row is selected, an image of the selected row is made which then is dragged to the specific table view cell.I am able to move the image but my problem is that if I dont put the imageView under a UIView, the dragging stuff does not happen.....
My dragging code is based on apple's touches sample code .
Update:
After trying for some time, I am able to almost implement this.I still have one doubt though.. I am creating an image of a cell once it is tapped.Then UIPanGestureRecognizer has been added to that imageView which it turn makes its movement possible .The only problem is how can I know on which cell the image has been dropped?

you should implement:
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
which will return YES, and also implememt:
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
//do whatever u want after row has been moved
}
and then call this function from tableView:
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
to enter to edit mode.

This is a sample of an application with cells drag-n-drop in UITableView using native methods of delegate and dataSource of UITableView.
https://github.com/coderDove/UITableView-Drag-n-Drop

Related

How to implement "Loading" in UITableView when scrolling down?

I am working on iPhone application . This application contains plenty of web services. I need to show list in UITableView with page by page. How to show "Loading" and call web service when scrolling down the UITableview ? Please help me..
I assume that you are adding a custom cell to the bottom of your table view to indicate the end of the current data set. When you create that cell, set its tag property to a meaningful constant you've previously defined, like END_OF_TABLE_CELL.
Then use the delegate method tableView:willDisplayCell:forRowAtIndexPath to check if the cell about to display is your custom cell, and trigger a reload of the table's datasource.
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
//check your cell's tag
if (cell.tag == END_OF_TABLE_CELL) {
//Call your custom method to fetch more data and reload the table
// You should also remove this custom cell from the table and add one at the bottom
}
}
This way you will have a table that refreshes itself whenever a user scrolls down far enough to bring the custom cell into view.
Good Luck!

Iphone: UITableView how to detect that row is selected

I have a UITableView, and I press the row, but do not release finger, so didSelectRowAtIndexPath is not called yet, right?
How to detect on which row i clicked? in which method?
How to detect that I clicked on table view? Is there any method like rowIsPressed? or onTableViewTouchDown?
There are two options at least:
1) You can subclass UITableViewCell and than use the touchhandling you know from a default uiview. (Like touchesBegan: etc.) / OR add a custom uiview as contentView of the tablecell.
2) Add a gesturerecognizer to each tablecell.
To find out in which row you are, you could give every row a tag (which than refers to the row) in cellForRow:atIndexPath:
try this
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
There is no such method to detect the selected state before lifting the finger... I guess you should override the UITableView class and add a UIGestureRecognizer to it.
And about your second question, I think you are talking about this table view delegate method:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
But there is also another method that will give you the current selected row. This is it (directly from the table view class):
- (NSIndexPath *)indexPathForSelectedRow
Hope to have helped!

How to display the selected cell in highlighted state when i come back in UITableView?

I created a custom cell for my table view and for the most part everything seems to be working fine, but when I select one of the rows (which takes me to another UIView), then come back from the subsequent view via the nav controller, the selected cell is not in highlighted state. How to display the selected cell in highlighted state when i come back?
any help is appreciated in advance, thanks.
when you are coming back from anotherview make sure that save the selectedCell and then in viewwillappear method reloaddata.in cellforindexpath write the code of selection style uitableviewcellselectionstyleblue
As #sachin said, you should save selected index path
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
self.selectedIndexPath = indexPath;
}
and in
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
You should check if indexPath is equal to selectedIndexPath,
but you should be aware that Apple discoureges that kind of behavior in HIG: http://developer.apple.com/library/ios/#DOCUMENTATION/UserExperience/Conceptual/MobileHIG/UIElementGuidelines/UIElementGuidelines.html
In rare cases, a row might remain highlighted when secondary details
or controls related to the row item are displayed in the same screen.
However, this is not encouraged because it is difficult to display
simultaneously a list of choices, a selected item, and related details
or controls without creating an uncomfortably crowded layout.

Look like iPhone inbuilt contact applications image selection

I have created an application in which i have to add users to the sqlite database.
Now the problem is I want the look of the standard iPhone Contact application Where while adding user we have the width of first cell smaller than other cells and the image before that cell..
Can you please give me the idea how such thing is possible.
How to make one cell small and rest others of normal size..
Thanks for any help in advance
There are three UITableViewDelegate messages you can listen for to adjust height
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
However, even thought I didn't write Contacts.app I have a feeling they are also using
- (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath
To adjust the views as well. Remember, you don't have to pack everything into a single monolithic custom table view cell. You can create multiple custom table view cells and load them each appropriately depending on the index path.
The contact detail view is a grouped tableview. Each cluster of cells is a section. The top section is a single custom cell with two subviews that look like squashed tableview cells. The left view shows the contact's photo. The right view shows the name.
To reproduce, create a custom UITableView subclass and lay it out like you want either programmatically or in Interface Builder. Then in the tableview delegate's cellForRowAtIndexPath check indexPath.section and return the proper row for the section.
It appears that the Contacts app uses a custom tableHeaderView when presenting the contact details with an image and label. A similar implementation is included in the sample project iPhoneCoreDataRecipes. The RecipeDetailView loads a separate nib in tableViewHeaderView that is used to set the tableView.tableHeaderView property. Have a look at RecipeDetailViewController.{h,m} and DetailHeaderView.xib. When the Contacts app switches to editing mode, the headerView appears to be swapped out for another view that has a button and a tableView with a single cell. This will allow you to set up a separate tableViewDelegate to handle the Name parts of the contact and a delegate to handle the address / telephony details.

Change UITableViewCell appearance when drag handle is pressed

I'd like to change the appearance of a cell when the user tap the drag handle.
I searched in the documentation, forums and google, but I can't find a method or an event that say when the drag icon is pressed.
Any help will be appreciated!
Thanks
Does this function help:
// Allows customization of the target row for a particular row as it is being moved/reordered
- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath;
It doesn't seem to fire until the cell has been dragged about half way into another cell, though.
It is the only indication I could find that a cell is moving.