I want to have my UITableViewCell disabled but still be able to press the accessoryView button.
I thought this would work:
cell.userInteractionEnabled = NO;
cell.accessoryView.userInteractionEnabled = YES;
But my UITableViewCellAccessoryDetailDisclosureButton is still disabled.
accessoryView will get all his touchEvents from his superview, in this case your cell and wont react to your actions.
What might work is creating a touch event to that accesoryView on the viewController you make the tableView on and make it react to touches.
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
{
UITouch *touch = [[event allTouches] anyObject];
if ([touch view] == accessoryView) {
//do something because you hit accessoryView
}
}
If you disable a view, all its subviews will be disabled.
For this you have to set userInteractionEnabled =NO all subviews in cell expect in Disclosure and cell.
cell.selectionStyle=UITableViewCellSelectionStyleNone;
cell.userInteractionEnabled = YES;
Place this line in this method.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
Leave this method blank. if u want to code in this delegate method so filter those row by code:
if(indexPath.row == row no in which u don't want click event)
{
leave blank
}
else
{
code which u want .
}
}
you get your click event for particular button in this method.
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath;
Related
I am having UITableView with customCell(CustomCell having 2 lables and One ImageView).
In normal Mode I need white color of all the cells.
If user press the particular cell, then color of that cell should be gray(rest of the cells should be white)
And If user release the same cell, Color of that cell should be Orange(rest of the cells should be white)
How can I figure it out?
I have tried this using setSelectedBackground, willSelectRowAtIndexPath and Gestures methods. But can't see these 3 Color states for the same Cell. Any Of the 2 states are working together.
Any Idea how can i achieve the same functionality?
I have implemented same functionality in android using selectors. I want the same functionality in iPhone. Any Help?
Thanks in advance!
Write these two method in your Custom Cell
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
self.contentView.backgroundColor=[UIColor greenColor];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
self.contentView.backgroundColor=[UIColor orangeColor];
}
A pretty elegant solution is to override the setHighlighted: method of your tableViewCells.
- (void)setHighlighted:(BOOL)highlighted {
[super setHighlighted:highlighted];
if(highlighted) {
_backView.backgroundColor = [UIColor blueColor];
}
else
{
_backView.backgroundColor = [UIColor blackColor];
}
}
UITableView will automatically set the selected cell highlighted #property to YES when the user tap on the cell.
Don't forget to call [_tableView deselectCellAtIndexPath:indexPath animated:NO]; in your didSelect tableView delegate method if you want to make your cell unselected!
If you want gray then
cell.selectionStyle=UITableViewCellSelectionStyleGray;
Or you can set background color on didSelectRow
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView reloadData];
UITableViewCell *cell=(UITableViewCell*)[tableView cellForRowAtIndexPath:indexPath];
[cell setBackgroundColor:[UIColor orangeColor]];
}
If you don't want to reload tableData then you have to save your previousSelected Index value then
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//Make oreange cell
UITableViewCell *presentCell=(UITableViewCell*)[tableView cellForRowAtIndexPath:indexPath];
[presentCell setBackgroundColor:[UIColor orangeColor]];
//make your previous cellBackgrod to clear, you can make it white as per your requirement
UITableViewCell *previouscell=(UITableViewCell*)[tableView cellForRowAtIndexPath:previousSelectedCellIndexPath];
[previouscell setBackgroundColor:[UIColor clearColor]];
//save your selected cell index
previousSelectedCellIndexPath=indexPath;
}
Is it possible to distinguish which side of a row in a uitableview has been clicked?
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//which side of row has been detected left or right
}
You can subclass UITableViewCell. In your subclass, you can make an iVar wasTouchOnLeft. Then override touchesBegan or touchesEnded like the example below:
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch=(UITouch *)[touches anyObject];
if([touch locationInView:self].x< (0.5 * self.frame.size.width) )
{
NSLog(#"Touched Left");
wasTouchOnLeft=YES;
}
else {
NSLog(#"Touched Right");
wasTouchOnLeft=NO;
}
[super touchesEnded:touches withEvent:event];
}
In the didSelectRowAtIndexPath of your UITableViewController you can then access the wasTouchOnLeft variable for the cell.
You can't determine this directly in this method.
But if you really want this feature create a custom tableview cell that has two big buttons (one left one on the right side) or apply a touch recognizer on the cell.
If you have this delegate the action to your controller
(You could also store it in the cell and access it via)
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(((YourCustomTableCell*)[tableView cellForRowAtIndexPath:indexPath]).clickedSideProperty) action;
else anotherAction;
}
But better delegate it from the table cell than access it from didSelectRow…
I have facing problem is that Is there any way to change default frame
of delete button in UITableView and one more thing is that delete button is always display in center of cell of UITableViewcell so any way to change frame of delete button.
// Update the data model according to edit actions delete or insert.
- (void)tableView:(UITableView *)aTableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
[appDelegate.categories removeObjectAtIndex:indexPath.row];
[categoryTableView reloadData];
}
}
#pragma mark Row reordering
// Determine whether a given row is eligible for reordering or not.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
// Process the row move. This means updating the data model to correct the item indices.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath
toIndexPath:(NSIndexPath *)toIndexPath
{
NSString *item = [[appDelegate.categories objectAtIndex:fromIndexPath.row] retain];
[appDelegate.categories removeObject:item];
[appDelegate.categories insertObject:item atIndex:toIndexPath.row];
[item release];
}
Thanks in advance.
in ios 6.1 i was not able to use the above code. I've seen several answers that suggests subclassing the cell class and providing a custom -willTransitionToState. Changing the subview/buttonview frame does not affectively change it's displayed position.
However if you override the layoutSubviews method it will work. Here is the code:
//below i moved the delete button to the left by 10 points. This is because my custom tableview has lef
- (void)layoutSubviews
{
[super layoutSubviews];
for(UIView *subview in self.subviews)
{
if([NSStringFromClass([subview class]) isEqualToString:#"UITableViewCellDeleteConfirmationControl"])
{
CGPoint o = subview.center;
subview.center = CGPointMake(o.x - 10, o.y);
}
}
}
You can use your own view by setting the cell's editingAccessoryView property. This can have whatever frame you like, though you'll need to supply a gradient if you want it to look like the standard delete button.
In the xib file of your custom cell, add a new button of whatever size you like (as long as it is smaller than the cell!). This button should not be a subview of your cell, but a separate item in the file.
Connect this button to the editingAccessoryView outlet of your cell and it will replace the standard delete button.
how to detect touch event for table cells
i tried this
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//<my stuff>
[super touchesBegan:touches withEvent:event];
}
but its not working actuallly i have aUIimage view in table cell and i want to chnage imgae based on tap so my touch event is not working for that cell
If you want to detect a touch on the UITableViewCell you don't really need to detect touch events. In your UITableViewController subclass, you need to implement the following delegate method:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
Then you modify the image of the table cell for the selected index path.
You probably need to set myImageView.userInteractionEnabled = YES;.
In one of my projects I needed any tap on the tableView to dismiss the keyboard so the underlying tableView would show. Since a UITableView is really a UIScrollView, it will respond to the scrollView delegate methods. Using these 2 methods will dismiss if either the user taps on a cell or scrolls the tableView at all:
IMPORTANT: Make sure you implement the UIScrollViewDelegate in your .h file as well as the UITableViewDelegate and UITableViewDataSourceDelegate!!!
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//remove keyboard if table row is clicked
if ([self.firstName isFirstResponder] || [self.lastName isFirstResponder]) {
[tableView deselectRowAtIndexPath:indexPath animated:NO];
[self.firstName resignFirstResponder];
[self.lastName resignFirstResponder];
}
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
//remove keyboard if table scrolls
if ([self.firstName isFirstResponder] || [self.lastName isFirstResponder]) {
[self.firstName resignFirstResponder];
[self.lastName resignFirstResponder];
}
}
What's the best way of adding an image for the touch state in a UITableViewCell?
I know how to set the cell.selectionStyle but I actually need to add an image (a different one for each row) just when the user is touching it (and it pushes the new view in) Like a "Button State Touched".
What's the best way of achieving this? I tried with
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { }
Or would I have to do it inside
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
Thanks!
I recommend that you create a custom UITableViewCell and use the touchesBegan and touchesEnded.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchedBegan:touched withEvent:event];
// ... display image 2, hide image 1
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchedEnded:touched withEvent:event];
// ... display image 1, hide image 2
}
I think you need both:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { }
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
For the didSelectRow, you need to store the current selected cell index. Then you call [tableView reloadData]
Then in the cellForRowAtIndexPath:, you need to set cell.imageView.image = [UIImage imageNamed:#"your_image_here"];