How to receive touches on UIPickerview using viewForRow delegate? - iphone

Could anyone help me out. I'm trying to implement multiple selection on UIPickerview(in iOS 7). With few references from the stack overflow ,i have done as below,
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
UITableViewCell *cell = (UITableViewCell *)view;
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
[cell setBackgroundColor:[UIColor clearColor]];
[cell setBounds: CGRectMake(0, 0, cell.frame.size.width -20 , 44)];
cell.tag = row;
cell.userInteractionEnabled = YES;
UITapGestureRecognizer *singleTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(toggleSelection:)];
singleTapGestureRecognizer.numberOfTapsRequired = 1;
singleTapGestureRecognizer.delegate= self;
[cell addGestureRecognizer:singleTapGestureRecognizer];
[self.doorSelectionPickerView addGestureRecognizer:singleTapGestureRecognizer];
}
//TO DO FOR CHECKMARK
cell.textLabel.text = #"1";
return cell;
}
After trying this, its not receiving the tap gesture, and thus toggleSelection method is not invoked.
Note: UIpickerview is given as input view of UITextField
userSafeDoorTextfeild.inputView = [self doorSelectionPickerView];

I fixed the issue and could help someone who are in this troubled situation.
-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:
(NSInteger)row forComponent:(NSInteger)component
reusingView:(UIView *)view {
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
[cell setBackgroundColor:[UIColor clearColor]];
[cell setBounds: CGRectMake(0, 0, cell.frame.size.width -20 , 44)];
cell.tag = row;
cell.userInteractionEnabled = YES;
UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc]
initWithTarget:self action:#selector(showTimePicker:)];
[recognizer setNumberOfTapsRequired:1];
[recognizer setDelegate:self];
if(pickerView.tag == DOORSELECTION_PICKER_TAG){
NSLog(#"its door selection category");
*[pickerView addGestureRecognizer:recognizer];*
}
}
}
Instead of adding the tap gesture to the class scope picker view, add the gesture to this method picker view object returned in viewForRow delegate method after checking the tag.

Related

How to Add TableView With two Section to UIScrollView as a sub view

In my coding the table view will display but when i am scrolling the table view, it will completely scrolled. i have scrolled only table view cell. please any one help me. here is my code.
[super viewDidLoad];
scrollview=[[UIScrollView alloc]initWithFrame:CGRectMake(0,0,320,460)];
scrollview.showsVerticalScrollIndicator=NO;
scrollview.scrollEnabled=YES;
scrollview.userInteractionEnabled=YES;
[self.view addSubview:scrollview];
scrollview.contentSize = CGSizeMake(320,900);
[scrollview addSubview:recipePhoto];
[scrollview addSubview:detailLabel];
[scrollview addSubview:prizeLabel];
[scrollview addSubview:discountLabel];
[scrollview addSubview:saveLabel];
UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 480, 600, 400) style:UITableViewStylePlain];
[tableView setAutoresizesSubviews:YES];
[tableView setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
tableView.scrollEnabled = NO;
tableView.backgroundColor=[UIColor clearColor];
[tableView setDataSource:self];
[tableView setDelegate:self];
[scrollview addSubview:tableView];
[tableView reloadData];
I don't see any problem in the code provided by you. You might want to check other parts of views or share more info here so that I can help you.
I added your code in my sample project and i was able to scroll to see the table view. Below is the code I used (almost same as yours, commented the labels)
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
UIScrollView *scrollview=[[UIScrollView alloc]initWithFrame:CGRectMake(0,0,320,460)];
scrollview.showsVerticalScrollIndicator=NO;
scrollview.scrollEnabled=YES;
scrollview.userInteractionEnabled=YES;
[self.view addSubview:scrollview];
scrollview.backgroundColor = [UIColor grayColor];
scrollview.contentSize = CGSizeMake(320,900);
// [scrollview addSubview:recipePhoto];
// [scrollview addSubview:detailLabel];
// [scrollview addSubview:prizeLabel];
// [scrollview addSubview:discountLabel];
// [scrollview addSubview:saveLabel];
UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 480, 600, 400) style:UITableViewStylePlain];
[tableView setAutoresizesSubviews:YES];
[tableView setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
tableView.scrollEnabled = NO;
tableView.backgroundColor=[UIColor clearColor];
[tableView setDataSource:self];
[tableView setDelegate:self];
[scrollview addSubview:tableView];
[tableView reloadData];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *cellIdentifier = #"CellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.textLabel.text = [NSString stringWithFormat:#"cell %d", indexPath.row];
cell.backgroundColor = [UIColor blueColor];
return cell;
}
Here Two Way to complete that requirements are:
1.
just set contentSize of UIScrollView related to UITableView contentSize.height
just add this code after your above code
tableView.frame = CGRectMake(tableView.frame.origin.x, tableView.frame.origin.y, tableView.frame.size.width, tableView.contentSize.height);
float fscrview = tableView.frame.origin.y + tableView.contentSize.height + 20;
yourScrollView.contentSize=CGSizeMake(320, fscrview);
also set scrollEnabled = NO; to the tableView as you set in your code
and 2. When TableView is scrolled at that time set scrollEnabled = NO of UIScrollView.

