TextField in UITableView - iphone

I'm new to iphone world .. help me out of this.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *MYIdentifier =#"MyIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MYIdentifier];
if(cell==nil)
{
cell=[[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MYIdentifier] autorelease];
}
CGRect frame =CGRectMake(5 ,10 , 320, 44);
UITextField *txtField = [[UITextField alloc]initWithFrame:frame];
[txtField setBorderStyle:UITextBorderStyleNone];
txtField.delegate=self;
switch (indexPath.row) {
case 0:
txtField.placeholder=editFrndBDb.frndName;
txtField.text=editFrndBDb.frndName;
txtField.tag=1;
break;
case 1:
txtField.placeholder=editFrndBDb.bDay;
txtField.text=editFrndBDb.bDay;
txtField.tag=2;
break;
case 2:
txtField.placeholder=editFrndBDb.frndNote;
txtField.text=editFrndBDb.frndNote;
txtField.tag=3;
break;
default:
break;
}
[cell.contentView addSubview:txtField];
[txtField release];
cell.selectionStyle=UITableViewCellSelectionStyleNone;
return cell;
}
-(IBAction ) saveChanges:(id) sender
{
UITextField *name =(UITextField *)[self.viewWithTag:1];
UITextField *bday= (UITextField *)[self.viewWithTag:2];
UITextField *note=(UITextField *)[self.viewWithTag:3];
NSInteger fid=editFrndBDb.friendId;
if(name.text==NULL)
name.text=#" ";
if(bday.text!=NULL)
bday.text=#" ";
if(note.text!=NULL)
note.text=#" ";
[editFrndBDb editFriendInfo:name.text frndBdayIs:bday.text frndNoteIs:note.text frndIdIs:fid];
}
getting error in saveChange Method in Statement
UITextField *name= (UITextField *)[self.viewWithTag:1];
error message : -
"viewWithTag is some thing not a structure or a union:"
help me out of this ...

You are incorrectly using the property syntax for a message
it should be
[self viewWithTag:3];
not
[self.viewWithTag:3];

Related

while i am entering text in one field it is diplaying same text in some other textfield too

