Creating the UIpickerview in tableview button click. When click the each button first time the picker want to show . if i click the same button in another time picker want to hide and get the picker value. here i put my code only the last cell index done the process the first cell index is not working? how can i fix this issues help me!!! here screen!.
create the picker in global like
UIPickerView *myPickerView;` and acess in tableview
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [_arr_tags count]; /// in tag array number of button 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:nil];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
/// create a button
UIImage *ButtonImagecmt = [UIImage imageNamed:#"orangeButton#2x.png"];
UIButton *myButtoncmt = [UIButton buttonWithType:UIButtonTypeRoundedRect];
myButtoncmt.frame = CGRectMake(10,50,90,35);
[myButtoncmt setBackgroundImage:ButtonImagecmt forState:UIControlStateNormal];
[myButtoncmt addTarget:self action:#selector(picker_select:)forControlEvents:UIControlEventTouchDown];
[cell addSubview:myButtoncmt];
flg=1; /// first time picker want to hide so flag set 1.
myPickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(120, 0, 160, 180)];
myPickerView.delegate = self;
myPickerView.showsSelectionIndicator = YES;
myPickerView.hidden=true;
[cell addSubview:myPickerView];
}
return cell;
}
- (IBAction)picker_select:(id)search
{
if (flg==1)
{
/// first time button clickt picker want to display
myPickerView.hidden=false;
flg=0;
}
else
{
/// secound time button click picker want to hide
myPickerView.hidden=true;
flg=1;
}
}
This code only working in last cell. want to work in all the cell in table view
First let me explain your error after that I will suggest you other way.
You are declaring UIPickerView *myPickerView; in out side of following method
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
But you are initializing myPickerView inside of following method
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
and again you are trying to access myPickerView outside of the following method
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
simply the object myPickerView represents last cell pickerview. so what ever button you tapped in table view, myPickerView always represents last cell pickerview. As per reusability concept, you can't see this picker until you are loading last cell on screen.
so what I am suggesting you to work with your code
set index row as tag to your button like bellow in cellForRowAtIndexPath method
myButtoncmt.tag = indexpath.row;
now in the same way set tag to your pickerview also(I am not recommended this but I am trying to resolve issue with your code ) like bellow
myPickerView.tag = [_arr_tags count]+indexpath.row;
Now in - (IBAction)picker_select:(id)search access that using following code
UIButton *tempBtn =(UIButton *) search;
UITableViewCell *cell= [_tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:tempBtn.tag inSection:0]]
UIPickerView *pickerView = [cell viewWithTag:[_arr_tags count]+tempBtn.tag];
pickerView.hidden = !pickerView.isHidden;
Now you can see all pickers. try and let me know your result
Related
I have an NSArray and a tableView with some buttons whose title is the string in the array at the current indexpath row
- (CustomCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexpath {
_selectedRow = indexpath.row;
static NSString *simpleTableIdentifier = #"customCell";
cell = [myTableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
[cell.playButton setTitle:array[indexpath.row] forState: UIControlStateNormal];
return cell;
}
Button titles are well shown.
Now I have some mp3 files whose names are the same as the strings in the array, and I want to play the file corresponding to the selected cell.
fileToPlay = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:#"%#", array[_selectedRow]]; ofType:#"mp3"];
What's happening here is that the file played is always the one corresponding to the last visible cell in the table.
I also have
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexpath {
_selectedRow = indexpath.row;
}
but If I try to print _selectedRow here, nothing appears in the log.
When I click on a cell in the table, it doesn't seem selected (it's not gray colored).
dataSource and delegate are also well connected to the tableview.
UPDATE
I found out that if I click on the button, it's like I'm not clicking on the selected row. if I click on the cell (outside the button), the indexPath.row is correct.
Remove the following line from the cellForRowAtIndexPath method.
_selectedRow = indexpath.row;
You are setting the _selectedRow value each time the cellForRowAtIndexPath is called,which is when each cell is drawn and explains why the value of the last cell is being taken.
Set Code:
- (CustomCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexpath {
cell.playButton.tag = indexpath.row;
[cell.playButton addTarget:self action:#selector(btnPlay:) forControlEvents:UIControlEventTouchUpInside];
}
- (IBAction)btnPlay:(id)sender{
UIButton *btn = (UIButton *)sender;
_selectedRow = btn.tag;
}
You are select selection color for touch color set.
Try these:
1) set myGestureRecognizer.cancelsTouchInView to false... maybe your touches are getting in way. (It's a common issue when you may have gesture recognizers)
2) In your tableView's attribute's inspector, set Selection to singleton. That solved my issue
3) set this : [tableView setAllowsSelection:YES];
I am displaying a TabelCell that has images, button, UIView. When click the button it has to show UIView.
UIView is displaying but the problem is, if I click the button in first cell instead of displaying the UIView in the first cell, it is displaying the second cell or in some other cell. I don't know why this is happening.
The code is given below
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"HomeTablecell";
cell = (HomeTableCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
cell.vEdit.hidden=TRUE; //UIView hidden initially
[cell.editButton addTarget:self action:#selector(EditButtonClicked:) forControlEvents:UIControlEventTouchUpInside]; //When this button is clicked cell.vEdit become visible
}
-(void)EditButtonClicked:(UIButton*)sender
{
cell.vEdit.hidden=FALSE;
}
The answer for my question is FPPopover, when I use that control, the view is displaying exactly in the position I wanted
https://github.com/50pixels/FPPopover/
1: make sure the cell is referenced correctly(for instance I added tag to the button clicked)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"HomeTablecell";
cell = (HomeTableCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
cell.vEdit.hidden=YES; //UIView hidden initially
cell.editBUtton.tag = indexPath.row //suppose has 1 section, you can customise the tag rule based on section/row
[cell.editButton addTarget:self action:#selector(EditButtonClicked:) forControlEvents:UIControlEventTouchUpInside]; //When this button is clicked cell.vEdit become visible
}
-(void)EditButtonClicked:(UIButton*)sender
{
HomeTableCell* theCell = self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:sender.tag inSection:0];
theCell.vEdit.hidden =NO;
}
2: Make sure UIView is hidden in your customised cell class for reused cell
//Your cell class
- (void)prepareForReuse {
[super prepareForReuse];
self.vEdit.hidden=YES;
}
I have a UIButton in each of my UITableViewCells. When the button is pressed I am disabling it so that the user cannot press it again (it is a like button). However, when the user scrolls passed a cell and the scrolls back to the cell the button is selectable once again. I'm guessing this is because the cell is redrawn when the user comes back to it, resetting the button. Is there a way I can avoid this? My code is below
Code
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifer = #"cellName";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifer];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifer];
}
UIButton *ilikeit = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[ilikeit addTarget:self action:#selector(like:) forControlEvents:UIControlEventTouchUpInside];
ilikeit.frame = CGRectMake(55, h+70, 45, 25);
[cell addSubview:ilikeit];
return cell;
}
-(void) like:(id) sender {
((UIButton *)sender).enabled = NO;
}
You can store the button state in your data model for the tableview class.
Say the TableView is loading data from a class MyData which looks like this:
#interface MyData: NSObject
// Your other data here such as strings etc
NSString *otherData;
// Here add a selected flag
BOOL selected;
#end
in the TableViewDelegate, read and write to this model.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
MyData *thisCellData = [yourGlobalMyDataArray objectAtIndex:indexPath.row];
// set other data
if (thisCellData.selected){
// Hide the button
// Do other stuff as needed for this button
}
else {
// Show the button
// Do other stuff as needed for this button
}
}
The same data model should be updated when user selects the button.
Hope this helps.
Problem- 1 You are creating your button every time cellForRowAtIndexPath gets called.
Problem- 2 You have no code to keep the state of your button with cell reusability, that's why it is refreshed when cell is going for reuse.
Solution- For this you have to keep your selected button state. Make a global NSInteger variable. And set its value as sender.tag value in like: method. And use -
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifer];
// here create your UIButton
// Set its tag to indexPath.row
// add this to cell as subview
}
// find button of current cell using tag
UIButton *btn = (UIButton*)[cell.contentView viewWithTag:indexPath.row];
// place your check here if button tag matches with selected tag then disable btn here.
// Else use setEnable with YES parameter.
}
I have UITableViewController with a static table view and 2 sections. The first section has two cells, with a layout Table View Cell > Content View > Text Field.
The second section of table view cells has a layout that is Table View Cell > Content View > Label - and the cells expand when they're selected by using beginUpdates and endUpdates and reporting cell heights via tableView:heightForRowAtIndexPath:.
Right now, when I touch one of the first sections' cells, the keyboard comes up and the cursor is placed in the text field, but the cell is not selected - which means that if I had previously selected one of the cells in the second section, it would remain selected (and therefore expanded).
Also, if I have selected a cell in the first section with the text field given first responder, then I select a cell in the second section, the cell expands, but the first cell does not resign first responder and dismiss the keyboard.
Did I layout my table view incorrectly such that the cell selected states are not managed automatically and if not: what is the proper way to manage the selected states of the UITableViewCells?
I dont know how you have written the code. But from above information I have created one sample application. The code is as below and it is working as you want.
I have considered the table with 2 section.
First section has the textfield on Cell.
Second section has the UIlabel on the cell.
txtFieldCopy is of type UITextField declared in .h of class.
Please check the way I have assigned the Tag to the view as we need it to get the indexpath.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCellStyle style =UITableViewCellStyleSubtitle;
static NSString *MyIdentifier = #"MyIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
int iTag=[indexPath row]+1;
txtFieldCopy=nil;
if(indexPath.section==1)
{
UILabel *lblFirst=nil;
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:style reuseIdentifier:MyIdentifier];
lblFirst = [[UILabel alloc] initWithFrame:CGRectMake(45, 0, 100,
40)] ;
lblFirst.tag=iTag;
[cell.contentView addSubview:lblFirst];
lblFirst=nil;
}
lblFirst = (UILabel *)[cell.contentView viewWithTag:iTag];
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
lblFirst.text =#"Label 1";
}
else
{
UITextField *txtFirst=nil;
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:style reuseIdentifier:MyIdentifier];
txtFirst = [[UITextField alloc] initWithFrame:CGRectMake(40, 0, 50,
40)] ;
txtFirst.tag=iTag;
txtFirst.delegate=(id)self;
[cell.contentView addSubview:txtFirst];
txtFirst=nil;
}
txtFirst = (UITextField *)[cell.contentView viewWithTag:iTag];
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
txtFirst.text =#"text 1";
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(txtFieldCopy)
{
[txtFieldCopy resignFirstResponder];
txtFieldCopy=nil;
}
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
NSLog(#"%d",textField.tag);
NSIndexPath *iRowId =[NSIndexPath indexPathForRow:(textField.tag-1) inSection:0];
[tblSample selectRowAtIndexPath:iRowId animated:NO scrollPosition:UITableViewScrollPositionMiddle];
txtFieldCopy=textField;
}
Make sure if suppose in between you are reloading the table view then txtFieldCopy=nil; in the cellForRowAtIndexPath get called and set to null. so you can ignore this line. But before please check whether you are calling it again and again.
Let me know if it worked for you or not. I am sure it definitely work for you
I have a master detail app in ios, with SDK 7.0, Xcode 5, using ARC.
master has many items, detail has a table view. When I click on an item, the contents of tableview will change. This works well until I put a UITextField in each cell, because I want to edit the contents in the table.
The problem is: when I click on a new item, the old contents don't disappear,so the contents of a cell is a superposition of the new UITextField's text and the old UITextField's text.
The first normal tableview like this:
After I click on an new item, the tableview will like this:
The snippet of codes of master is:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
LHBPoetry *poetry = poetryArray[indexPath.row];
self.detailViewController.poetryId = poetry.poetryId;
}
I have tried a lot of things in the above method, for example, I make all instances of the detail view controller to be nil; table view's data array removeAllObejects; table view reloadData; It can't fix the problem.
The snippet of detail is:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"detailCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
UITextField *textField = textField = [[UITextField alloc] initWithFrame:
CGRectMake(90, 12, 200, 25)];
textField.tag = indexPath.row;
textField.text =_sentenceArray[indexPath.row];
textField.clearsOnBeginEditing = NO;
textField.delegate = self;
textField.returnKeyType = UIReturnKeyDone;
[textField addTarget:self
action:#selector(textFieldDone:)
forControlEvents:UIControlEventEditingDidEnd];
[cell.contentView addSubview:textField];
textField.text = _sentenceArray[indexPath.row];
return cell;
}
I draw this tableview in Main.storyborad, It has a prototype cell with an identifier.
Any help will be appreciated.
k there is something i want to tell, wy because u are keep on adding the textfields for reused cells, there is not one textfield in the cell ..:) there are more then one text field's, because of that u are getting overlapped with one other, i think u are using default "master- detail" application, and modifying it..:)
oky for that u need to modify like below
in master controller
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell"];
if(cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"Cell"];
UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(2, 3, 300, 30)];
[textField addTarget:self action:#selector(textFieldDone:) forControlEvents:UIControlEventEditingDidEnd]; //hear u are adding once initially
textField.tag = 100;
[cell addSubview:textField];
}
NSString *object = _objects[indexPath.row];//_objects is mutable array holding the sentences or names
UITextField *textField = (UITextField *)[cell viewWithTag:100];//after that u are reusing the textfields
textField.text = object;
textField.tag = indexPath.row;
return cell;
}
now you are creating the cell thats wy u dont want the prototype cell remove it from story board
in the above u removed the custom cell becz u are creating the cell in the code it self
now in the method
- (void) textFieldDone:(UITextField *)inTextFIeld
{
int index = inTextFIeld.tag;
[_objects replaceObjectAtIndex:index withObject:[inTextFIeld text]];
[self.masterTableVIew reloadData];//made connection to ur tableview
}