how can i make uipickerview with multiple row selection and displaying liste options selected in uitextfield iphone?

i have to make a picker view with multiple row selection. i have tried this solution but i was failed for me.
-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
UITableViewCell *cell = (UITableViewCell *)view;
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:nil];
[cell setBackgroundColor:[UIColor clearColor]];
[cell setBounds:CGRectMake(0, 0, cell.frame.size.width - 20, 44)];
cell.tag = row ;
UITapGestureRecognizer * singleTapGestureRecognizer = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:#selector(toggleSelection:)];
singleTapGestureRecognizer.numberOfTapsRequired = 1;
[cell addGestureRecognizer:singleTapGestureRecognizer];
}
if ([self.selectedItems indexOfObject:[NSNumber numberWithInt:row]] != NSNotFound) {
[cell setAccessoryType:UITableViewCellAccessoryCheckmark];
} else {
[cell setAccessoryType:UITableViewCellAccessoryNone];
}
cell.textLabel.text = [self.options objectAtIndex:row];
return cell;
}
-(void)toggleSelection:(UITapGestureRecognizer *)recognizer {
NSNumber *row = [NSNumber numberWithInt:recognizer.view.tag];
NSUInteger index = [self.selectedItems indexOfObject:row];
if (index != NSNotFound) {
[self.selectedItems removeObjectAtIndex:index];
[(UITableViewCell *)(recognizer.view) setAccessoryType:UITableViewCellAccessoryNone];
} else {
[self.selectedItems addObject:row];
[(UITableViewCell *)(recognizer.view) setAccessoryType:UITableViewCellAccessoryCheckmark];
}
}
For multiple row selection in pickerView you can use https://github.com/alexleutgoeb/ALPickerView
for mutliple selection we have to create custom picker by using Tableview.once see this one

Detect UIImageView click in UITableViewCell

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.

When i touch the button inside the tableview it automatically goes to TabGesture method, but i want button touch method?

