Which method will be called when we hold the UITableViewCell - iphone

When we select UITableViewCell than - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath called.
But which method will be called if we hold the UITebleViewCell. Guys my problem is that I have created a tableview which contain large cell and in that cell I am setting various view an view's background color.
When I select the cell, the background color of my cell will be gone. I have solved this problem by setting view background color again in didSelectRowAtIndexPath method like this.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
UIView *vLineview=[selectedCell viewWithTag:1];
vLineview.backgroundColor = [UIColor colorWithRed:(89/255.0) green:(89/255.0) blue:(89/255.0) alpha:1];
}
This done the trick and my view background color will displayed but when I hold the UITableViewCell than it will gone again.
How can I solve this? Do I have to and gesture recognizer to detect long touch and implement my view background method in it? Or there is any other method available for that.

Try with set cell selection style none like this in cellForRowAtIndexPath
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];

You can subclass UITableViewCell and override this method:
- (void) setSelected: (BOOL) selected
animated: (BOOL) animated
{
[super setSelected: selected
animated: animated];
// Configure the view for the selected state
}

Is this an issue with the UITableViewCell remaining selected after you tap on the row?
If row selection is not required, make sure you call deselectRowAtIndexPath:animated at the end of your tableView:didSelectRowAtIndexPath method.
[tableView deselectRowAtIndexPath:indexPath animated:YES];

Pleasese see method named -(void)beginUpdates
Call this method if you want subsequent insertions, deletion, and selection operations (for example, cellForRowAtIndexPath: and indexPathsForVisibleRows) to be animated simultaneously.
for more detail visit apple documentation in below link
UITableView Class Refrence
Also try [cell setSelectionStyle:UITableViewCellSelectionStyleNone];

Related

Get selected UITableViewCell on touch down

I need to get the the currently selected UITableViewCell as its tapped. So upon my finger touching the screen/cell I want to run a method from which I can say something as simple as:
selectedCell = cell;
cell being the one I just tapped and selectedCell being a copy I store.
I am using a custom subclassed UITableViewCell which is why I think I'm having trouble.
Just implement setHighlighted:animated: method on your own custom tableview cell like this.
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
[super setHighlighted:highlighted animated:animated];
NSLog (#"setHighlighted:%# animated:%#", (highlighted?#"YES":#"NO"), (animated?#"YES":#"NO"));
}
TouchDown
- setSelected:animated: is called on the cell itself on touch down here, you can inform a cells delegate
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[self.delegate willSelectCell:self];
}
declare custom cells delegate as property of the cell
#property id<MyCellDelegate> delegate;
TouchUP
save the cell in the delegate
- (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
_selectedCell = [aTableView cellForRowAtIndexPath:indexPath];
}
just pay attention to the fact that Cell Views can be reused

UITableView lose selected state iOS

I have a UITableView and have it so when you press the first cell, it takes you to a new view (new xib). When you press the second cell, you go to another view and so on. When you go to any of those new views, and come back to the tableview view, the cell you just pressed is still selected (its highlighted blue). What is the code to fix this?
In your tableView datasource delegate method:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
Inside viewWillAppear of your tableView whose cell you wish to remove selection from:
UITableViewCell *cell = (UITableViewCell *)[myTableView cellForRowAtIndexPath:lastSelected];
[cell setSelected:NO];
Where lastSelected can be a global var of type NSIndexPath storing indexPath from the didSelectRowAtIndexPath of the above UITableView
It's quite easy..
In the same method that you push the new view you should add the line:
[tableView deselectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated];

change color of uitableviewcell for selected cell in sectioned table

