I am doing one registration form for iphone application. In that am having one tableview with four section.In last section am having one row with register button.I got the tableview with data well.But while scrolling the tableview up and down my register button goes to 2nd section.I added the code below.What wrong i did in this.Help me to solve.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
// static NSString *CellIdentifier = #"Cell";
// UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
NSString *identifier = #"reuseIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleGray;
NSLog(#" row and section ===== %d,%d",indexPath.row,indexPath.section);
if(indexPath.section != 3)
{
UILabel *lbl_title = [[UILabel alloc] initWithFrame:(isiPad?CGRectMake(7,10,250,50):CGRectMake(70,40,340, 35))];
[lbl_title setFont:[UIFont fontWithName:#"Verdana-Bold" size:(isiPad?20:13)]];
lbl_title.backgroundColor = [UIColor clearColor];
[lbl_title setTextColor:[UIColor blackColor]];
lbl_title.tag = ((indexPath.section+1)*10)+indexPath.row;
//NSLog(#"lbl data tag %d",lbl_title.tag);
lbl_title.textAlignment = UITextAlignmentLeft;
[cell.contentView addSubview:lbl_title];
UILabel *lbl_data = [[UILabel alloc] initWithFrame:(isiPad?CGRectMake(260,10,410,50):CGRectMake(70,40,340, 35))];
[lbl_data setFont:[UIFont fontWithName:#"Verdana-Bold" size:(isiPad?20:13)]];
lbl_data.backgroundColor = [UIColor clearColor];//25-25-112
[lbl_data setTextColor:[UIColor colorWithRed:25.0/255.0 green:25.0/255.0 blue:112.0/255.0 alpha:1.0]];
lbl_data.textAlignment = UITextAlignmentCenter;
lbl_data.tag = ((indexPath.section+1)*100)+indexPath.row;
//NSLog(#"lbl data tag %d,%#",lbl_data.tag,lbl_data);
[cell.contentView addSubview:lbl_data];
if(indexPath.section == 0)
{
if(indexPath.row == 0)
{
lbl_title.text = #"Email";
}
else if(indexPath.row == 1)
{
lbl_title.text = #"Password";
}
else if(indexPath.row == 2)
{
lbl_title.text = #"Confirm password";
}
}
else if(indexPath.section == 1)
{
if(indexPath.row == 0)
{
lbl_title.text = #"First name";
}
else if(indexPath.row == 1)
{
lbl_title.text = #"Last name";
}
else if(indexPath.row == 2)
{
lbl_title.text = #"Nickname";
}
else if(indexPath.row == 3)
{
lbl_title.text = #"Gender";
}
}
else if(indexPath.section == 2)
{
if(indexPath.row == 0)
{
lbl_title.text = #"City";
}
else if(indexPath.row == 1)
{
lbl_title.text = #"State";
}
else if(indexPath.row == 2)
{
lbl_title.text = #"Country";
}
}
}
if(indexPath.section == 3)
{
if(indexPath.row == 0)
{
cell.backgroundColor = [UIColor colorWithRed:165.0/255.0 green:42.0/255.0 blue:42.0/255.0 alpha:1.0];//165-42-42
UIButton *btn_register = [[UIButton alloc] initWithFrame:isiPad?CGRectMake(10,0,660,70):CGRectMake(0,0,320,40)];
[btn_register addTarget:self action:#selector(btn_register_clicked:) forControlEvents:UIControlEventTouchUpInside];
[btn_register setTitle:#"Register" forState:UIControlStateNormal];
//btn_register.titleLabel.textAlignment = UITextAlignmentCenter;
btn_register.backgroundColor = [UIColor blueColor];
[btn_register setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[btn_register setFont:[UIFont fontWithName:#"Verdana-Bold" size:24]];
[cell.contentView addSubview:btn_register];
}
}
}
else
{
NSLog(#"updating cell.........");
}
return cell;
}
It gets repeated because you added it to a cell's contentview, and then you re-use that cell.
Rather than adding it in cell's contentView, add the button as a cell accessory.
cell.accessoryView = btn_register;
And in all other cells, set accessoryView to nil.
cell.accessoryView = nil;
Why don't you try setting different identifiers for each section of cells. You can also use Use [tableView dequeueReusableCellWithIdentifier:forIndexPath:
Using a different cell identifier for the "Register" cell should work. And your code will be much clear if you use a container to hold the data, like an NSDictionary. For example:
NSArray *account = #[#"Email", #"Password", #"Confirm password"];
NSArray *userInfo = #[#"First name", #"Last name", #"Gender", #"Nickname"];
// ...
NSDictionary *data = #{#"Account": account, #"User Information": userInfo /* ... */};
Related
I am new to xcode5. And i used to group tableview in my app. it's work fine in iOS6.1 but doesn't show data in iOS7. It is just display "Header" section.
That is my code :
- (void)viewDidLoad
{
[super viewDidLoad];
[atableView reloadData];
self.notification = [[UISwitch alloc] initWithFrame:CGRectZero];
LoginSetting = [[NSArray alloc] initWithObjects:#"Company ID",#"Username",nil];
[LoginSetting retain];
mic = [[NSArray alloc] initWithObjects:#"Notification",#"App Version",#"Server",nil];
[mic retain];
detailText=[[NSArray alloc]initWithObjects:#"kaisquare",#"admin",nil];
[detailText retain];
logout=[[NSArray alloc]initWithObjects:#"",nil];
self.title = #"Setting";
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(section == 0)
return [LoginSetting count];
else if(section==1)
return [mic count];
else
return [logout count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
if(section == 0)
return #"Login Setting";
else if(section==1)
return #"Mics";
else
return #"";
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.section == 0)
{
if (indexPath.row == 0)
{
}
else if (indexPath.row == 1)
{
}
else
{
}
}
else if(indexPath.section==1)
{
if (indexPath.row == 0)
{
}
else if (indexPath.row == 1)
{
}
else if (indexPath.row == 2)
{
alertView = [[UIAlertView alloc] initWithTitle:#"Server" message:#"" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Okay", nil];
alertView.alertViewStyle = UIAlertViewStylePlainTextInput;
myTextField = [alertView textFieldAtIndex:0];
myTextField.text=mainString;
[alertView show];
[alertView release];
}
else
{
}
}
else
{
if (indexPath.row == 0)
{
}
}
}
-(void)alertView:(UIAlertView *)alertView
didDismissWithButtonIndex:(NSInteger)buttonIndex
{
if (buttonIndex != 1)
{
NSLog(#"Cancel");
return;
}
else
{
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:CellIdentifier
forIndexPath:indexPath];
if (cell == nil)
{
// cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.backgroundColor = [UIColor clearColor];
cellCompanyId = [[UILabel alloc] initWithFrame:CGRectMake(-20, 0, cell.frame.size.width, cell.frame.size.height)];
cellCompanyId.backgroundColor = [UIColor clearColor];
cellCompanyId.font = [UIFont systemFontOfSize:18];
cellCompanyId.lineBreakMode = NSLineBreakByWordWrapping;
[cell addSubview:cellCompanyId];
cellUsername = [[UILabel alloc] initWithFrame:CGRectMake(-20, 0, cell.frame.size.width, cell.frame.size.height)];
cellUsername.backgroundColor = [UIColor clearColor];
cellUsername.font = [UIFont systemFontOfSize:18];
cellUsername.lineBreakMode = NSLineBreakByWordWrapping;
[cell addSubview:cellUsername];
cellAppversion = [[UILabel alloc] initWithFrame:CGRectMake(-20, 0, cell.frame.size.width, cell.frame.size.height)];
cellAppversion.backgroundColor = [UIColor clearColor];
cellAppversion.font = [UIFont systemFontOfSize:18];
cellAppversion.lineBreakMode = NSLineBreakByWordWrapping;
[cell addSubview:cellAppversion];
cellServer = [[UILabel alloc] initWithFrame:CGRectMake(-20, 0, cell.frame.size.width, cell.frame.size.height)];
cellServer.backgroundColor = [UIColor clearColor];
cellServer.font = [UIFont systemFontOfSize:14];
cellServer.lineBreakMode = NSLineBreakByWordWrapping;
[cell addSubview:cellServer];
}
// Set up the cell...
if(indexPath.section == 0)
{
cell.textLabel.text = [LoginSetting objectAtIndex:indexPath.row];
if (indexPath.row == 0)
{
cellCompanyId.textAlignment=NSTextAlignmentRight;
cellCompanyId.text = strCompanyid;
}
else if (indexPath.row == 1)
{
cellUsername.textAlignment=NSTextAlignmentRight;
cellUsername.text = strUsertxt;
}
else
return 0;
}
else if(indexPath.section==1)
{
cell.textLabel.text = [mic objectAtIndex:indexPath.row];
if (indexPath.row == 0)
{
UISwitch *aSwitch = [[[UISwitch alloc]init]autorelease];
aSwitch.frame=CGRectMake(215, 7, 0, 0);
[aSwitch addTarget:self action:#selector(switchChanged:) forControlEvents:UIControlEventValueChanged];
[cell.contentView addSubview:aSwitch];
}
else if (indexPath.row == 1)
{
cellAppversion.textAlignment=NSTextAlignmentRight;
cellAppversion.text = #"1.3.1";
}
else if (indexPath.row == 2)
{
cellServer.textAlignment=NSTextAlignmentRight;
cellServer.text = mainString;
}
else
return 0;
}
else
{
if (indexPath.row == 0)
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:#selector(LogoutEvent)
forControlEvents:UIControlEventTouchUpInside];
UIImage *buttonImage = [UIImage imageNamed:#"login_button.png"];
[button setBackgroundImage:buttonImage forState:UIControlStateNormal];
button.frame = CGRectMake(0, 0, cell.frame.size.width, cell.frame.size.height);
[cell addSubview:button];
}
}
//UITableViewCellAccessoryCheckmark
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 35.0;
}
I don't understand why my tableViews is shown in xcode4, and xcode5 one isn't. I'm using Dynamic Prototype Content Grouped Style UITableView.
You must register a class or nib file using the registerNib:forCellReuseIdentifier: or registerClass:forCellReuseIdentifier: method before calling dequeueReusableCellWithIdentifier:forIndexPath: method.
I have a UITableView that I used for username/password enter.
But I have problem to get values when I click on button.
This is how I make table:
- (UITableViewCell *)tableView:(UITableView *)table cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *kCellIdentifier = #"Cell";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:kCellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:kCellIdentifier];
cell.accessoryType = UITableViewCellAccessoryNone;
if ([indexPath section] == 0) {
UITextField *playerTextField = [[UITextField alloc] initWithFrame:CGRectMake(110, 10, 185, 30)];
playerTextField.adjustsFontSizeToFitWidth = YES;
playerTextField.textColor = [UIColor blackColor];
if ([indexPath row] == 0) {
playerTextField.placeholder = #"example#gmail.com";
playerTextField.keyboardType = UIKeyboardTypeEmailAddress;
playerTextField.returnKeyType = UIReturnKeyNext;
}
else {
playerTextField.placeholder = #"Required";
playerTextField.keyboardType = UIKeyboardTypeDefault;
playerTextField.returnKeyType = UIReturnKeyDone;
playerTextField.secureTextEntry = YES;
}
playerTextField.backgroundColor = [UIColor whiteColor];
playerTextField.autocorrectionType = UITextAutocorrectionTypeNo;
playerTextField.autocapitalizationType = UITextAutocapitalizationTypeNone;
playerTextField.textAlignment = NSTextAlignmentLeft;
playerTextField.tag = 0;
playerTextField.clearButtonMode = UITextFieldViewModeNever;
[playerTextField setEnabled: YES];
playerTextField.backgroundColor = [UIColor clearColor];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
[cell addSubview:playerTextField];
}
}
if ([indexPath section] == 0) { // Email & Password Section
if ([indexPath row] == 0) { // Email
cell.textLabel.text = #"Email";
}
else {
cell.textLabel.text = #"Password";
}
}
return cell;
}
And this is what I have tried in button tap:
...
for (UITableViewCell* aCell in [self.tableView visibleCells] ) {
UITextField* aField = (UITextField *)[aCell viewWithTag:0];
NSLog(aField.text);
}
..
This prints me just: Email Password (labels not data that I entered).
How to get entered values here?
All views default their tag to 0. Because of this, [aCell viewWithTag: 0] is returning the first view with tag zero - in this case the cell's textLabel - which is a UILabel that also responds to .text. If you set the tags in your cellForRowAtIndexPath: to something other than zero it should start working for you.
In that case you can give the tag to textfield like playerTextField.tag = 115 and you can get view by tag like UITextField *aField = (UITextField *)[aCell viewWithTag:115]; and display NSLog(#"text:%#",aField.text);
-(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];
}
// NSLog(#"msgcnt123 %#\n",[messageCount objectAtIndex:indexPath.row]);
NSArray *seperateArray = [[clist objectForKey:[[clist allKeys]objectAtIndex:indexPath.row]]componentsSeparatedByString:#"#"];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
// NSLog(#"key %#\n",[[clist allKeys]objectAtIndex:indexPath.row]);
cell.textLabel.text = [seperateArray objectAtIndex:0];
// cell.textLabel.text = [contactlist objectAtIndex:indexPath.row];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
// NSLog(#"sep %#\n",seperateArray);
if (![[seperateArray objectAtIndex:1] isEqualToString:#"0"]) {
NSLog(#"msgCount %#\n",[seperateArray objectAtIndex:1]);
lblCnt = [[UILabel alloc]initWithFrame:CGRectMake(260, 13, 20, 20)];
lblCnt.backgroundColor = [UIColor lightGrayColor];
lblCnt.textColor = [UIColor blackColor];
lblCnt.text = [seperateArray objectAtIndex:1];
lblCnt.textAlignment = UITextAlignmentCenter;
lblCnt.layer.masksToBounds = YES;
lblCnt.layer.cornerRadius = 2.0f;
[cell.contentView addSubview:lblCnt];
lblCnt.tag = 1000;
}
else
{
/*NSLog(#"msgCount1 %#\n",[seperateArray objectAtIndex:1]);
[lblCnt removeFromSuperview];
lblCnt.hidden = YES;*/
for (UIView *view in [cell.contentView subviews])
{
if (view.tag == 1000)
{
[view removeFromSuperview];
}
}
}
return cell;
}
I am displaying contact name with a label in UITableView.The label displaying number of received messages for each contact.My view name is chatViewcontroller.If I am in that view that label count does not updated.For ex:For contact name "John" label count is "2".When John receives a message that label count should be updated as "3".It is not happening so.It is updated if i move come from another view.Is it possible to Update tableView chatviewController itself?.
Make sure that you updated your datasource(clist) and then call [tableview reloadData]
I know that if I have some images and subviews added in customized cell then I have to reuse the cell so that custom control won't appear on other cells but here I have other issue. I just want to have ImageView on first cell of first section so I have used IndexPath.Section==0 and IndexPath.Row==0 condition in following code but the problem is when I scroll table, the other cell will meet this condition and my code will create imageview on that cell as well. I have tried Tagging it and using same tagged cellView but it didn't help either. The cell issue is with disabling user interactions for few cells. Eventually after scrolling it disables user interactions for all cells. Is there anyway to resolve this?
Thanks.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
}
if(indexPath.section == 0 && indexPath.row == 0) {
UIImageView *imageView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"me.jpg"]] autorelease];
UIView *cellView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0,320,132)] autorelease];
[imageView setFrame: CGRectMake(10, 10, 54, 54)];
[cellView addSubview:imageView];
cell.backgroundView = cellView;
return cell;
} else if(indexPath.row == 0) {
NSString * title = [NSString string];
switch (indexPath.section) {
case 1:
title = #"Friends";
break;
case 2:
title = #"Accounts";
break;
case 3:
title = #"Stats";
break;
default:
title = nil;
break;
}
cell.textLabel.text = title;
cell.userInteractionEnabled = NO;
return cell;
}
cell.textLabel.text = #"Test";
return cell;
}
[RESOLVED] Correct code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
if(indexPath.section == 0 && indexPath.row == 0) {
UIImageView *imageView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"me.jpg"]] autorelease];
cell.imageView.image = imageView.image;
cell.textLabel.text = nil;
cell.textLabel.textColor = [UIColor clearColor];
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.userInteractionEnabled = YES;
return cell;
} else if(indexPath.row == 0) {
NSString * title = [NSString string];
switch (indexPath.section) {
case 1:
title = #"Friends";
break;
case 2:
title = #"Accounts";
break;
case 3:
title = #"Stats";
break;
default:
title = nil;
break;
}
cell.imageView.image = nil;
cell.textLabel.text = title;
cell.textLabel.textColor = [UIColor redColor];
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.userInteractionEnabled = NO;
return cell;
}
cell.imageView.image = nil;
cell.textLabel.text = [cellItems objectAtIndex:(rows+indexPath.row-1)];
cell.textLabel.textColor = [UIColor blueColor];
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.userInteractionEnabled = YES;
return cell;
}
[IMPROVED CODE]
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *NormalCellIdentifier = #"NormalCell";
static NSString *TitleCellIdentifier = #"TitleCell";
NSString *neededCellType;
if(indexPath.section == 0 && indexPath.row == 0) {
neededCellType = TitleCellIdentifier;
} else {
neededCellType = NormalCellIdentifier;
}
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:neededCellType];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:neededCellType] autorelease];
//Only add content to cell if it is new
if([neededCellType isEqualToString: TitleCellIdentifier]) {
UIImageView *imageView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"me.jpg"]] autorelease];
cell.imageView.image = imageView.image;
}
}
if([neededCellType isEqualToString: NormalCellIdentifier]) {
NSString * title;
if(indexPath.row == 0) {
switch (indexPath.section) {
case 1:
title = #"Friends";
break;
case 2:
title = #"Accounts";
break;
case 3:
title = #"Stats";
break;
default:
title = nil;
break;
}
cell.textLabel.text = title;
cell.textLabel.textColor = [UIColor redColor];
cell.userInteractionEnabled = NO;
} else {
cell.userInteractionEnabled = YES;
cell.textLabel.textColor = [UIColor blueColor];
cell.textLabel.text = #"Test";
}
}
return cell;
}
I think your problem is that the reuse of cells makes it so that the cells that aren't being created as new cells have some properties set that you must redefine. For instance, try assigning cell.userInteractionEnabled = YES to all other cases and see what the result is.
The problem is that you are not allowing for the possibility that the cell that was correctly showing the image gets reused later and the image view is still in there.
Here are two solutions:
set the tag value of the image view when you create it, then when you setup the cells, include code to check for and remove the old imageView if necessary.
assign different reuse identifiers to cells that need an image view and those that do not. Then make sure that you are only adding a new image view to cells when they are being created and not when they are being reused.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *NormalCellIdentifier = #"NormalCell";
static NSString *TitleCellIdentifier = #"TitleCell";
NSString *neededCellType;
if(indexPath.section == 0 && indexPath.row == 0) {
neededCellType = TitleCellIdentifier;
} else {
neededCellType = NormalCellIdentifier;
}
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:neededCellType];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:neededCellType] autorelease];
//Only add content to cell if it is new
if([neededCellType isEqualToString: TitleCellIdentifier]) {
UIImageView *imageView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"me.jpg"]] autorelease];
UIView *cellView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0,320,132)] autorelease];
[imageView setFrame: CGRectMake(10, 10, 54, 54)];
[cellView addSubview:imageView];
cell.backgroundView = cellView;
}
}
if([neededCellType isEqualToString: NormalCellIdentifier]) {
NSString * title;
if(indexPath.row == 0) {
switch (indexPath.section) {
case 1:
title = #"Friends";
break;
case 2:
title = #"Accounts";
break;
case 3:
title = #"Stats";
break;
default:
title = nil;
break;
}
cell.textLabel.text = title;
cell.userInteractionEnabled = NO;
}
else {
cell.textLabel.text = #"Test";
return cell;
}
}
}
(Those last few lines fell out of the code box). That should do it.
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.