Loading text in Label inside cell UITableView - ios

I have defined 3 sections with different numbers of cell on each. It works but when editing the text still there. I tried to clear the content when start typing but didn't work.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
UITextField *fdates = [[UITextField alloc] initWithFrame:CGRectMake(110, 10, 185, 30)];
fdates.adjustsFontSizeToFitWidth = YES;
UITextField *fdeparture= [[UITextField alloc] initWithFrame:CGRectMake(110, 10, 185, 30)];
fdeparture.adjustsFontSizeToFitWidth = YES;
UITextField *farrival = [[UITextField alloc] initWithFrame:CGRectMake(110, 10, 185, 30)];
farrival.adjustsFontSizeToFitWidth = YES;
UITextField *faircraft = [[UITextField alloc] initWithFrame:CGRectMake(110, 10, 185, 30)];
faircraft.adjustsFontSizeToFitWidth = YES;
if ([indexPath section] == 0) {
if ([indexPath row] == 0) {
fdates.text = fplan.dates;
fdates.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
fdates.returnKeyType = UIReturnKeyNext;
}
}else if ([indexPath section] == 1) {
if (indexPath.row == 0) {
fdeparture.text = fplan.depAirport;
fdeparture.keyboardType = UIKeyboardTypeDefault;
fdeparture.returnKeyType = UIReturnKeyNext;
}else {
farrival.text = fplan.destAirport;
farrival.keyboardType = UIKeyboardTypeDefault;
farrival.returnKeyType = UIReturnKeyNext;
}
}else {
if (indexPath.row == 0) {
faircraft.text = fplan.aircraft_id;
faircraft.keyboardType = UIKeyboardTypeDefault;
faircraft.returnKeyType = UIReturnKeyNext;
// faircraft.
}
}
[cell.contentView addSubview:fdates];
[cell.contentView addSubview:fdeparture];
[cell.contentView addSubview:farrival];
[cell.contentView addSubview:faircraft];
return cell;
}
To be more clear:
Thanks!!

try following code. i have edited your code use this
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
UITextField *fdates = [[UITextField alloc] initWithFrame:CGRectMake(110, 10, 185, 30)];
fdates.adjustsFontSizeToFitWidth = YES;
if ([indexPath section] == 0) {
if ([indexPath row] == 0) {
fdates.text = fplan.dates;
fdates.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
fdates.returnKeyType = UIReturnKeyNext;
}
}else if ([indexPath section] == 1) {
if (indexPath.row == 0) {
fdates.text = fplan.depAirport;
fdates.keyboardType = UIKeyboardTypeDefault;
fdates.returnKeyType = UIReturnKeyNext;
}else {
fdates.text = fplan.destAirport;
fdates.keyboardType = UIKeyboardTypeDefault;
fdates.returnKeyType = UIReturnKeyNext;
}
}else {
if (indexPath.row == 0) {
fdates.text = fplan.aircraft_id;
fdates.keyboardType = UIKeyboardTypeDefault;
fdates.returnKeyType = UIReturnKeyNext;
// faircraft.
}
}
[cell.contentView addSubview:fdates];
return cell;
}

Try to add on cell not on content view....for more info see below....
[cell addSubview:fdates];
[cell addSubview:fdeparture];
[cell addSubview:farrival];
[cell addSubview:faircraft];

Related

Tableview not showing any of the arrays

