Iphone: UITableView how to detect that row is selected - iphone

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!

Related

Overriding the default tableview setEditing behaviour

I have currently implemented the setEditing/commitEditing methods for my table view, which now shows the 'delete' button upon swiping a cell.
Just wondering if there is any way to change the text on this button from 'delete' to something else, so that I can use the 'commitEditing' method to perform custom behaviour, such as update some settings?
thanks
You can specify the delete button title with the UITableViewDelegate method:
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath

How to implement add a index search view on top the tableView?

I want to add the index letter on top of the UITableView when touch the index, how to do it?
Can't find any third party implementation of this, which is surprising (I would be happy to be proven wrong). Looks like you'll have to code it from scratch by overlaying a thin UIView subclass on top of the table view (probably best to use core graphics here to draw the outline and the letters), intercept touches on it and determine which letter they are in contact with and then send appropriate scrolling instructions to the table view using
- (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated
Well because you have already sections you can implement when willDisplayCell method is called to [self setTitle:[sectionsArray objectAtIndex:indexPath.section];
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath
*)indexPath

Delete Button in UITableView

i want a Delete Button in my UITableView like in die AdressBook when editing a contact.
Is this a custom cell, or a UIButton?
To make such button you can do following:
Make your UITableView style Grouped.
Add +1 section to your table.
Say to your table that it is one cell for last section
Just return custom cell (with red background and appropriate label) for last section
If you implement the Delegate method's for edit the tableview
- (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath
And than implement the DataSource method
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
For allowing to get that delete button when you swipe a cell use the
Cell Editing Style : UITableViewCellEditingStyleDelete
// Edit: Wrong answer.
Do you mean the Big one at the bottom or with the sign on the left?
use....
firstly use
[btn removeFromSuperview];
and
[tblobj reload];

Add a view to a UITableViewCell's contentView when it is selected

I am trying to add a new view with 5 buttons to a cell once it is selected. I am trying to do this in 3 different ways. The first one is by checking if it is selected inside the
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
method.
if (cell == nil ) {
//initial customizaton of cells
} else {
if ([cell isSelected]) {
//I would add the view to the cell's contentView here but it never enters this if even though the cell is still blue, I scrolled the table back and forth several times and the blue cell never evaluated here
}
}
I have also tried to do it inside the following two methods:
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
And:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
I used tableView:cellForRowAtIndexPath: to get a reference to the cell and when I had that reference I tried adding the view inside the cell's contentView but it didn't seem to work(by "didn't work" I mean it didn't appear, I'll debug this further).
I know I didn't provide much detailed code but that's because I mainly want to know what's the general principle of how it should be done. I want to know why the cell doesn't stay with the selected property equal to YES even though it stays blue (when selectionStyle is the default one) when I tap it. I want it to stay "blue"(selected) with that view inside it until I deselect it and remove said view.
Did you try creating your own custom UITableViewCell? I think that is the point, you can overload setSelected: method there.
I followed NR4TR's advice and so far so good. I have subclassed UITableViewCell and have overridden the setSelected method in it.
Hoping this is useful to someone else in the future!

[iPhone]How to customize header of Section in grouped TableView?

To customize cell, we implement class inherit UITableViewCell. Now, to customize header of Section in grouped UITableView (same to below picture), how to do? Please guide to me!
Implement tableView:viewForHeaderInSection: in your table view delegate.
Starting with iOS 5 you also need to implement
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
In addition to
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
Otherwise it won't work.
From Apple's reference
The returned object can be a UILabel or UIImageView object, as well as a custom view. This method only works correctly when tableView:heightForHeaderInSection: is also implemented.