In my application, I have used one modal viewcontroller which contains one TableView with multiple textfields.
Say My main viewcontroller called SridharViewController and the modal viewcontroller called ResultsViewController
There is one button in SridharViewController , touching that button will open the modal(ResultsViewController)
[self presentModalViewController:resultsViewController animated:YES];
And in the results view controller , There are many text fields which are aligned in the TableView. and one 'OK' button . pressing 'OK' button will dismiss the modal
[self dismissModalViewControllerAnimated:YES];
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"cellForRowAtIndexPath called");
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
//UITextField *formTextField = [[UITextField alloc] initWithFrame:CGRectMake(30, 11, 270, 30)];
UITextField *formTextField =nil;
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
if(indexPath.row == 0){
formTextField = [[UITextField alloc] initWithFrame:CGRectMake(30, 12, 270, 30)];
}
else{
formTextField = [[UITextField alloc] initWithFrame:CGRectMake(30, 11, 270, 30)];
}
}else{
if(indexPath.row == 0){
formTextField = [[UITextField alloc] initWithFrame:CGRectMake(50, 12, 670, 30)];
}
else{
formTextField = [[UITextField alloc] initWithFrame:CGRectMake(50, 11, 670, 30)];
}
}
formTextField.placeholder = [self.formFields objectAtIndex:indexPath.row];
formTextField.tag = indexPath.row;
formTextField.text=#"";
formTextField.adjustsFontSizeToFitWidth = YES;
formTextField.textColor = [UIColor blackColor];
formTextField.borderStyle = UITextBorderStyleNone;
formTextField.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
[formTextField setAutocapitalizationType:UITextAutocapitalizationTypeNone];
[formTextField setAutocorrectionType:UITextAutocorrectionTypeNo];
formTextField.backgroundColor = [UIColor clearColor];
formTextField.textAlignment = UITextAlignmentLeft;
formTextField.delegate = self;
[formTextField setEnabled: YES];
[formTextField setReturnKeyType:UIReturnKeyNext];
[self.textFields addObject:formTextField];
[cell addSubview:formTextField];
return cell;
}
When I calling the modal view second time , the modal shows the previous content. I want to clear that previous content.
I have tried both updates and reload functions. None of them calling the cellForRowAtIndexPath method.
[self.theTable reloadInputViews];
Please provide me the best way to do this.
In viewWillAppear do the following
arrayOfInputs = [NSArray array];
[self.tableview reloadData];
where arrayOfInputs is where you store your objects that would display on tableview. After making the data source empty and reload table view, all the contents will clear. Then you can reload the table view with required data.
When you want to reload tableview please use below line
[self.tableview reloadData]
Add this code in ViewDidLoad method this will clear all textfield text
for (UIView *view in [self.view subviews]) {
if ([view isKindOfClass:[UITextField class]]) {
UITextField *textField = (UITextField *)view;
textField.text = #"";
}
}
I solved my problem by this code
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSLog(#" cellForRowAtIndexPath create new cell %d",indexPath.row);
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
NSLog(#" cellForRowAtIndexPath update cell %d",indexPath.row);
//UITextField *formTextField = [[UITextField alloc] initWithFrame:CGRectMake(30, 11, 270, 30)];
for(UITextField *lbl in [cell subviews])
{
NSLog(#" removing UITextField from cell %d",indexPath.row);
if(lbl.tag == (indexPath.row+2)){
[lbl removeFromSuperview];
}
}
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
if(indexPath.row == 0){
self.tableTextField = [[UITextField alloc] initWithFrame:CGRectMake(30, 12, 270, 30)];
}
else{
self.tableTextField = [[UITextField alloc] initWithFrame:CGRectMake(30, 11, 270, 30)];
}
}else{
if(indexPath.row == 0){
self.tableTextField = [[UITextField alloc] initWithFrame:CGRectMake(50, 12, 670, 30)];
}
else{
self.tableTextField = [[UITextField alloc] initWithFrame:CGRectMake(50, 11, 670, 30)];
}
}
self.tableTextField .placeholder = [self.formFields objectAtIndex:indexPath.row];
self.tableTextField .tag = indexPath.row+2;
self.tableTextField .adjustsFontSizeToFitWidth = YES;
self.tableTextField .textColor = [UIColor blackColor];
self.tableTextField .borderStyle = UITextBorderStyleNone;
self.tableTextField .keyboardType = UIKeyboardTypeNumbersAndPunctuation;
[self.tableTextField setAutocapitalizationType:UITextAutocapitalizationTypeNone];
[self.tableTextField setAutocorrectionType:UITextAutocorrectionTypeNo];
self.tableTextField .backgroundColor = [UIColor clearColor];
self.tableTextField .textAlignment = UITextAlignmentLeft;
self.tableTextField .delegate = self;
self.tableTextField .text=#"";
[self.tableTextField setEnabled: YES];
[self.tableTextField setReturnKeyType:UIReturnKeyNext];
[cell addSubview:self.tableTextField ];
return cell;
}
Related
how can i return keyboard on textfield while i have put the textfield on cell.contentview , i am using textfieldshouldreturn methodbut its not working So please tell me how can i solve it ?
this is my code for set textfield on tableview cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellidentitifier = #"cell";
UITableViewCell * cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellidentitifier];
if (cell ==nil)
{
cell = [tableView dequeueReusableCellWithIdentifier:cellidentitifier];
}
else
{
[cell prepareForReuse];
}
UIImageView *imgview = [[UIImageView alloc]initWithFrame:CGRectMake(30, 10, 70, 70)];
imgview.backgroundColor = [UIColor blackColor];
[cell.contentView addSubview:imgview];
UITextField *_nameTF = [[UITextField alloc]initWithFrame:CGRectMake(120, 10, 160, 22)];
_nameTF.placeholder =#"Client Name";
_nameTF.layer.borderWidth = 1.0;
[cell.contentView addSubview:_nameTF];
_nameTF = nil;
UITextField *_timeTF = [[UITextField alloc]initWithFrame:CGRectMake(120, 37, 160, 22)];
_timeTF.placeholder = #"Time";
_timeTF.layer.borderWidth= 1.0;
[cell.contentView addSubview:_timeTF];
_timeTF = nil;
UITextField *_PhoneTF = [[UITextField alloc]initWithFrame:CGRectMake(120, 64, 160, 22)];
_PhoneTF.placeholder = #"Phone";
_PhoneTF.layer.borderWidth= 1.0;
[cell.contentView addSubview:_PhoneTF];
_PhoneTF = nil;
return cell;
}
set delegates to textfield in cellForRowAtIndexPath as below, before return cell statement.
_nameTF.delegate = self;
_timeTF.delegate = self;
_PhoneTF.delegate = self;
and
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
}
and to your viewcontroller set delegate as below in .h file
#interface myviewVC : UIViewController<UITextFieldDelegate>
in my app i have an UITableView with 120 rows and every row has 1 UItextfeilds and 1 Buttons as show in the code bellow :
-(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];
}
else
{
}
UITextField *NameTextField = [[UITextField alloc] initWithFrame:CGRectMake(0, 10, 230, 28)];
NameTextField.borderStyle = UITextBorderStyleRoundedRect;
NameTextField.delegate = self;
NameTextField.textColor = UIColorFromRGB(0x2A1807);
NameTextField.font = [UIFont fontWithName:#"Helvetica" size:(17.0)];
NameTextField.font = [UIFont boldSystemFontOfSize:20];
NameTextField.tag = [indexPath row ];
NSString *temp = [self.sectionNames objectAtIndex:[indexPath section]];
NameTextField.text = [[self.myDataArray objectForKey:temp] objectAtIndex:[indexPath row]];
[cell.contentView addSubview:NameTextField];
[NameTextField release];
UIButton * aBtn = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *wijzigImage = [UIImage imageNamed:#"btn_delete.png"];
aBtn.frame = CGRectMake(240, 10, 28, 26);
[aBtn setImage:wijzigImage forState:UIControlStateNormal];
[aBtn addTarget:self action:#selector(deleteCustomCellWithUIButton:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:aBtn];
return cell;
}
i have noticed that the Scrolling is slow and It doesn't go fluently.
any idea ?
Thanks
That's because you create textfields and buttons every time, add it inside if (cell == nil) {...}. The only thing that should be left outside that if is textField text setting.
Btw, your textfield leaks.
I found the solution :
if (cell == nil) {
cell = [[[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier] autorelease];
}
else
{
UITextField *oldTextField = (UITextField *)[cell.contentView viewWithTag:999];
[oldTextField removeFromSuperview];
UIButton *oldBtn = (UIButton *)[cell.contentView viewWithTag:888];
[oldBtn removeFromSuperview];
}
NameTextField.tag = 999;
aBtn.tag = 88;
tableview problem: i am using 3 uilable for displaying productname, some description and image. all data displayed but when scrolling the table the labels are filled with another text with the actual text.. how can we handle this?
Code:
- (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];
}
UIImageView *imageview = [[UIImageView alloc]initWithFrame:CGRectMake(2, 2, 41, 41)];
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
NSDictionary *aDict1 = [[NSDictionary alloc]init];
aDict1 = [tableData objectAtIndex:indexPath.row];
NSString *prdStus = [aDict1 objectForKey:#"ProductStatus"];
NSLog(#"product status is %# ",prdStus);
if ([prdStus isEqualToString:#"Orange"]) {
[imageview setImage:[UIImage imageNamed:#"Yellow.png"]];
}
if ([prdStus isEqualToString:#"Green"]) {
[imageview setImage:[UIImage imageNamed:#"Green.png"]];
}
if ([prdStus isEqualToString:#"Red"]) {
[imageview setImage:[UIImage imageNamed:#"Red.png"]];
}
UILabel *label2 = [[UILabel alloc]initWithFrame:CGRectMake(46, 0, 220, 12) ];
label2.font = [UIFont systemFontOfSize:12];
label2.text = #"";
if (indexPath.row <[tableData count]) {
label2.text = [aDict1 objectForKey:#"ProductName"];
}
[cell addSubview:label2];
[cell addSubview:imageview];
label2.backgroundColor =[UIColor clearColor];
UILabel *label3 = [[UILabel alloc]initWithFrame:CGRectMake(46, 13, 220, 30) ];
label3.font = [UIFont systemFontOfSize:10];
label3.text = #"";
label3.text = [aDict1 objectForKey:#"ProductDescription"];
[cell addSubview:label3];
return cell;
}
please tell me how to avoid this..
Grouped Table view.
Here is also i am facing same problem.
I am using 4 section
1st and 3rd sections 1 row each,
2nd sec 3 rows,
4th sec 5
when configure the text the first section label displayed on 4th and 3rd section data also displayed on 4th section.
Code
- (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.userInteractionEnabled =NO;
if (indexPath.section == 0)
{
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(12, 2, 294, 40) ];
label.backgroundColor = [UIColor clearColor];
NSDictionary *aDict1 = [[NSDictionary alloc]init];
aDict1 = [detailsArray objectAtIndex:0];
label.text=#"";
label.text =[aDict1 objectForKey:#"ProductName"];
label.numberOfLines = 2;
label.lineBreakMode = UILineBreakModeWordWrap;
[cell addSubview:label];
[label release];
// [cell addSubview:imgview1];
// [imgview1 release];
}
if (indexPath.section ==1 ) {
if (indexPath.row == 0)
{
imgview = [[UIImageView alloc]initWithFrame:CGRectMake(255,2 , 46, 46)];
[cell addSubview:imgview];
[imgview release];
cell.textLabel.text = #"Actual Halal Rating";
NSDictionary *aDict1 = [[NSDictionary alloc]init];
aDict1 = [detailsArray objectAtIndex:0];
NSString *statStr=[[NSString alloc] init];
statStr = [aDict1 objectForKey:#"ProductHalalStatus"];
NSLog(#"status is %#",statStr);
imgview1 = [[UIImageView alloc]initWithFrame:CGRectMake(255,2 , 20, 20)];
if ([statStr isEqualToString:#"Red"]) {
[imgview1 setImage:[UIImage imageNamed:#"Red.png"]];
}
if ([statStr isEqualToString:#"Orange"] ) {
[imgview1 setImage:[UIImage imageNamed:#"Yellow.png"]];
}
if ([statStr isEqualToString:#"Green"]) {
[imgview1 setImage:[UIImage imageNamed:#"Green.png"]];
}
[cell addSubview:imgview1];
[imgview1 release];
}
if (indexPath.row == 1) {
cell.textLabel.text = #"Halal (Permisible)";
[imgview setImage:[UIImage imageNamed:#"Green.png"]];
}
if (indexPath.row == 2) {
cell.textLabel.text = #"Masbooh (Doubtful)";
[imgview setImage:[UIImage imageNamed:#"Yellow.png"]];
}
if (indexPath.row == 3) {
cell.textLabel.text = #"Haram (Not Permisible)";
[imgview setImage:[UIImage imageNamed:#"Red.png"]];
}
}
if (indexPath.section == 2) {
NSDictionary *aDict2 = [[NSDictionary alloc]init];
aDict2 = [detailsArray objectAtIndex:0];
// NSArray *ingrArr =[aDict2 objectForKey:#"IngredientInfo1"];
textview1 =[[UITextView alloc]initWithFrame:CGRectMake(12, 2, 294, 96)];
//textview1.text = ingrStr;
textview1.editable =NO;
[textview1 setFont:[UIFont systemFontOfSize:15]];
[cell addSubview:textview1];
[textview1 release];
}
if (indexPath.section == 3) {
}
return cell;
}
Remove the line, from you code ....
if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
and add the following line
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]
I had a problem to set the selection style as blue, I have added the cell back ground image so that i can not set the selection style as blue how can i make it if we added images to each cell.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = (UITableViewCell *)[myTableView dequeueReusableCellWithIdentifier:#"MyIdentifier"];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:#"MyIdentifier"] autorelease];
UIView* elementView = [ [UIView alloc] initWithFrame:CGRectMake(5,5,300,480)];
elementView.tag = 0;
[cell.contentView addSubview:elementView];
[elementView release];
}
cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
cell.selectionStyle=UITableViewCellSelectionStyleNone;
UIView* elementView = [cell.contentView viewWithTag:0];
for(UIView* subView in elementView.subviews)
{
[subView removeFromSuperview];
}
UIImageView* cellBGImageView=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 65)];
cellBGImageView.image=[UIImage imageNamed:#"cellbg.png" ];
[elementView addSubview:cellBGImageView];
[cellBGImageView release];
Customer *objectForCustomer=[customerList objectAtIndex:indexPath.row];
UILabel *nameLable =[[UILabel alloc] initWithFrame:CGRectMake(10, 7, 280, 20)];
[nameLable setFont:[UIFont boldSystemFontOfSize:17]];
nameLable.textAlignment=UITextAlignmentLeft;
//nameLable.textColor = [UIColor blackColor];
nameLable.numberOfLines = 0;
nameLable.tag=2;
nameLable.backgroundColor = [UIColor clearColor];
nameLable.text=objectForCustomer.customerName;
[elementView addSubview:nameLable];
[nameLable release];
return cell;
}
I know that when you have a custom cell, you can point it in IB. Don't really know if it is possible from code (I guess it should).
Try using this in your code:
[cell setBackgroundView:cellBGImageView.image];
I am sceptic about if it works, but worth a try.
When you are covering entire cell with image, you cannot see the cell selected.
you have to implement custom cell for that to handle (or show) that some cell is selected.
Hey I have sen a problem in your code that you are setting the selectionStyle of cell as none.
The problrm is not with the image but the problem is with the code.
I am rewriting the code for you convinience. Please check it out
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{ UITableViewCell *cell = (UITableViewCell )[myTableView dequeueReusableCellWithIdentifier:#"MyIdentifier"];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:#"MyIdentifier"] autorelease];
UIView elementView = [ [UIView alloc] initWithFrame:CGRectMake(5,5,300,480)];
elementView.tag = 0;
[cell.contentView addSubview:elementView];
[elementView release];
}
cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
//THIS IS THE LINE I HAVE COMMENTED THAT WAS CREATING THE PROBLEM
// cell.selectionStyle=UITableViewCellSelectionStyleNone;
UIView* elementView = [cell.contentView viewWithTag:0];
for(UIView* subView in elementView.subviews)
{
[subView removeFromSuperview];
}
UIImageView* cellBGImageView=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 65)];
cellBGImageView.image=[UIImage imageNamed:#"cellbg.png" ];
[elementView addSubview:cellBGImageView];
[cellBGImageView release];
Customer *objectForCustomer=[customerList objectAtIndex:indexPath.row];
UILabel *nameLable =[[UILabel alloc] initWithFrame:CGRectMake(10, 7, 280, 20)];
[nameLable setFont:[UIFont boldSystemFontOfSize:17]];
nameLable.textAlignment=UITextAlignmentLeft; //nameLable.textColor = [UIColor blackColor]; nameLable.numberOfLines = 0;
nameLable.tag=2;
nameLable.backgroundColor = [UIColor clearColor];
nameLable.text=objectForCustomer.customerName;
[elementView addSubview:nameLable];
[nameLable release];
return cell;
}
hAPPY cODING...
How to put a UITextField inside of a UITableViewCell (grouped)? I want a user to be able to edit it.
Add the UITextField as an subview of the UITableViewCell's contentView:
[mycell.contentView addSubview:view];
Apple's own UICatalog demo application has an example of placing UITextFields in Grouped UITableView Cells: http://developer.apple.com/iphone/library/samplecode/UICatalog/index.html
Check the contents of TextFieldController.m
Plus there's lots of other great code there for working with UIKit Objects.
This is how I've implemented it into my application, however you'll obviously need to change a few things. Hope this helps.
- (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];
}
// Configure the cell.
//adding all the UITextField's to the UITableViewCell is a pain in the ass. Pretty sure this is correct though.
if ([indexPath section] == 0) {
tUser = [[UITextField alloc] initWithFrame:CGRectMake(110, 10, 185, 30)];
tUser.adjustsFontSizeToFitWidth = YES;
tUser.textColor = [UIColor blackColor];
tPass = [[UITextField alloc] initWithFrame:CGRectMake(110, 10, 185, 30)];
tPass.adjustsFontSizeToFitWidth = YES;
tPass.textColor = [UIColor blackColor];
if ([indexPath section] == 0) {
if ([indexPath row] == 0) {
tUser.placeholder = #"#JohnAppleseed";
tUser.keyboardType = UIKeyboardTypeEmailAddress;
tUser.returnKeyType = UIReturnKeyNext;
}
if ([indexPath row] == 1) {
tPass.placeholder = #"Required";
tPass.keyboardType = UIKeyboardTypeDefault;
tPass.returnKeyType = UIReturnKeyDone;
tPass.secureTextEntry = YES;
}
}
tUser.backgroundColor = [UIColor whiteColor];
tUser.autocorrectionType = UITextAutocorrectionTypeNo;
tUser.autocapitalizationType = UITextAutocapitalizationTypeNone;
tUser.textAlignment = UITextAlignmentLeft;
tPass.backgroundColor = [UIColor whiteColor];
tPass.autocorrectionType = UITextAutocorrectionTypeNo;
tPass.autocapitalizationType = UITextAutocapitalizationTypeNone;
tPass.textAlignment = UITextAlignmentLeft;
tUser.clearButtonMode = UITextFieldViewModeNever;
tPass.clearButtonMode = UITextFieldViewModeNever;
[tUser setEnabled:YES];
[tPass setEnabled:YES];
//[tUser release];
//[tPass release];
}
if ([indexPath section] == 0) { // Email & Password Section
if ([indexPath row] == 0) { // Email
cell.textLabel.text = #"Username";
[cell addSubview:tUser];
[tUser setText:[[NSUserDefaults standardUserDefaults] objectForKey:#"twitter_name_preference"]];
}
else {
cell.textLabel.text = #"Password";
[cell addSubview:tPass];
[tPass setText:[[NSUserDefaults standardUserDefaults] objectForKey:#"twitter_pass_preference"]];
}
}
return cell; }
Hope it helps.