When I am entering text in textfield 1 that text is also displayed in textfield 9.
here is some code what i did to create textfield in tableview cell.
i added textfield in tableview cell .like below in cellforRowatintexpath method
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"myCell";
//UILabel* nameLabel = nil;
cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
else
{
UILabel *titleLbl = (UILabel *)[cell viewWithTag:1];
[titleLbl removeFromSuperview];
}
nameLabel = [[UILabel alloc] initWithFrame:CGRectMake( 7.0, 10.0, 160.0, 44.0 )];
nameLabel.text = [labels objectAtIndex:indexPath.row];
nameLabel.font = [UIFont boldSystemFontOfSize:12];
nameLabel.lineBreakMode = UILineBreakModeWordWrap;
nameLabel.tag =1;
nameLabel.numberOfLines =0;
[nameLabel sizeToFit];
CGSize expectedlabelsize = [nameLabel.text sizeWithFont:nameLabel.font constrainedToSize:nameLabel.frame.size lineBreakMode:UILineBreakModeWordWrap];
CGRect newFrame =nameLabel.frame;
newFrame.size.height =expectedlabelsize.height;
[cell.contentView addSubview: nameLabel];
[nameLabel release];
//adding textfield to tableview cell.
tv = [[UITextField alloc] initWithFrame:CGRectMake(nameLabel.frame.origin.x+nameLabel.frame.size.width+2, 10.0, cell.contentView.bounds.size.width, 30)];
tv.tag = indexPath.row + 2;
//tv.text =[labels objectAtIndex:indexPath.row];
tv.font = [UIFont boldSystemFontOfSize:12];
//this method will take text from textfield of tableview cell using their individual tag
[tv addTarget:self action:#selector(getTextFieldValue:) forControlEvents:UIControlEventEditingDidEnd];
[cell.contentView addSubview:tv];
[tv setDelegate:self];
[tv release];
return cell;}
and here is the method getTextFieldValue method
- (void) getTextFieldValue:(UITextField *)textField{
if (textField.tag == 2) {
//NSString *value1 = [NSString stringWithFormat:#"%#",textField.text];
//NSLog(#"value1 is %#",value1);.
NSLog(#"2--->%#",textField.text);
}else if(textField.tag == 3){
NSLog(#"3--->%#",textField.text);
}else if(textField.tag == 4){
NSLog(#"4--->%#",textField.text);
}
else if(textField.tag == 5){
NSLog(#"5--->%#",textField.text);
}
else if(textField.tag == 6){
NSLog(#"6--->%#",textField.text);
}
else if(textField.tag == 7){
NSLog(#"7--->%#",textField.text);
}
else if(textField.tag == 8){
NSLog(#"8--->%#",textField.text);
}
else if(textField.tag == 9){
NSLog(#"9--->%#",textField.text);
}
else if(textField.tag == 10){
NSLog(#"10--->%#",textField.text);
}
else if(textField.tag == 11){
NSLog(#"11--->%#",textField.text);
}
else if(textField.tag == 12){
NSLog(#"12--->%#",textField.text);
} }
First of all, please rewrite your getTextFieldValue method like this:
- (void)getTextFieldValue:(UITextField *)textField {
NSLog(#"%d--->%#", textField.tag, textField.text);
}
You keep adding UITextFields to the cell, even when it's reused. Try putting that code inside the if statement.
You should set the text of the text field to nil every time though. Else it will still be there on dequeued cells (when scrolling).
Something like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"myCell";
cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake( 7.0, 10.0, 160.0, 44.0 )];
//TODO: Set up nameLabel here
[cell.contentView addSubview: nameLabel];
[nameLabel release];
UITextField *tv = [[UITextField alloc] init];
tv.font = [UIFont boldSystemFontOfSize:12];
[tv setDelegate:self];
[tv addTarget:self action:#selector(getTextFieldValue:) forControlEvents:UIControlEventEditingDidEnd];
[cell.contentView addSubview:tv];
[tv release];
}
UILabel *nameLabel = (UILabel *)[cell viewWithTag:1];
nameLabel.text = [labels objectAtIndex:indexPath.row];
[nameLabel sizeToFit];
return cell;
}
That is the retain problem is there.
You can take different cellIdentifier for each row. Only change one statement you get your solution.
Solve this problem with above method. i was facing same problem & i solve with this method.
I hope this will help you.
NSString *CellIdentifier = [NSString stringWithFormat:#"cellidentifier-%d",indexPath.row];
Maybe you have set some IBOutlet to the textField? Did you?

iphone:UItableviewcell duplicates when using table reload

My tableview gets duplicated when I use [tableview reloadData].I used this to refresh the tableview when I move back.How could I overcome the issue?
This is my code.
#define kMyTag 1
#define despTag 2
#define subTag 3
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[self.tableview reloadData];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
AsyncImageView *asyncImageView = nil;
UILabel *label = nil;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil] ;
UILabel *mainLabel=[[UILabel alloc]init];
mainLabel.tag = kMyTag; // define kMyTag in your header file using #define
mainLabel.frame=CGRectMake(95, 0, 170, 30);
mainLabel.font = [UIFont boldSystemFontOfSize:14];
mainLabel.backgroundColor=[UIColor clearColor];
[cell.contentView addSubview:mainLabel];
UILabel *despLabel=[[UILabel alloc]init];
despLabel.tag=despTag;
despLabel.frame=CGRectMake(95, 25, 190, 20);
despLabel.font= [UIFont systemFontOfSize:12];
despLabel.backgroundColor=[UIColor clearColor];
[cell.contentView addSubview:despLabel];
} else {
label = (UILabel *) [cell.contentView viewWithTag:LABEL_TAG];
}
switch([indexPath section]){
case 0:
{
NSString *urlString = [img objectAtIndex:indexPath.row];
urlString=[urlString stringByReplacingOccurrencesOfString:#" " withString:#"%20"];
tname=[mname objectAtIndex:indexPath.row];
tprice=[mprice objectAtIndex:indexPath.row];
tquan=[mquan objectAtIndex:indexPath.row];
tspice=[mspice objectAtIndex:indexPath.row];
UILabel *mainLabel=(UILabel *)[cell.contentView viewWithTag: kMyTag];
mainLabel.text = [NSString stringWithFormat:#"%#",tname];
UILabel *despLabel=(UILabel *)[cell.contentView viewWithTag: despTag];
despLabel.text = [NSString stringWithFormat:#"Qty :%# Spice: %#",tquan,tspice];
UILabel *subLabel=(UILabel *)[cell.contentView viewWithTag: subTag];
float pric=[tprice floatValue];
subLabel.text = [NSString stringWithFormat:#"Price :%# %.2f",currencyType, pric];
}
}
}
Overcomes the issue by removing all the objects from array,then reselects the DB table & reloads uitableview.
it happens because you are not passing any value in dequeueReusableCellWithIdentifier
pass a string value and then use it in reuseIdentifier also

Accordion table cell - How to dynamically expand/contract uitableviewcell?

I am trying create an accordion type of uitableviewcell that, when the user selects the cell, it expands to display a detailed info view inline similar to how the digg app works. I initially tried replacing the current tablecell with a customcell in cellForRowAtIndex, however the animation looks a bit choppy as you can see the cell being replaced and overall the effect doesn't work too well.
If you look at the digg app and others who have done this it seems that they aren't replacing the current cell but instead perhaps adding a subview to the cell? The original cell however doesn't seem to animate at all and only the new view accordions into the table.
Does anyone have any ideas how to accomplish a similar effect?
I have made some progress using neha's method below and while the cell is animating the correct way it is wreaking havoc with the other cells in the table. What I have done is subclassed UITableViewCell with a custom class which contains an instance of a UIView which actually draws the view which I then add to the table cell's contentview.
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
if (selected) {
[self expandCell];
}
}
-(void)expandCell {
self.contentView.frame = CGRectMake(0.0, 0.0, self.contentView.bounds.size.width, 110);
}
Here are all the table delegate methods I am using:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (isSearching && indexPath.row == selectedIndex) {
static NSString *CellIdentifier = #"SearchCell";
CustomTableCell *cell = (CustomTableCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[CustomTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
[cell setCustomTitle:[timeZoneNames objectAtIndex:indexPath.row] detail:[timeZoneNames objectAtIndex:indexPath.row]];
UILabel *theText = [[UILabel alloc] initWithFrame:CGRectMake(10.0, 10.0, cell.contentView.bounds.size.width -20, 22.0)];
theText.text = #"Title Text";
[cell.contentView addSubview:theText];
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(10.0, 10 + 46.0, cell.contentView.bounds.size.width - 20, 40.0)];
textField.borderStyle = UITextBorderStyleLine;
[cell.contentView addSubview:textField];
UILabel *testLabel = [[UILabel alloc] initWithFrame:CGRectMake(5.0, 88.0, cell.contentView.bounds.size.width - 20, 22.0)];
testLabel.text = [NSString stringWithFormat:#"Some text here"];
[cell.contentView addSubview:testLabel];
[theText release];
[textField release];
[testLabel release];
return cell;
} else {
static NSString *CellIdentifier = #"Cell";
CustomTableCell *cell = (CustomTableCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[CustomTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
[cell setCustomTitle:[timeZoneNames objectAtIndex:indexPath.row] detail:[timeZoneNames objectAtIndex:indexPath.row]];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:NO];
selectedIndex = indexPath.row;
isSearching = YES;
[tableView beginUpdates];
[tableView endUpdates];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (isSearching && indexPath.row == selectedIndex) {
return 110;
}
return rowHeight;
}
It seems now that the cell is expanding but not actually being refreshed so the labels, and textfield aren't being shown. They do however show up when I scroll the cell off and on the screen.
Any ideas?
The Apple way to do is quite simple.
First, you'll need to save the selected indexPath row:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
self.selectedRowIndex = [indexPath retain];
[tableView beginUpdates];
[tableView endUpdates];
}
I'll explain the begin/end updated part later.
Then, when you have the currently selected index, you can tell the tableView that it should give that row more space.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
//check if the index actually exists
if(selectedRowIndex && indexPath.row == selectedRowIndex.row) {
return 100;
}
return 44;
}
This will return height 100 for the selected cell.
Now we can go back to the begin/end updates. That block triggers the reload of all tableView geometry. Moreover, that block is animated, which eventually gives the impressions of the row expanding.
Pawel's beginUpdates/endUpdates trick is good, and I often use it. But in this case you simply need to reload the rows that are changing state, ensuring that you correctly reload them with the desired cell type, and that you return the correct new cell height.
Here is a complete working implementation of what I think you're trying to accomplish:
.h:
#import <UIKit/UIKit.h>
#interface ExpandingTableViewController : UITableViewController
{
}
#property (retain) NSIndexPath* selectedIndexPath;
#end
.m:
#implementation ExpandingTableViewController
#synthesize selectedIndexPath;
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return 10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier1 = #"Cell1";
static NSString *CellIdentifier2 = #"Cell2";
UITableViewCell *cell;
NSIndexPath* indexPathSelected = self.selectedIndexPath;
if ( nil == indexPathSelected || [indexPathSelected compare: indexPath] != NSOrderedSame )
{
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1] autorelease];
}
cell.textLabel.text = [NSString stringWithFormat: #"cell %d", indexPath.row];
}
else
{
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier2];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier2] autorelease];
}
cell.textLabel.text = [NSString stringWithFormat: #"cell %d", indexPath.row];
cell.detailTextLabel.text = [NSString stringWithFormat: #"(expanded!)", indexPath.row];
}
return cell;
}
#pragma mark -
#pragma mark Table view delegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if ( self.selectedIndexPath != nil && [self.selectedIndexPath compare: indexPath] == NSOrderedSame )
{
return tableView.rowHeight * 2;
}
return tableView.rowHeight;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSArray* toReload = [NSArray arrayWithObjects: indexPath, self.selectedIndexPath, nil];
self.selectedIndexPath = indexPath;
[tableView reloadRowsAtIndexPaths: toReload withRowAnimation: UITableViewRowAnimationMiddle];
}
#pragma mark -
#pragma mark Memory management
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)viewDidUnload {
}
- (void)dealloc {
[super dealloc];
}
#end
If you don't want to reload the cell (you want to keep your existing cell and just change the size, and likely add/remove some subviews), then simply do the beginUpdates/endUpdates trick in didSelectRowAtIndexPath:, and call some method on your cell to incite the layout change. beginUpdates/endUpdates will prompt the tableView to re-query the heights for each cell - so be sure to return the correct value.
Create a class that subclasses UITableviewcell in your project. Create this class' nib and set its parent to be the class in your project with tableview and override its -
(void)setSelected:(BOOL)selected animated:(BOOL)animated
Write methods contractCell() and expandCell() in this class, and provide the height of the cells you want in expandCell method. Call this methods appropriately based on some flags set to identify wheather the cell is in expanded state or contracted state. Use your tableview's
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
method to handle selection of cells.
Replace your cellForRowAtIndexPath function with this one.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath
*)indexPath {
if (isSearching && indexPath.row == selectedIndex) {
static NSString *CellIdentifier = #"SearchCell";
CustomTableCell *cell = [[[CustomTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
[cell setCustomTitle:[timeZoneNames objectAtIndex:indexPath.row] detail:[timeZoneNames objectAtIndex:indexPath.row]];
UILabel *theText = [[UILabel alloc] initWithFrame:CGRectMake(10.0,
10.0, cell.contentView.bounds.size.width
-20, 22.0)];
theText.text = #"Title Text";
[cell.contentView addSubview:theText];
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(10.0, 10 +
46.0, cell.contentView.bounds.size.width - 20, 40.0)];
textField.borderStyle = UITextBorderStyleLine;
[cell.contentView addSubview:textField];
UILabel *testLabel = [[UILabel alloc] initWithFrame:CGRectMake(5.0,
88.0, cell.contentView.bounds.size.width - 20, 22.0)];
testLabel.text = [NSString stringWithFormat:#"Some text here"];
[cell.contentView addSubview:testLabel];
[theText release];
[textField release];
[testLabel release];
return cell;
} else {
static NSString *CellIdentifier = #"Cell";
CustomTableCell *cell = [[[CustomTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
[cell setCustomTitle:[timeZoneNames objectAtIndex:indexPath.row] detail:[timeZoneNames objectAtIndex:indexPath.row]];
return cell;
}
}
create array wof dictionary which have a key Select_sts which is 0 in start when click its change 1
accourding u change table
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
customView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 40.0)];
UILabel * headerLabel = [[UILabel alloc] initWithFrame:CGRectZero];
headerLabel.backgroundColor = [UIColor clearColor];
headerLabel.opaque = NO;
headerLabel.textColor = [UIColor blackColor];
headerLabel.highlightedTextColor = [UIColor whiteColor];
headerLabel.font = [UIFont boldSystemFontOfSize:16];
headerLabel.frame = CGRectMake(5.0, 10.0, 300.0, 20.0);
headerLabel.text=[NSString stringWithFormat: #"PNR %#",[[record objectAtIndex:section] objectForKey:#"number"]];
customView.backgroundColor=[UIColor whiteColor];
btn_openClose.tag=section+10000;
btn_openClose.backgroundColor=[UIColor clearColor];
// [btn_openClose setImage:[UIImage imageNamed:#"down_arrow.png"] forState:UIControlStateNormal];
[btn_openClose addTarget:self action:#selector(collapseExpandButtonTap:) forControlEvents:UIControlEventTouchUpInside];
[customView addSubview:btn_openClose];
}
- (void) collapseExpandButtonTap:(id) sender{
int indexNo=[sender tag]-10000;
// NSLog(#"total_record %#",[total_record objectAtIndex:indexNo]);
NSMutableDictionary *mutDictionary = [[total_record objectAtIndex:indexNo] mutableCopy];
if([[mutDictionary objectForKey:#"Select_sts"] integerValue]==0)
[mutDictionary setObject:[NSNumber numberWithInt:1] forKey:#"√"];
else
[mutDictionary setObject:[NSNumber numberWithInt:0] forKey:#"Select_sts"];
[total_record replaceObjectAtIndex:indexNo withObject:mutDictionary];
// [table_view beginUpdates];
// [table_view reloadData];
// [table_view endUpdates];
NSMutableIndexSet *indetsetToUpdate = [[NSMutableIndexSet alloc]init];
[indetsetToUpdate addIndex:indexNo]; // [indetsetToUpdate addIndex:<#(NSUInteger)#>]
// You can add multiple indexes(sections) here.
[table_view reloadSections:indetsetToUpdate withRowAnimation:UITableViewRowAnimationFade];
}

iphone keyboard won't appear

I can click on the field, and it momentarily turns blue, but those events plus makeFirstResponder together do not cause the keyboard to show on a UITextField.
Plain vanilla code follows; I include it so others can discern what is NOT there and therefore what, presumably, with solve the problem.
I put in leading spaces to format this question more like code, but the parser seems to have stripped them, thus leaving left-justified code. Sorry!
UITextFieldDelete, check:
#interface RevenueViewController : UIViewController <UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate> {
UILabel *sgmntdControlLabel;
UISegmentedControl *sgmntdControl;
UITableView *theTableView;
}
#property(nonatomic,retain) UISegmentedControl *sgmntdControl;
#property(nonatomic,retain) UILabel *sgmntdControlLabel;
Delegate and source, check.
-(void)loadView;
// code
CGRect frameTable = CGRectMake(10,0,300,260);
theTableView = [[UITableView alloc] initWithFrame:frameTable style:UITableViewStyleGrouped];
[theTableView setDelegate:self];
[theTableView setDataSource:self];
// code
[self.view addSubview:theTableView];
[super viewDidLoad];
}
Insert UITextField to cell, check.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSInteger sectionNmbr = [indexPath section];
NSInteger rowNmbr = [indexPath row];
NSLog(#"cellForRowAtIndexPath=%d, %d",sectionNmbr,rowNmbr);
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell.
switch (sectionNmbr) {
case 0:
if (rowNmbr == 0) {
cell.tag = 1;
cell.textLabel.text = #"Label for sctn 0, row 0";
UITextField *tf = [[UITextField alloc] init];
tf.keyboardType = UIKeyboardTypeNumberPad;
tf.returnKeyType = UIReturnKeyDone;
tf.delegate = self;
[cell.contentView addSubview:tf];
}
if (rowNmbr == 1) {
cell.tag = 2;
cell.textLabel.text = #"Label for sctn 0, row 1";
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
}
break;
}
}
Successfully end up where we want to be (?), no check, (and no keyboard!):
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"didSelectRowAtIndexPath");
switch (indexPath.section) {
case 0:
NSLog(#" section=0, rowNmbr=%d",indexPath.row);
switch (indexPath.row) {
case 0:
UITableViewCell *cellSelected = [tableView cellForRowAtIndexPath: indexPath];
UITextField *textField = [[cellSelected.contentView subviews] objectAtIndex: 0];
[ textField setEnabled: YES ];
[textField becomeFirstResponder];
// here is where keyboard will appear?
[tableView deselectRowAtIndexPath: indexPath animated: NO];
break;
case 1:
// code
break;
default:
break;
}
break;
case 1:
// code
break;
default:
// handle otherwise un-handled exception
break;
}
}
Thank you for your insights!
are you sure that
[[cellSelected.contentView subviews] objectAtIndex: 0];
is returning what you expect it to? Try checking it with the debugger

Tags in TextField

I have three cells, within that I have each textfields. Now I want the user is clicking in which textbox. This method textFieldDidEndEditing gives me the value which user is inputting but I don't get any tag of the textfield.
Here is my code:
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{ switch (section)
{ case 0: return #"";
case 1: return #"";
default:return #"";
}
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
if(optconscodecount != 0 && gotOK == 2)
return 2;
else
return 1;
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
switch(section)
{ case 0: try=1; return conscodecount;
case 1: try=2; return optconscodecount;
default:return 0;}
}
// Heights per row
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{ int section = [indexPath section];
//int row = [indexPath row];
switch (section)
{case 0:return 80.0f;
case 1:return 80.0f;
default:return 80.0f;}
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"\n%#", appDelegate.conscodeNameArray);
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
// Set up the cell...
row = [indexPath row];
section = [indexPath section];
switch (section)
{case 0:try=1;cell = [tableView dequeueReusableCellWithIdentifier:#"textCell"];
if (!cell)
{cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:#"textCell"] autorelease];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20.0f, 10.0f, 280.0f, 20.0f)];
[label setText:[appDelegate.conscodeNameArray objectAtIndex:row]];[cell addSubview:label]; [label release];
[cell addSubview:[[UITextField alloc] initWithFrame:CGRectMake(20.0f, 40.0f, 280.0f, 30.0f)]];
}
UITextField *tf = [[cell subviews] lastObject];
tf.placeholder = [appDelegate.conscodeNameArray objectAtIndex:row];
tf.tag =tagcount;
tagcount=tagcount+1;
tf.delegate = self;
tf.borderStyle = UITextBorderStyleRoundedRect;
return cell;
break;
case 1:
try=2;
cell = [tableView dequeueReusableCellWithIdentifier:#"textCell"];
if (!cell)
{
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:#"textCell"] autorelease];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20.0f, 10.0f, 280.0f, 20.0f)];
[label setText:[appDelegate.optconscodeNameArray objectAtIndex:row]];
[cell addSubview:label];
[label release];
[cell addSubview:[[UITextField alloc] initWithFrame:CGRectMake(20.0f, 40.0f, 280.0f, 30.0f)]];
}
UITextField *my = [[cell subviews] lastObject];
my.tag = 0;
my.tag = my.tag+1;
my.placeholder = [appDelegate.optconscodeNameArray objectAtIndex:row];
my.delegate = self;
my.borderStyle = UITextBorderStyleRoundedRect;
return cell;
break;
return cell;
break;
default:
break;
}
return cell;
}
nameField.tag = 1;
agefield.tag = 2;
// in the delegate method just check
if (textField.tag == 1) {
NSLog(#" clicked in Name field");
} else if (textField.tag ==2) {
NSLog(#" clicked in Age field");
}
If you were to retain each text field in an instance variable, you could do a simple comparison in your delegate method to see which field had finished editing. You can do this even if you create them completely dynamically by using an associative array to map from the instance to the field name, or vice-versa.