Reputation: 545
I have a uitableview with 2 rows n 4 sections and i have a label and textfield in each cells. And the problems is my label comes in all section where as my textfield is not repeating in other sections. Actually i have two textfield one for each cell, one is normal textfield and another is picker textfield (when I clicked TF the picker will pop up). For one section both the TF coming but it's not repeating in other section.
My code
static NSString *CellIdentifier = @"Cell";
UITextField *textField;
NSString *string=[NSString stringWithFormat:@"ident_%d",indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:string];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.selectionStyle=UITableViewCellSelectionStyleNone;
UILabel *lbl1 = [[UILabel alloc]initWithFrame:CGRectMake(50, 10, 100, 35)];
[lbl1 setFont:[UIFont systemFontOfSize:16]];
[lbl1 setTextColor:[UIColor blackColor]];
[lbl1 setBackgroundColor:[UIColor clearColor]];
if (indexPath.row==0) {
lbl1.text = @"Quantity";
[cell.contentView addSubview:self.qntTF];
}
if (indexPath.row==1) {
lbl1.text = @"Unit";
[cell.contentView addSubview:self.unitTF];
}
// Configure the cell...;
textField =[self.tableArray objectAtIndex:indexPath.row];
[cell.contentView addSubview:textField];
cell.textLabel.text = nil;
textField.tag = TextFieldTag;
cell.detailTextLabel.text = nil;
[cell addSubview:lbl1];
[lbl1 release];
return cell;
Upvotes: 0
Views: 228
Reputation: 5178
before return the cell add [cell setNeedsDisplay]; line.
[cell setNeedsDisplay];
return cell;
it will be ok.
thanx.
Upvotes: 0
Reputation: 62676
The way the code is now, you're adding many, many more labels than you think (each time the table scrolls) and you're only adding one text field, then moving it around to different cells. It ends up on whichever cell was rendered last.
The way your cellForRow... method is now, every time row 0 and 1 for any section appears - including when the user is just scrolling them on and off the screen - another text view and label are added as subviews to the cell. Scroll up and down 100 times, there will be 100 text views and labels stacked one on top of another in the cell.
To avoid this, only add subviews when the cell is created. So in this block...
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
UILabel *label = /* all of your label init */
label.tag = kSOME_UNIQUE_INT_CONST;
UITextField *textField = /* all of your text field init */
textField.tag = kSOME_OTHER_UNIQUE_INT_CONST
[cell addSubview:label];
[cell addSubview:textField];
}
Now, after the block, you can assume that each cell you has exactly one label and one textField. The next job is to find them (they were either just created, or already in the cell if it was reused)...
UILabel *label = (UILabel *)[cell viewWithTag:kSOME_UNIQUE_INT_CONST];
UILabel *textField = (UITextField *)[cell viewWithTag:kSOME_OTHER_UNIQUE_INT_CONST];
Now you're ready to configure the cell for the given section and row. The key here is to remember that you're often getting reused cells. They will still be configured for rows that have been scrolled away. So every "if" block needs a corresponding "else" block to change the subviews' states:
if (indexPath.row == 0) {
label.text = @"Quantity";
// not sure what view you were adding here, but don't
// subview adds only in the cell==nil block above
} else {
label.text = /* whatever the label should be for row != 0 */
// this is important: if you change label state in the if, you
// must specify it's state also in an else, otherwise you'll see
// leftover state on the label when the cell is reused for a different row
}
Upvotes: 1
Reputation: 333
Why don't you initialise the text box the same way you do the label?
You have for the TextField:
UITextField *textField;
Then The Label:
UILabel *lbl1 = [[UILabel alloc]initWithFrame:CGRectMake(50, 10, 100, 35)];
[lbl1 setFont:[UIFont systemFontOfSize:16]];
[lbl1 setTextColor:[UIColor blackColor]];
[lbl1 setBackgroundColor:[UIColor clearColor]];
Why don't you try this near the label:
UITextField *textfield = [[UITextField alloc]initWithFrame:CGRectMake(LOCATION)];
[textfield setTextColor:[UIColor blackColor]];
[textfield setBackgroundColor:[UIColor clearColor]];
[textfield setBorderStyle:UITextBorderStyleNone];
Also it could be that your array you get your textfields from only has two textfields in it?
Also it could be because it looks like you are not instancing a new text box just adding the same one from the implementation over and over again, Example:
if (indexPath.row==0) {
lbl1.text = @"Quantity";
[cell.contentView addSubview:self.qntTF];
}
if (indexPath.row==1) {
lbl1.text = @"Unit";
[cell.contentView addSubview:self.unitTF];
}
Upvotes: 0