I have sectioned table view with check selection for each section(every section allows only single selection like radio button).
now what i want to do is i want to change the color of cell that user has selected(not for fraction of time color change with animation using selectedBackgroundView property of cell).
Please help.
Thanks.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.contentView.backgroundColor = [UIColor colorWithRed:1.0 green:0.0 blue:0.0 alpha:1.0];
Use this code........
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.contentView.backgroundColor = [UIColor yellowColor];
}
Happy Coding....
Use this in tableview didSelectRowAtIndexPath: delegate method
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
if you want to change the selection style of a particular cell in the tableview then following code can help.. Put this in the - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath method and make the necessary changes
// x is the numbered location of that particular cell appearing in table view
if(indexPath.row==x)
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
// UITableViewCellSelectionStyleBlue or UITableViewCellSelectionStyleNone
cell.selectionStyle=UITableViewCellSelectionStyleGray;
}
when the user select a row
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
gets called. You may consider implement this method in your delegate.
If you want to change only the background when it is selected you have a view reserved for that in a cell:
UIView *selectionBackground = [[UIView alloc] init];
selectionBackground.backgroundColor = A_COLOR;
myCell.selectedBackgroundView = selectionBackground;
Where A_COLOR is a UIColor. You can set this in your cellForRowAtIndexPath.
A. If your cell's color is set to transparent (clearColor) you can simply set cell.selectedBackgroundView.backgroundColor.
For dynamic usage this can be done in -tableView:didSelectRowAtIndexPath: (you can change color each time user selects a row). Otherwise in -tableView:cellForRowAtIndexPath: - either when you setup the cell (after dequeuing or creating - to allow color changes, for example, each time you call reloadData) or when you create the table (if it wasn't dequeued - mostly for persistent color values).
B. If your cell's color is not set to transparent - set color in -tableView:didSelectRowAtIndexPath: as mentioned in most other comments. But using this method, don't forget to restore color of previously selected row.
try this:
[cell setBackgroundColor:[UIColor colorWithRed:55.0/255.0f green:255.0/255.0f blue:178.0/255.0f alpha:1]];

Problem modifying UITableViewCell accessoryType in tableView:didSelectRowAtIndexPath:

I have a transparent table view (UIViewController with subview UIImageView, and another subview UITableView on top of the UIImageView sibling with background = clearColor, UITableViewCells background = clearColor). I also want taps on the cells to toggle the cell's accessoryType between checkmark and none. If I modify the UITableViewCell's accessoryType in tableView:didSelectRowAtIndexPath:, sometimes (30-50% of the time, both on the ios4.1 simulator and on a 3GS iphone running os4.1) when toggling from accessoryType None to accessoryType checkmark the checkmark image is painted against an opaque white background instead of a transparent background. If instead I reload the table (where the accessoryType is also set appropriately for each cell) the transparency works correctly 100% of the time.
Is this a bug? Or is modifying a cell in tableView:didSelectRowAtIndexPath: not the right thing to do, and that row should be reloaded instead? Or is there something else I'm missing?
edit: Here's my didSelectRowAtIndexPath code that shows the undesirable behavior:
edit 2: One more detail of what's happening. It's the very tail end of the deselect animation where the problem happens. The checkmark appears and is displayed properly & transparently while the deselect animation is running and the blue selection is gradually fading out. After the deselect animation finishes and the blue is all gone, maybe 1/10 of a second after the selection color is completely gone, is when the checkmark accessory "automagically" turns opaque with no further user input.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if ( self.editing )
{
}
else
{
[self.myTableView deselectRowAtIndexPath:indexPath animated:YES];
UITableViewCell *cell = [self.myTableView cellForRowAtIndexPath:indexPath];
// toggle the selection status of the selected row
//
NSNumber *rowObj = [NSNumber numberWithUnsignedInt:indexPath.row];
if ( [self.selectedRows containsObject:rowObj] )
{
// currently selected, now de-select
//
cell.accessoryType = UITableViewCellAccessoryNone;
[self.selectedRows removeObject:rowObj];
}
else
{
// currently unselected, now select
//
cell.accessoryType = UITableViewCellAccessoryCheckmark;
[self.selectedRows addObject:rowObj];
}
}
}
I think it's cleaner if you move the toogle selection piece of code to
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
and on didSelectRowAtIndexPath call to
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
This way you will reload only the selected row as soon as it is selected, and no need for calling reloadData.
Hope this helps!
This post hasn't been updated in 4 months but I figured I would submit my solution as it seems to work and could help somebody else with a similar problem.
Everything happens in the didSelectRowAtIndexPath method. The self.view I call is a tableView (so replace it with your tableView's name, e.g. self.tableView)
// let's go through all the cells to update them as I only want one cell at a time to have the checkmark next to it
for (int i=0; i<[self.detailsArray count]; i++) {
// whenever we reach the cell we have selected, let's change its accessoryType and its colour
if (i == indexPath.row) {
// that's the row we have selected, let's update its
UITableViewCell *cell = [self.view cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
cell.textLabel.textColor = [UIColor colorWithRed:51.0f/255.0f green:102.0f/255.0f blue:153.0f/255.0f alpha:1.0f];
}
// if it isn't the cell we have selected, let's change it back to boring dark colour and no accessoryType
else {
UITableViewCell *cell = [self.view cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]];
cell.accessoryType = UITableViewCellAccessoryNone;
cell.textLabel.textColor = [UIColor darkTextColor];
}
}
// don't forget to deselect the row
[self.view deselectRowAtIndexPath:indexPath animated:YES];
this UITableViewDelegate method:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
is called right before displaying a cell. Modifying your accessory view there should work.
In debugging another problem in an unrelated project with a custom table row selection animation, I found that if the cell selection style is not UITableViewCellSelectionStyleNone, then any changes to the cell performed in didSelectRowAtIndexPath are subject to glitches and other desirable results (the changes made to cell N do not show up until cell != N is selected later, and animations performed on the cell never show up at all). When I changed the selection style to UITableViewCellSelectionStyleNone, then all visual cell changes made in didSelectRowAtIndexPath show up right away without problems.

How do get the view that is the tapped detail-disclosure-button

The cell in this iPhone TableView definitely has a Detail-Disclosure-Button.
When I tap it... shouldn't this code give me access to the button?
Instead detailButton is always just null.
- (void) tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *aCell = [tableView cellForRowAtIndexPath:indexPath];
UIButton *detailButton = (UIButton *)[aCell accessoryView];
}
are you over-riding the cell accessory button in 'cellForRowAtIndexPath' by any chance?
accessoryView is provided for your own customization, to override accessoryType. The table view cell will internally handle whatever you assign to accessoryType without affecting accessoryView.