Currently i am working simple iPhone application, Using UITableview to enter the field like Emp_Name, Emp_Salary etc.. and using UIButton to set a image like checkbox and set target and perform selector,
When i touch the checkbox button, it automatically comes to UITabGesture method but not comes inside the button touch method. how to fix this, please help me
I tried the source code:
- (void)viewDidLoad
{
[super viewDidLoad];
UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeFrom:)];
[recognizer setNumberOfTouchesRequired:1];
recognizer.cancelsTouchesInView = NO;
[[self view] addGestureRecognizer:recognizer];
[recognizer release];
}
-(void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer // TabGesture method
{
NSLog(#"Tab received.");
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
if (indexPath.section == 0 && indexPath.row == 0)
{
perlessonCheckButton = [UIButton buttonWithType:UIButtonTypeCustom];
perlessonCheckButton.frame = CGRectMake(5, 5, 30, 30);
[perlessonCheckButton setImage:[UIImage imageNamed:#"tickAltered1.png"] forState:UIControlStateNormal];
[perlessonCheckButton addTarget:self action:#selector(showPerLessonOptions) forControlEvents:UIControlEventTouchUpInside];
[perlessonCheckButton setBackgroundColor:[UIColor lightTextColor]];
[perlessonCheckButton setUserInteractionEnabled:NO];
[cell.contentView addSubview:perlessonCheckButton];
}
}
-(void) showPerLessonOptions // Button method
{
[perlessonCheckButton setUserInteractionEnabled:NO];
[flatFeeCheckButton setUserInteractionEnabled:YES];
[perlessonCheckButton setImage:[UIImage imageNamed:#"tickAltered1.png"] forState:UIControlStateNormal];
[flatFeeCheckButton setImage:[UIImage imageNamed:#"untickAltered1.png"] forState:UIControlStateNormal];
}
-(void)handleSingleTap:(UIGestureRecognizer *)gestureRecognizer
{
CGPoint tapLocation = [gestureRecognizer locationInView:self.view];
if(CGRectContainsPoint(yourButton.frame, tapLocation))
{
[self showPerLessonOptions];
}
}
I think the problem is with this line,
[perlessonCheckButton setUserInteractionEnabled:NO];
The above line disabling the user interaction.
Change it to,
[perlessonCheckButton setUserInteractionEnabled:YES];
If the above method is not working then you can add a gesture recognizer to your button.
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(showPerLessonOptions:)];
[perlessonCheckButton addGestureRecognizer:tap];
[perlessonCheckButton setUserInteractionEnabled:YES];
For more about the UITapGestureRecognizer

uItableviewcell to retrieve value dynamically

i did an application which has imageview to capture image from camera and a button where button is clicked pickerView will appear by actionsheet and will choose type of image(jpg, png, bmp..), once select from picker, button title will change according to the pickerView result,
then i try to convert this to UITableView, which has 2 section ( 1 for image and 1 for select the type of the image,
now i'm having problem where i try to imageView into the cell but image was overlapping other cell, and once image capture from camera that imageView not updated but once scroll the table than only updated,
another problem is, once select 2nd section i able to call the pickerView to choose the image type as previously, but once select from pickerView i don't how to updated the cell.text
below is my code, can anyone help here or improve it,
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 1;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
switch (section) {
case 0:
return NSLocalizedString(#"Image", #"");
break;
case 1:
return NSLocalizedString(#"Image type", #"");
break;
default:
break;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:#"Cell"];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"Cell"] autorelease];
}
switch (indexPath.section) {
case 0:
{
UIImageView *abc = [[UIImageView alloc] initWithFrame:CGRectMake(5,5,100,100)];
abc.image = imageView.image;
[cell addSubview:abc];
}
break;
case 1:
cell.textLabel.text = #"- Click here -";
break;
default:
break;
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:#"Cell"];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:#"Cell"] autorelease];
}
if(indexPath.section == 0) {
NSLog(#"selected %d",indexPath.section);
}
if(indexPath.section == 1) {
NSLog(#"selected %d",indexPath.section);
[self chooseType];
}
}
- (void)chooseType{
actionSheet = [[UIActionSheet alloc] initWithTitle:#""
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
NSArray *typeObject = [NSArray arrayWithObjects:#".jpg",#".png",#".bmp",nil];
self.pickerData = typeObject;
pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0.0, 44.0, 0.0, 0.0)];
pickerView.delegate = self;
pickerView.showsSelectionIndicator = YES;
pickerDateToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
pickerDateToolbar.barStyle = UIBarStyleBlackOpaque;
[pickerDateToolbar sizeToFit];
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
[barItems addObject:flexSpace];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(TypeDonePick)];
[barItems addObject:doneBtn];
[pickerDateToolbar setItems:barItems animated:YES];
[actionSheet addSubview:pickerDateToolbar];
[actionSheet addSubview:pickerView];
[actionSheet showInView:self.view];
[actionSheet setBounds:CGRectMake(0,0,320, 464)];
[pickerView release];
[actionSheet release];
}
- (void)TypeDonePick
{
NSInteger row = [pickerView selectedRowInComponent:0];
titleCause.textColor = [UIColor blackColor];
titleCause.text = [pickerData objectAtIndex:row];
NSLog(#" %# ",titleCause.text);
[actionSheet dismissWithClickedButtonIndex:0 animated:YES];
}
is it possible to change cell test from outside range UITable function
thanks
Your problem is that the table doesn't know it needs to update the cells already visible. for a quick solution just add [tableView reloadData] after the user picks the image/image type, this will cause the table to recreate the cells and get the updated data from them.
If you want to update cells individually you can use [tableView cellForRowAtIndexPath:] to get the cell you want to update. e.g.
UITableViewCell *cell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:1]];
cell.textLabel.text = #"updated text";
// You might need to call the following function though I'm not sure
// [cell.textLabel setNeedsDisplay];
If you want to update the image I suggest you add a tag to the imageView in the cell and then you'll be able to change it as follows:
UITableViewCell *cell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
UIImageView *imageView = [cell viewWithTag:kImageViewTag];
imageView.image = updatedImage;
// Again you might need to tell the image to refresh it self
//[imageView setNeedsDisplay];
To change the LabelText of UITableViewCell at IndexPath, We can use the code below.
--> It works correctly!
(with this example, I change labelText at row 0 of tableView):
UITableViewCell *cell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
cell.textLabel.text = #"updated text";
// must to write line below to update labelText for row 0
[tableView setNeedsDisplay];