I have to make a call to specific number when imageview is clicked in tableviewcell.The number to which call is made is displayed in a label beside the imageview.But I couldn't get which cell is clicked.Always its referring to the last cell.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(cell == nil)
{
cell =[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
lblphone = [[UILabel alloc] initWithFrame:CGRectZero];
lblphone.tag = 116;
lblphone.backgroundColor = [UIColor clearColor];
[lblphone setFont:[UIFont fontWithName:#"Helvetica" size:12]];
[lblphone setLineBreakMode:UILineBreakModeWordWrap];
[lblphone setUserInteractionEnabled:YES];
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(labelButton:)];
tapGestureRecognizer.cancelsTouchesInView=NO;
[tapGestureRecognizer setNumberOfTapsRequired:1];
[lblphone addGestureRecognizer:tapGestureRecognizer];
[tapGestureRecognizer release];
[cell addSubview:lblphone];
myImageView = [[UIImageView alloc] initWithFrame:CGRectZero];
myImageView.tag = 120;
myImageView.image=[UIImage imageNamed:#"CallImg.png"];
UITapGestureRecognizer *tapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(imageSelectedInTable:)];
[tapped setNumberOfTapsRequired:1];
[myImageView addGestureRecognizer:tapped];
[tapped release];
[cell addSubview:myImageView];
}
[myImageView setFrame:CGRectMake(10, 50,30,30)];
myImageView= (UIImageView*)[cell viewWithTag:120];
myImageView.image=[UIImage imageNamed:#"CallImg.png"];
myImageView.userInteractionEnabled=YES;
CGSize constraint5 = CGSizeMake(320, 2000.0f);
CGSize size5=[phone sizeWithFont:[UIFont fontWithName:#"Helvetica" size:14] constrainedToSize:constraint5 lineBreakMode:UILineBreakModeWordWrap];
lblphone =(UILabel *)[cell viewWithTag:116];
[lblphone setFrame:CGRectMake(45,name.frame.size.height+name.frame.origin.y,320, size5.height)];
lblphone.textAlignment=UITextAlignmentLeft;
lblphone.backgroundColor=[UIColor clearColor];
lblphone.text=[NSString stringWithFormat:#"%# ",phone ];
[lblphone sizeToFit];
}
And in the tapgesture of imageclick I am writing this :
-(IBAction)imageSelectedInTable:(UITapGestureRecognizer*)gesture
{
UIImageView *imgView = (UIImageView *) [gesture view];
UITableViewCell *cell = (UITableViewCell*)[[imgView superview]superview];
NSIndexPath *tappedIndexPath = [self.tableView indexPathForCell:cell];
NSLog(#"Image Tap %#", tappedIndexPath);
UILabel *phoneno = (UILabel *)[cell viewWithTag:116];
NSString *phoneNumber = [#"telprompt://" stringByAppendingString:phoneno.text];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumber]];
}
But it is always referncing to the last cell clicked irrespective of which cell is clicked.Couldn't understand where im going wrong ?
I see you try to access your cell,
UITableViewCell *cell = (UITableViewCell*)[[imgView superview]superview];
but you add your image view to
[cell addSubview:myImageView];
You should be adding your image view to cell.contentView.
It looks like the image view is a subview of the cell (one step down the tree), and the code that's probing for the parent is taking two steps up ...superview superview. (That latter approach is right for nib defined cells which add subviews to the cell's contentView).
Here's a safer way to climb the view hierarchy that will work no matter how the cell was built.
- (UITableViewCell *)tableViewCellContaining:(UIView *)aView {
UIView *answer = aView;
while (answer && ![answer isKindOfClass:[UITableViewCell self]])
answer = answer.superview;
return answer;
}
I think your code will probably work if you replace this line:
UITableViewCell *cell = (UITableViewCell*)[[imgView superview]superview];
with this line
UITableViewCell *cell = [self tableViewCellContaining:imgView]
for get/know Which cell is clicked, Write Following code
UITableViewCell *cell = (UITableViewCell *)[[imgView superview]superview];
and if you alSo want to get indexPath of tapped cell then write
NSIndexPath *indexPath = [tableView indexPathForCell:cell];
EDITED :-> Another way is
Give tag of UIImageView in cellForRowAtIndexPath Method
Such like,
imageView.tag = indexPath.row;
And Use Following Code
int row = ((UIImageView*)imgView).tag; //Or// int row = ((UIGestureRecognizer *)sender).view.tag;
NSIndexPath* indexpath = [NSIndexPath indexPathForRow:row inSection:0];
UITableViewCell* cell = [table cellForRowAtIndexPath:indexPath];
Here You can get Cell of Tapped UIImageView.
Related
I have a UITableView within a RootViewController and when a cell is tapped I want it to chance colour but only one should be enabled at one time.
I'm having difficulty trying to say "if CGRect does not contain tap".
.m
- (void)changeColour:(UITapGestureRecognizer *)tap
{
if (UIGestureRecognizerStateEnded == tap.state) {
UITableView *tableView = (UITableView *)tap.view;
CGPoint p = [tap locationInView:tap.view];
NSIndexPath* indexPath = [tableView indexPathForRowAtPoint:p];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
CGPoint pointInCell = [tap locationInView:cell];
if (CGRectContainsPoint(cell.frame, p)) {
UIView* view1 = [[UIView alloc] init];
view1.backgroundColor = [UIColor redColor];
[cell setBackgroundView:view1];
} else {
UIView* view1 = [[UIView alloc] init];
view1.backgroundColor = [UIColor whiteColor];
[cell setBackgroundView:view1];
}
}
}
Better way is to not to use gestures...
Use TableView Delegate methods...
To use this make sure you have assign delegates to your tableview..
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
UIView* view1 = [[UIView alloc] init];
view1.backgroundColor = [UIColor redColor];
[cell setBackgroundView:view1];
[view1 release];//for NON ARC ONLY
}
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
UIView* view1 = [[UIView alloc] init];
view1.backgroundColor = [UIColor whiteColor];
[cell setBackgroundView:view1];
[view1 release];//for NON ARC ONLY
}
This will definitely help you
Enjoy coding........
i have created a label and image in each cell of a table view. if image clicked i want the label data referred to that cell.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UILabel *phone=[[[UILabel alloc]initWithFrame:CGRectMake(10, 95, 320,15)]autorelease];
phone.font=[UIFont boldSystemFontOfSize:12];
[phone setTextAlignment:UITextAlignmentLeft];
[phone setText:[d valueForKey:#"Phone"]];
[cell addSubview:phone];
myImageView.image = [UIImage imageNamed:#"call.png"];
cell.imageView.image=myImageView.image;
cell.imageView.userInteractionEnabled=YES;
UITapGestureRecognizer *tapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(imageSelectedInTable:)];
[cell.imageView addGestureRecognizer:tapped];
[tapped release];
}
-(IBAction)imageSelectedInTable:(UITapGestureRecognizer*)gesture
{
NSLog(#"Selected an Image");
UIImageView *imgView = (UIImageView *) [gesture view];
UITableViewCell *cell = (UITableViewCell*)[[imgView superview]superview];
NSIndexPath *tappedIndexPath = [self.tableView indexPathForCell:cell];
NSLog(#"Image Tap %#", tappedIndexPath);
}
but how to get label data
thank u
There appears to be several things wrong with your code, like where are you getting "cell" in cellForRowAtIndexPath? That being said, to fix your immediate problem, you could add a tag to your phone label inside cellForRowAtIndexPath, e.g. phone.tag = 5; then inside your gesture recognizer method, add UILabel *phone = (UILabel *)[cell viewWithTag:5];. Now you have a reference to the label in that cell.
I have a UILabel in a UITableViewCell which displays a phone number. When I click it I should be able to make call to that particular number. So for making it clickable I have added a UITapGestureRecognizer to it .But I couldn't get how to refer the which tap is clicked, I mean which number was clicked.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
lblphone = [[UILabel alloc] initWithFrame:CGSizeMake(20,30,50,100)];
lblphone.tag = 116;
lblphone.backgroundColor = [UIColor clearColor];
lblphone.text=[NSString stringwithFormat:#"%#",phone];
[lblphone setFont:[UIFont fontWithName:#"Helvetica" size:12]];
[lblphone setLineBreakMode:UILineBreakModeWordWrap];
[lblphone setUserInteractionEnabled:YES];
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(labelButton:)];
[tapGestureRecognizer setNumberOfTapsRequired:1];
[lblphone addGestureRecognizer:tapGestureRecognizer];
[tapGestureRecognizer release];
[cell addSubview:lblphone];
}
-(IBAction)labelButton:(UITapGestureRecognizer*)tapGestureRecognizer
{
NSIndexPath *indexPath;
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
UILabel *phoneno = (UILabel*)[cell viewWithTag:116];
NSString *phoneNumber = [#"telprompt://" stringByAppendingString:phoneno.text];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumber]];
}
But everytime I could see the phonenumber of last cell in NSString *phoneNumber;
How can I refer which label is clicked in UITapGestureRecognizer?
Use lblphone.tag = indexPath.row + 1; instead of lblphone.tag = 116; in cellForRowAtIndexPath
Also you can achieve this without using the tags.
See Example below
-(void)labelButton:(UITapGestureRecognizer*)tapGestureRecognizer
{
UILabel *phoneno = (UILabel *) tapGestureRecognizer.view;
}
Table view center cell always highlight like a picker. If I select the center cell, that value is return. If I scroll the table at the time center point must highlighted the values only change(like picker action)How to do this? Any sample code for this task. Thanks in advance.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
UILabel *modelLabel;
UIButton *modelButton;
modelButton = [UIButton buttonWithType:UIButtonTypeCustom];
modelButton.frame = CGRectMake(0.0, 0.0, LABEL_WIDTH, LABEL_HEIGHT);
modelButton.tag = indexPath.row+100;
[modelButton addTarget:nil action:#selector(modelButtonAction:) forControlEvents:UIControlEventTouchUpInside];
[[cell contentView] addSubview:modelButton];
modelLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, LABEL_WIDTH, LABEL_HEIGHT)];
modelLabel.text = [NSString stringWithFormat:#"%#", [[modelArray objectAtIndex:indexPath.row] valueForKey:#"Model"]];
modelLabel.tag = indexPath.row+1000;
modelLabel.backgroundColor = [UIColor clearColor];
modelLabel.textColor = [UIColor grayColor];
modelLabel.alpha = 0.5;
modelLabel.textAlignment = UITextAlignmentCenter;
modelLabel.font = EUROSLITE_FONT(14);
[[cell contentView] addSubview:modelLabel];
}
return cell;
}
The rows count is 700, 800, like this.
A UITableView extends UIScrollView. So you can implement the following delegate:
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
There you can check the scrollView.contentOffset and set the corresponding cell to highlighted. Please note, that's not the center cell. You need to add (int)(scrollView.frame.size.height/2)-(int)(cell.frame.size.height/2) on scrollView.contentOffset.y.
Example (Please de-highlight all other cells):
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
[[tableView visibleCells] makeObjectsPerformSelector:#selector(setHighlighted:) withObject:[NSNumber numberWithBool:NO]];
CGPoint center = CGPointMake(0, scrollView.contentOffset.y+(int)(scrollView.frame.size.height/2));
NSIndexPath *cellIndexPath = [tableView indexPathForRowAtPoint:center];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:cellIndexPath];
[cell setHighlighted:YES];
}
I have seen some posts before, but didn't get the answer yet, thats why i am trying to post again in more effective manner. How can i use check-uncheck functionality in UITableView like below image.
This is table i want when i click on button of any cell, that buttons image will change, not on all cells.
For Check-Uncheck functionality only buttonClicked: method is not enough. You will have also put the condition in cellForRowAtIndexPath: method for which button is selected or which in unselected because cellForRowAtIndexPath: method will call each time when you will scroll your UITableView and cells will be refresh.
And i saw your previous question you're adding two buttons with two action not a good way just change the image of button for check-uncheck.
So here is what i do for this -
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController<UITableViewDelegate, UITableViewDataSource>
{
IBOutlet UITableView *tblView;
NSMutableArray *arrayCheckUnchek; // Will handle which button is selected or which is unselected
NSMutableArray *cellDataArray; // this is your data array
}
#end
Now in ViewController.m class -
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
arrayCheckUnchek = [[NSMutableArray alloc]init];
//Assign your cell data array
cellDataArray = [[NSMutableArray alloc]initWithObjects:#"cell-1",#"cell-2",#"cell-3",#"cell-4",#"cell-5", nil];
// setting all unchecks initially
for(int i=0; i<[cellDataArray count]; i++)
{
[arrayCheckUnchek addObject:#"Uncheck"];
}
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [cellDataArray count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [cellDataArray objectAtIndex:indexPath.row];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake(270.0, 7.0, 30.0, 30.0)];
if([[arrayCheckUnchek objectAtIndex:indexPath.row] isEqualToString:#"Uncheck"])
[button setImage:[UIImage imageNamed:#"uncheck_icon"] forState:UIControlStateNormal];
else
[button setImage:[UIImage imageNamed:#"check_icon"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:button];
return cell;
}
-(void)buttonClicked:(id)sender
{
//Getting the indexPath of cell of clicked button
CGPoint touchPoint = [sender convertPoint:CGPointZero toView:tblView];
NSIndexPath *indexPath = [tblView indexPathForRowAtPoint:touchPoint];
// No need to use tag sender will keep the reference of clicked button
UIButton *button = (UIButton *)sender;
//Checking the condition button is checked or unchecked.
//accordingly replace the array object and change the button image
if([[arrayCheckUnchek objectAtIndex:indexPath.row] isEqualToString:#"Uncheck"])
{
[button setImage:[UIImage imageNamed:#"check_icon"] forState:UIControlStateNormal];
[arrayCheckUnchek replaceObjectAtIndex:indexPath.row withObject:#"Check"];
}
else
{
[button setImage:[UIImage imageNamed:#"uncheck_icon"] forState:UIControlStateNormal];
[arrayCheckUnchek replaceObjectAtIndex:indexPath.row withObject:#"Uncheck"];
}
}
And finally it will look like -
Tags are useful with customised uitableviewcell designed in IB. If you create cells programmatically, you don't need tags. You use them only to find correct uiview to set properties.
Yes this is simple to set Tags while declaring the classes inside UITableViewCell and identify them using tags. I have posted some sample code for your reference. Am not sure it will solve your problem. You asked how to set tags and identify the classes using tags. So it will give you some basic idea about your question.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
label1 = [[UIView alloc] initWithFrame:CGRectMake(2, 20, 15, 15)];
label1.tag = 100;
label1.backgroundColor = [UIColor whiteColor];
label1.layer.cornerRadius = 5;
[cell.contentView addSubview: label1];
label2 = [[UILabel alloc] initWithFrame:CGRectMake(25, 2, 165, 23)];
label2.tag = 101;
label2.font = [UIFont boldSystemFontOfSize:15];
label2.backgroundColor = [UIColor clearColor];
[cell.contentView addSubview: label2];
label3 = [[UILabel alloc] initWithFrame:CGRectMake(190, 2, 90, 23)];
label3.tag = 102;
label3.font = [UIFont systemFontOfSize:14];
label3.textColor = [UIColor colorWithRed:0.14117670588235 green:0.435294117647059 blue:0.847058823529412 alpha:1];
label3.backgroundColor = [UIColor clearColor];
label3.textAlignment = UITextAlignmentRight;
[cell.contentView addSubview: label3];
}
label1 = (UILabel *) [cell.contentView viewWithTag:100];
NSString *string1 = [array1 objectAtIndex:indexPath.row];
label2 = (UILabel *) [cell.contentView viewWithTag:101];
NSString * string2 = [array2 objectAtIndex:indexPath.row];
label3 = (UILabel *) [cell.contentView viewWithTag:102];
NSString * string3 = [array3 objectAtIndex:indexPath.row];
return cell;
}
Please let me know this is useful or not. Otherwise we'll go to some other way.
Happy Coding. Thanks.
i agree with #VakulSaini because u can do this to handle which cell touch or swipe or what ever and this is an ex:
-(void)handleSwipLeft:(UISwipeGestureRecognizer *)gestureRecognizer
{
CGPoint Location = [gestureRecognizer locationInView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:Location];
cell = [tableView cellForRowAtIndexPath:indexPath];
}
Use cellForRowAtIndexPath method of UITableView and in This method Add any component like (UITextField,UILabel ..... etc ) In your UITableViewCell by using [cell.contentView addSubview:YourComponentName]; and give each component to its tag by indexPath.row.
such like ,,,
YourComponentName.tag = indexPath.row;