I'm trying to create a in app setting tableview, which requires different sections with different content. i first tried all the code outside (!cell), but that resulted in duplicate of the content on scroll, i've then found thread where it said i needed to put inside (!cell), but now it does not show any content. What am i doing wrong?
CellForRowAtIndexPath
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *MyIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (!cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:MyIdentifier];
cell.backgroundColor = [UIColor colorWithRed:0.953 green:0.953 blue:0.965 alpha:1] ;
self.emailTextField.delegate = self;
self.cellName = [[UILabel alloc] initWithFrame:CGRectMake(20, 12, 42, 20)];
self.cellName.textColor = [UIColor colorWithRed:0.137 green:0.145 blue:0.157 alpha:1];
self.cellName.font = [UIFont fontWithName:#"HelveticaNeue" size:14];
[cell.contentView addSubview:self.cellName];
if (indexPath.section == 1) {
cell.selectionStyle = UITableViewCellSelectionStyleNone;
self.cellName.text = [notArray objectAtIndex:indexPath.row];
if (indexPath.row == 0) {
self.acceptanceSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(251, 6, 31, 51)];
[cell.contentView addSubview:self.acceptanceSwitch];
} else if (indexPath.row == 1) {
self.messageSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(251, 6, 31, 51)];
[cell.contentView addSubview:self.messageSwitch];
} else if (indexPath.row == 2) {
self.likeSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(251, 6, 31, 51)];
[cell.contentView addSubview:self.likeSwitch];
}
} else if (indexPath.section == 0) {
cell.selectionStyle = UITableViewCellSelectionStyleNone;
self.cellName.text = [accountArray objectAtIndex:indexPath.row];
if (indexPath.row == 0) {
self.nameTextField = [[UITextField alloc] initWithFrame:CGRectMake(110, 7, 190, 30)];
self.nameTextField.enabled = NO;
[cell.contentView addSubview:self.nameTextField];
} else if (indexPath.row == 1) {
self.emailTextField = [[UITextField alloc] initWithFrame:CGRectMake(110, 7, 190, 30)];
[cell.contentView addSubview:self.emailTextField];
[self.emailTextField setReturnKeyType:UIReturnKeyDone];
[self.emailTextField setKeyboardType:UIKeyboardTypeEmailAddress];
} else if (indexPath.row == 2) {
self.genderSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(251, 6, 31, 51)];
[cell.contentView addSubview:self.genderSwitch];
}
}
}
if (indexPath.section == 0) {
cell.selectionStyle = UITableViewCellSelectionStyleNone;
self.cellName.text = [accountArray objectAtIndex:indexPath.row];
} else if (indexPath.section == 1) {
cell.selectionStyle = UITableViewCellSelectionStyleNone;
self.cellName.text = [notArray objectAtIndex:indexPath.row];
} else if (indexPath.section == 2) {
self.cellName.text = [policyArray objectAtIndex:indexPath.row];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
} else if (indexPath.section == 3) {
self.cellName.text = [buttonsArray objectAtIndex:indexPath.row];
self.cellName.textAlignment = NSTextAlignmentCenter;
}
return cell;
}
Please try the following:
create a subclass of UITableViewCell with public properties (label, textfield). If you have different requirement for cells in different sections, maybe you should have different cell subclasses.
register these cell classes with the tableview in your view did load. e.g.
[self.tableView registerClass:[MySectionOneTableViewCell class] forCellReuseIdentifier:MySectionOneIdentifier];
[self.tableView registerClass:[MySectionTwoTableViewCell class] forCellReuseIdentifier:MySectionTwoIdentifier];
in the method below, just dequeue the cell after making a check for the section and set the properties.
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath )indexPath {
UITableViewCell cell;
if (indexPath.section == 0){
cell = (MySectionOneTableViewCell*)[tableView dequeueReusableCellWithIdentifier:MySectionOneIdentifier forIndexPath:indexPath;
cell.textLabel = #"foo";
}
return cell;
}
implement method, prepare for reuse in your custom uitableviewcell classes where you initialize all your labels and text fields. This method prepares the views of the cell for reuse.
e.g.
(void)prepareForReuse {
self.textLabel.text = #"";
}
As mentionned #rmaddy, you shouldn't mention the cell generation in your (!cell). The "code illustration" is the following:
- (void)viewDidLoad {
// This part wasn't included in any specific if/else condition dealing with cell properties (like section or row), not either being modified during the cell generation
self.emailTextField.delegate = self;
self.cellName = [[UILabel alloc] initWithFrame:CGRectMake(20, 12, 42, 20)];
self.cellName.textColor = [UIColor colorWithRed:0.137 green:0.145 blue:0.157 alpha:1];
self.cellName.font = [UIFont fontWithName:#"HelveticaNeue" size:14];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
// This part ensure you will initiate each cell with the correct identifier
if (!cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier];
cell.backgroundColor = [UIColor colorWithRed:0.953 green:0.953 blue:0.965 alpha:1] ;
}
[cell.contentView addSubview:self.cellName];
// You should check your code and commute the common condition together
if (indexPath.section == 0) {
cell.selectionStyle = UITableViewCellSelectionStyleNone;
self.cellName.text = [accountArray objectAtIndex:indexPath.row];
// Be aware that your first section will contain only 3 rows
if (indexPath.row == 0) {
self.nameTextField = [[UITextField alloc] initWithFrame:CGRectMake(110, 7, 190, 30)];
self.nameTextField.enabled = NO;
[cell.contentView addSubview:self.nameTextField];
} else if (indexPath.row == 1) {
self.emailTextField = [[UITextField alloc] initWithFrame:CGRectMake(110, 7, 190, 30)];
[cell.contentView addSubview:self.emailTextField];
[self.emailTextField setReturnKeyType:UIReturnKeyDone];
[self.emailTextField setKeyboardType:UIKeyboardTypeEmailAddress];
} else if (indexPath.row == 2) {
self.genderSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(251, 6, 31, 51)];
[cell.contentView addSubview:self.genderSwitch];
}
} else if (indexPath.section == 1) {
cell.selectionStyle = UITableViewCellSelectionStyleNone;
self.cellName.text = [notArray objectAtIndex:indexPath.row];
// Be aware that your second section will contain only 3 rows
if (indexPath.row == 0) {
self.acceptanceSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(251, 6, 31, 51)];
[cell.contentView addSubview:self.acceptanceSwitch];
} else if (indexPath.row == 1) {
self.messageSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(251, 6, 31, 51)];
[cell.contentView addSubview:self.messageSwitch];
} else if (indexPath.row == 2) {
self.likeSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(251, 6, 31, 51)];
[cell.contentView addSubview:self.likeSwitch];
}
} else if (indexPath.section == 2) {
// Here you are not adding any specific content to your section 2
self.cellName.text = [policyArray objectAtIndex:indexPath.row];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
} else if (indexPath.section == 3) {
// The same, your fourth section won't content any specific content
// You should add something like [cell addSubview:MyCustomView]
self.cellName.text = [buttonsArray objectAtIndex:indexPath.row];
self.cellName.textAlignment = NSTextAlignmentCenter;
}
return cell;
}
As I mentioned in my comments, if your local properties (cellName, nameTextField, genderSwitch, etc...) are well implemented, you will have only 6 row in all your table (3 in the first section and 3 in the second. Your third and fourth section will be empty since you didn't mention any subview within them.
Finally, since your code contained many confusion for me, it is possible I made some mistake in my rewriting, so feel free to correct me in this case.
Hopping it will help you in your work.

Memory Leak UITableView

SOLUTION
Just read the answer of #Kjuly
Thanks alot
QUESTION
I used tableView with sections, each section has 4 rows, the first row it must show image from website which I used HJCache class to cache the image and avoid leaking/memory issues.
now, this code it works well and while I am scrolling fast it doesn't leak or make memory issue
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//UITableViewCell *cell=nil;
if (indexPath.row == 0) {
static NSString *CellIdentifier = #"Cell";
HJManagedImageV* mi;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
mi = [[HJManagedImageV alloc] initWithFrame:CGRectMake(0,0,cell.frame.size.width, 310)];
mi.tag = 999;
[cell addSubview:mi];
} else {
mi = (HJManagedImageV*)[cell viewWithTag:999];
[mi clear];
}
if (indexPath.row == 0) {
mi.image = [UIImage imageNamed:#"placeholder"];
mi.url = [NSURL URLWithString:[pictures objectAtIndex:indexPath.section]];
[objMan manage:mi];
UITapGestureRecognizer *tapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(likeTappedDouble:)];
tapped.numberOfTapsRequired = 2;
[mi setUserInteractionEnabled:YES];
[mi addGestureRecognizer:tapped];
}
return cell;
}
}
But when I try to configure other rows it leaked and while scrolling the application make memory issue and be very slowly.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//UITableViewCell *cell=nil;
if (indexPath.row == 0) {
static NSString *CellIdentifier = #"Cell";
HJManagedImageV* mi;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
mi = [[HJManagedImageV alloc] initWithFrame:CGRectMake(0,0,cell.frame.size.width, 310)];
mi.tag = 999;
[cell addSubview:mi];
} else {
mi = (HJManagedImageV*)[cell viewWithTag:999];
[mi clear];
}
if (indexPath.row == 0) {
mi.image = [UIImage imageNamed:#"placeholder"];
mi.url = [NSURL URLWithString:[pictures objectAtIndex:indexPath.section]];
[objMan manage:mi];
UITapGestureRecognizer *tapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(likeTappedDouble:)];
tapped.numberOfTapsRequired = 2;
[mi setUserInteractionEnabled:YES];
[mi addGestureRecognizer:tapped];
}
return cell;
}
static NSString *CellIdentifier = #"CellS";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
if (indexPath.row == 1) {
// configure row 1
}
if (indexPath.row == 2) {
// configure row 2
}
// etc for the others ..
return cell;
}
Where is the problem, Thanks..
UPDATE
This code doesn't work well, it add the subview in other row while scrolling
static NSString *CellIdentifier = #"CellS";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
if (indexPath.row == 1) {
UIImage* likesimage = [UIImage imageNamed:#"likespic"];
CGRect frameimg = CGRectMake(7, 5, likesimage.size.width, likesimage.size.height);
likesbutton = [[UIButton alloc] initWithFrame:frameimg];
[likesbutton setBackgroundImage:likesimage forState:UIControlStateNormal];
likesbutton.backgroundColor = [UIColor clearColor];
[cell addSubview:likesbutton];
label3 = [[UILabel alloc] initWithFrame:CGRectMake(20, 2, 100, 20)];
label3.textColor = [UIColor colorWithRed:61.0/255.0 green:113.0/255.0 blue:154.0/255.0 alpha:1.0];
label3.backgroundColor = [UIColor clearColor];
label3.font = [UIFont fontWithName:#"Helvetica-Bold" size:12];
label3.adjustsFontSizeToFitWidth = YES;
[cell addSubview:label3];
}
}
For your other cells, you need to reuse the cell either:
static NSString *CellIdentifier = #"CellS";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
EDIT for Updated Question:
You need to know how "cell reusing" works. As for your updated code, it said only row 1 needs the subviews like image, right? So you need add it outside of the if (cell == nil){} snippet, like the code below:
static NSString *CellIdentifier = #"CellS";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
if (indexPath.row == 1) {
UIImage* likesimage = [UIImage imageNamed:#"likespic"];
CGRect frameimg = CGRectMake(7, 5, likesimage.size.width, likesimage.size.height);
likesbutton = [[UIButton alloc] initWithFrame:frameimg];
[likesbutton setBackgroundImage:likesimage forState:UIControlStateNormal];
likesbutton.backgroundColor = [UIColor clearColor];
[cell addSubview:likesbutton];
label3 = [[UILabel alloc] initWithFrame:CGRectMake(20, 2, 100, 20)];
label3.textColor = [UIColor colorWithRed:61.0/255.0 green:113.0/255.0 blue:154.0/255.0 alpha:1.0];
label3.backgroundColor = [UIColor clearColor];
label3.font = [UIFont fontWithName:#"Helvetica-Bold" size:12];
label3.adjustsFontSizeToFitWidth = YES;
[cell addSubview:label3];
}
Note: It's better to create a new cell instance like what you did for row 0, cause it's only need to be created once.
try this ...
man you are doing 2 mistakes here
not checking for dequeued cells.
after allocating the cell , you are not releasing it's memory.
i suggest this code for you .
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"cellID";
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier] autorelease];
}
}
don't forget to autorelease cells .
remove all add subviews, then add new subviews
NSArray *subviews = [[NSArray alloc] initWithArray:cell.contentView.subviews];
for (UIView *subview in subviews) {
[subview removeFromSuperview];
}

UISwitch status change overlap

I have a problem with a UISwitch in a tableview cell.
This switch is normally in On position, then when a variable changes its value the switch go to the Off position but I have a problem like the switch in the second row:
This is the simple code into the cellForRowAtIndexPath section:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
tableView.backgroundColor = [UIColor clearColor];
tableView.opaque = NO;
tableView.backgroundView = nil;
[tableView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"myImage.png"]]];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
if ([self.title isEqualToString:#"Test"]) {cell.textLabel.text = [self.tester objectAtIndex: [indexPath row]];cell.selectionStyle = UITableViewCellSelectionStyleNone; cell.accessoryType = UITableViewCellAccessoryNone;
if (([indexPath row] == 5) || ([indexPath row] == 4)){
NSArray *segmentItems = [NSArray arrayWithObjects:#"Down", #"Up", nil];
UISegmentedControl *testSegmentedControl = [[UISegmentedControl alloc] initWithItems:segmentItems];
testSegmentedControl.autoresizingMask = UIViewAutoresizingFlexibleWidth;
testSegmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
testSegmentedControl.tintColor = [UIColor grayColor];
testSegmentedControl.frame = CGRectMake(160, 10, 150, 30);
[testSegmentedControl setTag:[indexPath row]];
[[cell contentView] addSubview:testSegmentedControl];
[testSegmentedControl addTarget:self
action:#selector(testing:)
forControlEvents:UIControlEventValueChanged];
}
else {
mySwitch = [ [ UISwitch alloc ] initWithFrame: CGRectMake(200, 10, 0, 0) ];
mySwitch.tag = [indexPath row];
mySwitch.onTintColor = [UIColor grayColor];
if ([indexPath row] == 0){
if ([asd41 isEqualToString:#"False"]) mySwitch.on = NO;
else mySwitch.on = YES;
}
if ([indexPath row] == 1){
if ([asd40 isEqualToString:#"False"]) mySwitch.on = NO;
else mySwitch.on = YES;
}
if ([indexPath row] == 2){
if ([asd42 isEqualToString:#"False"]) mySwitch.on = NO;
else mySwitch.on = YES;
}
if ([indexPath row] == 3){
if ([asd76 isEqualToString:#"False"]) mySwitch.on = NO;
else mySwitch.on = YES;
}
[ cell addSubview: mySwitch ];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration: 0.2];
[mySwitch addTarget:self action:#selector(switch1:) forControlEvents:UIControlEventValueChanged];}
}

ios - adding text fields to uitableviewcell programmatically not working

Rookie ios question. I am trying to create a sign up form using a table view controller. I am trying add textfields to each cell programmatically in the cellForRowAtIndexPath method but all my text fields seem to getting created on the first cell - one overlapping the other. Here is my code.
Here is how my cells are rendering. I think I am doing something goofy with the part that is reusing the cells. I did check some some other similar threads on stackoverflow but none of them helped. Could you please tell me what i am missing or is this even the way this is supposed to be done. Your inputs are really appreciated.
Thanks,
Mike
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"signUpFields" forIndexPath:indexPath];
// Configure the cell...
if (indexPath.row == 0){
//self.firstName = [[UITextField alloc] initWithFrame:CGRectMake(5, 0, 280, 21)];
self.firstName = [[UITextField alloc] initWithFrame:CGRectMake(120, 13, 375, 30)];
self.firstName.placeholder = #"First Name";
self.firstName.autocorrectionType = UITextAutocorrectionTypeNo;
[self.firstName setClearButtonMode:UITextFieldViewModeWhileEditing];
//cell.accessoryView = self.firstName;
[cell.contentView addSubview:self.firstName];
}
if (indexPath.row == 1){
self.lastName = [[UITextField alloc] initWithFrame:CGRectMake(120, 13, 375, 30)];
self.lastName.placeholder = #"Last Name";
self.lastName.autocorrectionType = UITextAutocorrectionTypeNo;
[self.lastName setClearButtonMode:UITextFieldViewModeWhileEditing];
//cell.accessoryView = self.lastName;
[cell.contentView addSubview:self.lastName];
}
if (indexPath.row == 2){
self.emailId = [[UITextField alloc] initWithFrame:CGRectMake(120, 13, 375, 30)];
self.emailId.placeholder = #"Email Id";
self.emailId.autocorrectionType = UITextAutocorrectionTypeNo;
[self.emailId setClearButtonMode:UITextFieldViewModeWhileEditing];
//cell.accessoryView = self.emailId;
[cell.contentView addSubview:self.emailId];
}
if (indexPath.row == 3){
self.password = [[UITextField alloc] initWithFrame:CGRectMake(120, 13, 375, 30)];
self.password.placeholder = #"Password";
self.password.secureTextEntry = YES;
self.password.autocorrectionType = UITextAutocorrectionTypeNo;
[self.password setClearButtonMode:UITextFieldViewModeWhileEditing];
//cell.accessoryView = self.password;
[cell.contentView addSubview:self.password];
}
self.firstName.delegate = self;
self.lastName.delegate = self;
self.emailId.delegate = self;
self.password.delegate = self;
[self.signUpTable addSubview:self.firstName];
[self.signUpTable addSubview:self.lastName];
[self.signUpTable addSubview:self.emailId];
[self.signUpTable addSubview:self.password];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
Try this..
- (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];
}
if (indexPath.row == 0){
//self.firstName = [[UITextField alloc] initWithFrame:CGRectMake(5, 0, 280, 21)];
self.firstName = [[UITextField alloc] initWithFrame:CGRectMake(120, 13, 375, 30)];
self.firstName.placeholder = #"First Name";
self.firstName.autocorrectionType = UITextAutocorrectionTypeNo;
[self.firstName setClearButtonMode:UITextFieldViewModeWhileEditing];
//cell.accessoryView = self.firstName;
[cell addSubview:self.firstName];
}
if (indexPath.row == 1){
self.lastName = [[UITextField alloc] initWithFrame:CGRectMake(120, 13, 375, 30)];
self.lastName.placeholder = #"Last Name";
self.lastName.autocorrectionType = UITextAutocorrectionTypeNo;
[self.lastName setClearButtonMode:UITextFieldViewModeWhileEditing];
//cell.accessoryView = self.lastName;
[cell addSubview:self.lastName];
}
if (indexPath.row == 2){
self.emailId = [[UITextField alloc] initWithFrame:CGRectMake(120, 13, 375, 30)];
self.emailId.placeholder = #"Email Id";
self.emailId.autocorrectionType = UITextAutocorrectionTypeNo;
[self.emailId setClearButtonMode:UITextFieldViewModeWhileEditing];
//cell.accessoryView = self.emailId;
[cell addSubview:self.emailId];
}
if (indexPath.row == 3){
self.password = [[UITextField alloc] initWithFrame:CGRectMake(120, 13, 375, 30)];
self.password.placeholder = #"Password";
self.password.secureTextEntry = YES;
self.password.autocorrectionType = UITextAutocorrectionTypeNo;
[self.password setClearButtonMode:UITextFieldViewModeWhileEditing];
//cell.accessoryView = self.password;
[cell addSubview:self.password];
}
self.firstName.delegate = self;
self.lastName.delegate = self;
self.emailId.delegate = self;
self.password.delegate = self;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
no need to add again text view in table view, this is wrong. Try this, this will work for you but better you should create a custom Cell and use. That is the best way to do it, because you can use whatever you want with Cell.
Don't add them as subviews, set them as the cell's accessory view.
- (UITextField*)getTextField{
UITextField *tf = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 20, 35)];
tf.delegate = self;
tf.textColor = [UIColor colorWithRed:.231 green:.337 blue:.533 alpha:1];
tf.autocorrectionType = UITextAutocorrectionTypeNo;
tf.borderStyle = UITextBorderStyleNone;
tf.frame = CGRectMake(0, 20, 170, 30);
tf.clearButtonMode = UITextFieldViewModeWhileEditing;
tf.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
tf.font = [UIFont systemFontOfSize:13];
return tf;
}
- (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];
cell.textLabel.font = [UIFont systemFontOfSize:13];
cell.detailTextLabel.font = [UIFont systemFontOfSize:13];
cell.detailTextLabel.numberOfLines = 2;
}
if (indexPath.section == 0) {
UITextField *tf = (UITextField*)cell.accessoryView;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.textLabel.numberOfLines = 2;
tf = [self getTextField];
cell.accessoryView = cell.editingAccessoryView = tf;
[((UITextField*)cell.accessoryView) setBorderStyle:self.tableView.editing ? UITextBorderStyleRoundedRect : UITextBorderStyleNone];
[((UITextField*)cell.accessoryView) setUserInteractionEnabled:self.tableView.editing ? YES : NO];
[((UITextField*)cell.accessoryView) setTextAlignment:!self.tableView.editing ? UITextAlignmentRight : UITextAlignmentLeft];
((UITextField*)cell.accessoryView).tag = indexPath.row;
}
return cell;
}
The idea is that the cell is re-drawn each time it appears on screen, coming from off the screen and if you add the text field with addSubview:, it will add it each time as well. You COULD do it, but then you have to clear the cell's contentView of subviews, but that requires extra work, cpu and memory use, and not to say it's the least elegant solution.
Looks like you have just four cells you want to display. This is a static case, the cells don't change, you should create this tableView and its cells statically in the xib/storyboard. That is the preferred way here.
If you really want to do it programmatically, create four UITableViewCells with the behavior you want before hand. Keep them in an array. Inside cellForRowAtIndex path return cellArray[indexPath.row];
Note this is the best approach only because you have just four tableViewCells.
ReuseIdentifiers come in handy only if you have more cells than can be accommodated on the screen at once. So in your case, you never actually reuse the cells.
keep this one and try it,
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *MyIdentifier = [NSString stringWithFormat:#"%d%d",indexPath.row,indexPath.section];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil)
{
//your stuff.
}
}
Try to use this one ...And there is no need to add these textfield to tableview. So please remove some code..
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"signUpFields" forIndexPath:indexPath];
// Configure the cell...
if (indexPath.row == 0){
//self.firstName = [[UITextField alloc] initWithFrame:CGRectMake(5, 0, 280, 21)];
self.firstName = [[UITextField alloc] initWithFrame:CGRectMake(120, 13, 375, 30)];
self.firstName.placeholder = #"First Name";
self.firstName.autocorrectionType = UITextAutocorrectionTypeNo;
[self.firstName setClearButtonMode:UITextFieldViewModeWhileEditing];
//cell.accessoryView = self.firstName;
[cell addSubview:self.firstName];
}
if (indexPath.row == 1){
self.lastName = [[UITextField alloc] initWithFrame:CGRectMake(120, 13, 375, 30)];
self.lastName.placeholder = #"Last Name";
self.lastName.autocorrectionType = UITextAutocorrectionTypeNo;
[self.lastName setClearButtonMode:UITextFieldViewModeWhileEditing];
//cell.accessoryView = self.lastName;
[cell addSubview:self.lastName];
}
if (indexPath.row == 2){
self.emailId = [[UITextField alloc] initWithFrame:CGRectMake(120, 13, 375, 30)];
self.emailId.placeholder = #"Email Id";
self.emailId.autocorrectionType = UITextAutocorrectionTypeNo;
[self.emailId setClearButtonMode:UITextFieldViewModeWhileEditing];
//cell.accessoryView = self.emailId;
[cell addSubview:self.emailId];
}
if (indexPath.row == 3){
self.password = [[UITextField alloc] initWithFrame:CGRectMake(120, 13, 375, 30)];
self.password.placeholder = #"Password";
self.password.secureTextEntry = YES;
self.password.autocorrectionType = UITextAutocorrectionTypeNo;
[self.password setClearButtonMode:UITextFieldViewModeWhileEditing];
//cell.accessoryView = self.password;
[cell addSubview:self.password];
}
self.firstName.delegate = self;
self.lastName.delegate = self;
self.emailId.delegate = self;
self.password.delegate = self;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
Instead of writing more cells just create one custom cell with textfield.Give the tag for each textfield and also set the count of rows in numberOfRowsIntheSections then it will display the cells how much you need. I think it will helps you.
Before You follow my Answer i wnat to tell you that following code is bad for memory management because it will create new cell for each rows of UITableView, so be careful for it.
But it is better to use, When UITableView Have Limited rows (about 50-100 may be ) then following code is helpful in your case, use it, If it is suitable for you.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = [NSString stringWithFormat:#"S%1dR%1d",indexPath.section,indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
/// Put/Create your UITextField or any Controls here
}
return cell;
}
#Mike : First of all I will suggest you to go with StoryBoard or Nib files.
If you use this, then you can use following code
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell"];
UITextField *txtField = (UITextField *)[cell viewWithTag:1];
txtField.text = #"Latest";
return cell;
}
Based on row number you can set text.
If you want to add the UITextField at run time. then you can use following code.
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell"];
UITextField *txtField = [[UITextField alloc] initWithFrame:CGRectMake(165.0, 7.0, 150.0, 30.0)];
txtField.text = #"Latest";
[cell.contentView addSubview:txtField];
return cell;
}
You can assign different tag and based on tags you can store those values.

UITableView list not displaying all items

In my UITableView I am trying to display all the items in my plist but its not showing all the items. Actually it is showing most of it but the lower items are being repeated for some odd reason. I basically want to show all the keys in the plist with their respective values. Is the list too long to display? there's about 30 items.
First I tried to sort the keys and thought that was the problem, so then I didn't sort at all and I get the same problem, lower down the list the items get repeated and not showing the last 3 items. Is there a limit?
Below is some code, I've just modified to fit:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier1 = #"PreferencesCell1";
static NSString *CellIdentifier2 = #"PreferencesCell2";
static NSString *CellIdentifier3 = #"PreferencesCell3";
UITableViewCell *cell;
NSArray *keys = [[[preferences objectAtIndex:indexPath.section] objectForKey:#"Rows"] allKeys];
NSString *prefName = [keys objectAtIndex:indexPath.row];
if (indexPath.section == 0 || indexPath.section == 2) {
if(indexPath.section == 0)
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
else if(indexPath.section == 2)
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier2];
if (cell == nil) {
if(indexPath.section == 0)
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1] autorelease];
else if(indexPath.section == 2)
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier2] autorelease];
cell.accessoryType = UITableViewCellAccessoryNone;
CGRect labelRect = CGRectMake(10, 5, 300, 31);
UILabel *settingName = [[UILabel alloc] initWithFrame:labelRect];
settingName.font = [UIFont boldSystemFontOfSize:17.0];
settingName.backgroundColor = [UIColor clearColor];
settingName.text = prefName;
[cell.contentView addSubview: settingName];
[settingName release];
}
} else if(indexPath.section == 1) {
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier3];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier3] autorelease];
CGRect labelRect = CGRectMake(10, 5, 300, 31);
UILabel *label = [[UILabel alloc] initWithFrame:labelRect];
label.font = [UIFont boldSystemFontOfSize:17.0];
label.backgroundColor = [UIColor clearColor];
label.text = prefName;
[cell.contentView addSubview: label];
}
cell.accessoryType = UITableViewCellAccessoryNone;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
return cell;
}
What I've found is that if I don't use the labels and just go for the generic cell.textLabel.text approach then all the items are displayed correctly. However if I use the UILabel approach, the bottom items are not shown. I need to go this route as I'm adding other items in the Cell.
Working Code.
Initialization and creation of cell must be created first, then using that referenced cell remove from superview, then render the subviews. So reordering of the code from above.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier1 = #"PreferencesCell1";
static NSString *CellIdentifier2 = #"PreferencesCell2";
static NSString *CellIdentifier3 = #"PreferencesCell3";
UITableViewCell *cell;
NSArray *keys = [[[preferences objectAtIndex:indexPath.section] objectForKey:#"Rows"] allKeys];
NSString *prefName = [keys objectAtIndex:indexPath.row];
// Create/Initialize Cell first
if (indexPath.section == 0 || indexPath.section == 2) {
if(indexPath.section == 0)
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
else if(indexPath.section == 2)
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier2];
if (cell == nil) {
if(indexPath.section == 0)
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1] autorelease];
else if(indexPath.section == 2)
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier2] autorelease];
}
} else if(indexPath.section == 1) {
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier3];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier3] autorelease];
}
}
// remove from superview
[cell.contentView.subviews makeObjectsPerformSelector:#selector(removeFromSuperview)];
// render the subviews
if (indexPath.section == 0 || indexPath.section == 2) {
cell.accessoryType = UITableViewCellAccessoryNone;
CGRect labelRect = CGRectMake(10, 5, 300, 31);
UILabel *settingName = [[UILabel alloc] initWithFrame:labelRect];
settingName.font = [UIFont boldSystemFontOfSize:17.0];
settingName.backgroundColor = [UIColor clearColor];
settingName.text = prefName;
[cell.contentView addSubview: settingName];
[settingName release];
} else if(indexPath.section == 1) {
CGRect labelRect = CGRectMake(10, 5, 300, 31);
UILabel *label = [[UILabel alloc] initWithFrame:labelRect];
label.font = [UIFont boldSystemFontOfSize:17.0];
label.backgroundColor = [UIColor clearColor];
label.text = prefName;
[cell.contentView addSubview: label];
cell.accessoryType = UITableViewCellAccessoryNone;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
return cell;
}
It looks like cells are being reused and you are just adding new views to their existing contents. You need to reset the content, as described here: UITbleViewCell Class Reference. If you were just setting the cell's textLabel each time, setting a new value would suffice here, but if you are adding subviews you may need something more like [cell.contentView.subviews makeObjectsPerformSelector: #selector(removeFromSuperview)];
The Limit for a Tableview is the free RAM size.
Please post some Code. But i think that this could be a Problem with Cell caching.

Resources