I have 2 columns in my table:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSUInteger const kLeftLabel =100;
static NSUInteger const kRightLabel=101;
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
CGRect leftF = CGRectMake(10, 5, 100, 30);
CGRect rightF = CGRectMake(120, 5, 100, 30);
left = [[[UILabel alloc] initWithFrame:leftF] autorelease];
left.tag = kLeftLabel; // assumming #define kLeftLabel 100
[cell.contentView addSubview:left];
right = [[[UILabel alloc] initWithFrame:rightF] autorelease];
right.tag = kRightLabel;
[cell.contentView addSubview:right];
}
else{
left= (UILabel*)[cell.contentView viewWithTag:kLeftLabel];
right = (UILabel*)[cell.contentView viewWithTag:kRightLabel];
}
profileDB *Obj = [appDelegate.profileArray objectAtIndex:indexPath.row];
left.text = Obj.profileName;
right.text = Obj.id;
return cell;
}
I want to read the right column's text only for a selected cell.
Is this possible?
First, You have to find out the selected cell.
- (void) tableView:(UITableView*)tableview didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
UITableViewCell *cell = [tableview cellForRowAtIndexPath:indexPath];
for(UILabel *lbl in [cell.contentView subviews])
{
if(([lbl isKindOfClass:[UILabel class]]) && ([lbl tag] == 101))
{
NSString *str = lbl.textLabel.text;
}
}
}
If you have added subviews on cell, the you can specific label's text by using their tag
Related
I am creating a TableView which contains 20 rows.I have to add single labels to the even cells and i have to add two labels at odd cells.When i adding the desired labels and scrolling my table the labels disappars as i go down help me out.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// UITableView *cell=[tableView ]
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
if(indexPath.row%2==0)
{
UILabel *cellLabel=[[UILabel alloc]initWithFrame:CGRectMake(100, 40, 40, 40)];
cellLabel.text=#"o1";
[cell addSubview:cellLabel];
[cellLabel release];
}
else
{
UILabel *cellLabel1=[[UILabel alloc]initWithFrame:CGRectMake(100, 40, 40, 40)];
cellLabel1.text=#"e1";
[cell addSubview:cellLabel1];
[cellLabel1 release];
UILabel *cellLabel2=[[UILabel alloc]initWithFrame:CGRectMake(150, 40, 40, 40)];
cellLabel2.text=#"e2";
[cell addSubview:cellLabel2];
[cellLabel2 release];
}
}
Check this. This is same as your code I have just change Y position of each label and its working fine with scrolling also.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// UITableView *cell=[tableView ]
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
if(indexPath.row%2==0)
{
UILabel *cellLabel=[[UILabel alloc]initWithFrame:CGRectMake(100, 10, 40, 40)];
cellLabel.text=#"o1";
[cell addSubview:cellLabel];
}
else
{
UILabel *cellLabel1=[[UILabel alloc]initWithFrame:CGRectMake(100, 10, 40, 40)];
cellLabel1.text=#"e1";
[cell addSubview:cellLabel1];
UILabel *cellLabel2=[[UILabel alloc]initWithFrame:CGRectMake(150, 10, 40, 40)];
cellLabel2.text=#"e2";
[cell addSubview:cellLabel2];
}
}
return cell;
}
The two kinds of cells are different, so I would make different cell identifiers for each. Something like:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *EvenCellIdentifier = #"EvenCell";
static NSString *OddCellIdentifier = #"OddCell";
NSString* cellIdentifier = (indexPath.row % 2) == 0 ? EvenCellIdentifier : OddCellIdentifier;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
if(indexPath.row%2==0)
{
UILabel *cellLabel=[[UILabel alloc]initWithFrame:CGRectMake(100, 40, 40, 40)];
cellLabel.text=#"o1";
[cell addSubview:cellLabel];
[cellLabel release];
}
else
{
UILabel *cellLabel1=[[UILabel alloc]initWithFrame:CGRectMake(100, 40, 40, 40)];
cellLabel1.text=#"e1";
[cell addSubview:cellLabel1];
[cellLabel1 release];
UILabel *cellLabel2=[[UILabel alloc]initWithFrame:CGRectMake(150, 40, 40, 40)];
cellLabel2.text=#"e2";
[cell addSubview:cellLabel2];
[cellLabel2 release];
}
}
return cell;
}
Do it like this :
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// UITableView *cell=[tableView ]
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
UILabel *cellLabel=[[UILabel alloc]initWithFrame:CGRectMake(100, 40, 40, 40)];
cellLabel.text=#"o1";
cellLabel.tag = 2;
[cell addSubview:cellLabel];
[cellLabel release];
UILabel *cellLabel1=[[UILabel alloc]initWithFrame:CGRectMake(100, 40, 40, 40)];
cellLabel1.text=#"e1";
cellLable1.tag = 3;
[cell addSubview:cellLabel1];
[cellLabel1 release];
UILabel *cellLabel2=[[UILabel alloc]initWithFrame:CGRectMake(150, 40, 40, 40)];
cellLabel2.text=#"e2";
cellLabel2.tag = 4;
[cell addSubview:cellLabel2];
[cellLabel2 release];
}
UILabel *label1 = (UILabel *) [cell viewWithTag:2];
UILabel *label2 = (UILabel *) [cell viewWithTag:3];
UILabel *label3 = (UILabel *) [cell viewWithTag:4];
if (indexPath.row%2==0)
{
label1.hidden = FALSE;
label2.hidden = TRUE;
label3.hidden = TRUE:
}
else
{
label1.hidden = TRUE;
label2.hidden = FALSE;
label3.hidden = FALSE;
}
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *MyIdentifier = #"MyIdentifier";
static NSString *MyIdentifier1 = #"MyIdentifier1";
UITableViewCell *cell;
if(indexPath.row % 2 != 0)
{
cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
}
else
{
cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier1];
}
if(cell == nil)
{
if(indexPath.row % 2 != 0)
{
cell=[self reuseTableViewCellWithIdentifier:MyIdentifier withIndexPath:indexPath];
}
else
{
cell=[self reuseTableViewCellWithIdentifier:MyIdentifier1 withIndexPath:indexPath];
}
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
-(UITableViewCell *)reuseTableViewCellWithIdentifier:(NSString *)identifier withIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell=[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier]autorelease];
if([identifier isEqualToString:#"MyIdentifier"])
{
UILabel *l1=[[[UILabel alloc]initWithFrame:CGRectMake(5, 5, 30, 20)]autorelease];
l1.backgroundColor=[UIColor redColor];
[cell.contentView addSubview:l1];
UILabel *l2=[[[UILabel alloc]initWithFrame:CGRectMake(65, 5, 30, 20)]autorelease];
l2.backgroundColor=[UIColor grayColor];
[cell.contentView addSubview:l2];
}
else
{
UILabel *l1=[[[UILabel alloc]initWithFrame:CGRectMake(5, 5, 30, 20)]autorelease];
l1.backgroundColor=[UIColor blueColor];
[cell.contentView addSubview:l1];
}
return cell;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.row%2==0)
{
static NSString *CellIdentifier = #"CustomCell";
CustomCell *cell = (CustomCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"CustomCell" owner:self options:nil];
for (id currentObject in topLevelObjects)
{
if ([currentObject isKindOfClass:[UITableViewCell class]])
{
cell = (CustomCell *) currentObject;
break;
}
}
}
cell.capitalLabel.text =[capitals objectAtIndex:indexPath.row];
cell.stateLabel.text = [states objectAtIndex:indexPath.row];
cell.t1.delegate=self;
cell.t1.tag=indexPath.row;
cell.t1.text=[arrTemp objectAtIndex:indexPath.row];
cell.s1.backgroundColor=[UIColor grayColor];
cell.s1.contentSize=CGSizeMake(1000, 40);
return cell;
}
else
{
static NSString *CellIdentifier1 = #"CustomCell1";
CustomCell1 *cell1 = (CustomCell1 *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
if (cell1 == nil)
{
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"CustomCell1" owner:self options:nil];
for (id currentObject in topLevelObjects)
{
if ([currentObject isKindOfClass:[UITableViewCell class]])
{
cell1 = (CustomCell1 *) currentObject;
break;
}
}
}
cell1.l1.text=#"alok";
return cell1;
}
}
static NSString *CellIdentifier = #"Cell";
static NSString *CellIdentifierButton = #"CellButton";
UITableViewCell *cell = nil;
if (indexPath.section <=2)
{
if(indexPath.section==2&&indexPath.row==4)
{
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifierButton];
}
else
{
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
}
}
if (cell == nil)
{
if (indexPath.section <=2)
{
if(indexPath.section==2&&indexPath.row==4)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifierButton];
UIButton *b1 = [[UIButton alloc]initWithFrame:CGRectMake(200, 10, 300, 80)];
[b1 setBackgroundColor:[UIColor redColor]];
[b1 setTag:501];
[cell.contentView b1];
UIButton * b2 = [[UIButton alloc]initWithFrame:CGRectMake(200, 120, 300, 80)];
[b2 setBackgroundColor:[UIColor redColor]];
[b2 setTag:502];
[cell.contentView b2];
}
else
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, 250, 44)];
[label setBackgroundColor:[UIColor clearColor]];
[label setTag:500];
[cell.contentView addSubview:label];
//end for section 1
//for section2
}
//end for section 2
}
}
}
//====setting the values===//
if (indexPath.section ==0)
{
UILabel *label = (UILabel *)[cell.contentView viewWithTag:500];
if (label) {
[label setText:[arr1 objectAtIndex:indexPath.row]];
}
}
if (indexPath.section ==1)
{
UILabel *label = (UILabel *)[cell.contentView viewWithTag:500];
if (label) {
[label setText:[arr2 objectAtIndex:indexPath.row]];
}
}
in my app i have an UITableView with 120 rows and every row has 1 UItextfeilds and 1 Buttons as show in the code bellow :
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier] autorelease];
}
else
{
}
UITextField *NameTextField = [[UITextField alloc] initWithFrame:CGRectMake(0, 10, 230, 28)];
NameTextField.borderStyle = UITextBorderStyleRoundedRect;
NameTextField.delegate = self;
NameTextField.textColor = UIColorFromRGB(0x2A1807);
NameTextField.font = [UIFont fontWithName:#"Helvetica" size:(17.0)];
NameTextField.font = [UIFont boldSystemFontOfSize:20];
NameTextField.tag = [indexPath row ];
NSString *temp = [self.sectionNames objectAtIndex:[indexPath section]];
NameTextField.text = [[self.myDataArray objectForKey:temp] objectAtIndex:[indexPath row]];
[cell.contentView addSubview:NameTextField];
[NameTextField release];
UIButton * aBtn = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *wijzigImage = [UIImage imageNamed:#"btn_delete.png"];
aBtn.frame = CGRectMake(240, 10, 28, 26);
[aBtn setImage:wijzigImage forState:UIControlStateNormal];
[aBtn addTarget:self action:#selector(deleteCustomCellWithUIButton:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:aBtn];
return cell;
}
i have noticed that the Scrolling is slow and It doesn't go fluently.
any idea ?
Thanks
That's because you create textfields and buttons every time, add it inside if (cell == nil) {...}. The only thing that should be left outside that if is textField text setting.
Btw, your textfield leaks.
I found the solution :
if (cell == nil) {
cell = [[[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier] autorelease];
}
else
{
UITextField *oldTextField = (UITextField *)[cell.contentView viewWithTag:999];
[oldTextField removeFromSuperview];
UIButton *oldBtn = (UIButton *)[cell.contentView viewWithTag:888];
[oldBtn removeFromSuperview];
}
NameTextField.tag = 999;
aBtn.tag = 88;
I am trying to implement UITableview based application. In my tableView their is 10 Section and each section having one row.
I want implement each section have Different type of ContentView(1-8 same ContentView 9th section Different ContentView). I did this code For that.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 10;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier1 = #"Cell1";
static NSString *CellIdentifier2 = #"Cell2";
UITextField *textField;
UITextView *textView;
NSUInteger section=[indexPath section];
if(section == 9){
UITableViewCell *cell=[self.tableView cellForRowAtIndexPath:indexPath];
//UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
if(cell==nil){
cell=[[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1]autorelease];
textView=[[UITextView alloc]initWithFrame:CGRectMake(5, 5, 290, 110)];
[textView setBackgroundColor:[UIColor scrollViewTexturedBackgroundColor
]];
[textView setTag:([indexPath section]+100)];
[cell.contentView addSubview:textView];
}else{
textView=(UITextView*)[cell.contentView viewWithTag:([indexPath section]+100)];
}
return cell;
}else {
UITableViewCell *cell=[self.tableView cellForRowAtIndexPath:indexPath];
// UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier2];
if(cell==nil){
cell=[[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier2]autorelease];
textField=[[UITextField alloc]initWithFrame:CGRectMake(5, 5, 290, 50)];
[textField setBackgroundColor:[UIColor scrollViewTexturedBackgroundColor]];
[textField setTag:([indexPath section]+100)];
[cell.contentView addSubview:textField];
}else{
textField=(UITextField*)[cell.contentView viewWithTag:([indexPath section]+100)];
}
return cell;
}
return nil;
}
My problem are:
1. After type some thing in the UITextField/UITextView i am scrolling in the UITableView. that time all data in the UITableViewCell(UITextField/UITextView) was lose, except last cell data.
2. If i create cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
Instead of
UITableViewCell *cell=[self.tableView cellForRowAtIndexPath:indexPath];
Data will repeating . How can i over come this problem?
This line:
UITableViewCell *cell=[self.tableView cellForRowAtIndexPath:indexPath];
Should never appear in your data source cellForRowAtIndexPath method.
Apart from that, your code is OK, except that you are not setting the text field value anywhere. You need a model (such as an array of strings for the 10 textfield values). This model should be updated when the textfields are edited, and in your method above you copy the value back out of the model and into the textfield's text property:
textfield.text = [self.modelArray objectAtIndex:indexPath.section];
The table pools and reuses cells in an unpredictable fashion, so that subview of a cell that just scrolled off the bottom might reappear next at the top, or might be disposed of.
This is why you saw it partially work. The cell's subviews work okay until their cell gets reused or unloaded, then things move to the wrong place or data disappears.
The solution is that your table's datasource needs to hold onto it's own data. This is usually an array representing your model. Your case is a little unusual because you are using the text controls in your table as inputs, rather than display, which is more typical.
I suggest doing it like this:
// in #interface
#property (nonatomic, retain) NSMutableArray *sections;
// in #implementation
#synthesize sections=_sections;
// at some point before the view appears
self.sections = [NSMutableArray array];
for (int i=0; i<10; i++) {
UIControl *textControl;
if (i<9) {
textControl=[[UITextView alloc]initWithFrame:CGRectMake(5, 5, 290, 110)];
} else {
textControl=[[UITextField alloc]initWithFrame:CGRectMake(5, 5, 290, 50)];
}
[textControl setBackgroundColor:[UIColor scrollViewTexturedBackgroundColor]];
[textControl setTag:i+100];
[sections addObject:textControl];
[textControl release];
}
Now your cellForRowAtIndexPath is a little simpler:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier1 = #"Cell1";
static NSString *CellIdentifier2 = #"Cell2";
NSUInteger section=[indexPath section];
if(section == 9) {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
if(cell==nil) {
cell=[[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1]autorelease];
}
} else {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier2];
if(cell==nil) {
cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier2]autorelease];
}
}
// who knows what subview this cell has? it might not have one, or it might have the wrong one
// just clean it up to be certain
for (UIView *view in cell.subviews) {
[view removeFromSuperView];
}
// get the textControl we set up for _this_ section/cell
UIControl *textControl = [self.sections objectAtIndex:section];
// now we have a fresh cell and the right textControl. drop it in
[cell addSubview:textControl];
return cell;
}
hey the reason is you are doing this things when cell is nil ? but you are not writing any code when cell is not nil.
look at this example , in this example i am adding image view in tableview cell , hence you can add textviews or any other views like this
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UIImageView *imgView;
if(cell == nil)
{
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
imgView = [[UIImageView alloc] initWithFrame:CGRectMake(100,0,100,62)];
[imgView setImage:[UIImage imageNamed:#"img.png"]];
imgView.tag = 55;
[cell.contentView addSubview:imgView];
[imgView release];
}
else
{
imgView = (id)[cell.contentView viewWithTag:55];
}
so as showin here imgView = (id)[cell.contentView viewWithTag:55]; you have to give tag to you and write code showing above in else..
Let you try to make the cell labels and textviews by using following code. It works for me.
if (tagvalue ==3) {
static NSString *CellIdentifier = #"Cell3";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:CellIdentifier] autorelease];
}
lbl7 = [[UILabel alloc] init];
[lbl7 setFont:[UIFont boldSystemFontOfSize:20]];
[cell.contentView addSubview:lbl7];
lbl7.backgroundColor = [UIColor clearColor];
lbl7.frame = CGRectMake(120, 5, 0, 40);
lbl7.tag = 70;
[lbl7 release];
lbl8 = [[UILabel alloc] init];
[lbl8 setFont:[UIFont boldSystemFontOfSize:18]];
[cell.contentView addSubview:lbl8];
lbl8.backgroundColor = [UIColor clearColor];
lbl8.textColor = [UIColor grayColor];
lbl8.frame = CGRectMake(120, 50, 0, 40);
lbl8.tag = 80;
[lbl8 release];
lbl7 = (UILabel*)[cell.contentView viewWithTag:70];
lbl8 = (UILabel*)[cell.contentView viewWithTag:80];
lbl7.text = [[rowsarray objectAtIndex:row]objectForKey:#"name"];
lbl8.text = [[rowsarray objectAtIndex:row]objectForKey:#"flavour"];
[lbl7 sizeToFit];
[lbl8 sizeToFit];
return cell;
}
if (tagvalue ==4) {
static NSString *CellIdentifier = #"Cell4";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
lbl9 = [[UILabel alloc] init];
[lbl9 setFont:[UIFont boldSystemFontOfSize:20]];
[cell.contentView addSubview:lbl9];
lbl9.backgroundColor = [UIColor clearColor];
lbl9.frame = CGRectMake(120, 5, 0, 40);
lbl9.tag = 90;
[lbl9 release];
lbl10 = [[UILabel alloc] init];
[lbl10 setFont:[UIFont boldSystemFontOfSize:18]];
[cell.contentView addSubview:lbl10];
lbl10.backgroundColor = [UIColor clearColor];
lbl10.textColor = [UIColor grayColor];
lbl10.frame = CGRectMake(120, 50, 0, 40);
lbl10.tag = 100;
[lbl10 release];
lbl9 = (UILabel*)[cell.contentView viewWithTag:90];
lbl10 = (UILabel*)[cell.contentView viewWithTag:100];
lbl9.text = [[rowsarray objectAtIndex:row]objectForKey:#"name"];
lbl10.text = [[rowsarray objectAtIndex:row]objectForKey:#"flavour"];
[lbl9 sizeToFit];
[lbl10 sizeToFit];
return cell;
}
I had same issue. Its not the problem with table class. The issue is at the place where you are calling this tableviewcontroller. First make the object of this call in .h and then allocate in .m, thats it..
When I was declaring in viewdidload like tbl *t = [self.storyboard...];, I was also facing the same problem. But when I put tbl *t; in .h problem solved.
i am experiencing some weird problems with my UITableView.
I have a Grouped TableView with 1 row per section. There is a Textfield in each row.
When i am not forced to scroll, everything is displayed correct.
But if i have to scroll, the previously hidden cells are messed up. they do contain a textfield, but there are many text labels laying over each other.
Cell http://dcsl.info/b/Untitled.png
Any advice?
- (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...
if(indexPath.section < [sectionArray count]) {
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(20, 10,580, 31)];
textField.tag = indexPath.section + 22;
textField.autocorrectionType = UITextAutocorrectionTypeNo;
textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
textField.delegate = self;
textField.text = [item.rechnerValues objectAtIndex:indexPath.section+1];
[cell.contentView addSubview:textField];
}
return cell;
}
This is because you are reusing cells. So previously created textFields are not removed. When you are scrolling you are adding new and new one on another.
Solutions:
Remove previously created textFields
(better) Just set appropriate text to already created text field
- (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];
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(20, 10,580, 31)];
textField.tag = indexPath.section + 22;
textField.autocorrectionType = UITextAutocorrectionTypeNo;
textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
textField.delegate = self;
textField.text = [item.rechnerValues objectAtIndex:indexPath.section+1];
[cell.contentView addSubview:textField];
[textField release];
}
else{
// Configure the cell...
if(indexPath.section < [sectionArray count]) {
UITextField *textFld=(UITextField*)[cell.contentView viewWithTag:indexPath.section + 22];
if(textFld){
textFld.text=[item.rechnerValues objectAtIndex:indexPath.section+1];
}
}
}
return cell;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITextField *textField;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
if(indexPath.section < [sectionArray count]) {
textField = [[UITextField alloc] initWithFrame:CGRectMake(20, 10,580, 31)];
textField.tag = indexPath.section + 22;
textField.autocorrectionType = UITextAutocorrectionTypeNo;
textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
textField.delegate = self;
[cell.contentView addSubview:textField];
[textField release];
}
}
else {
textField = (id)[cell.contentView viewWithTag:indexPath.section+22];
}
textField.text = [item.rechnerValues objectAtIndex:indexPath.section+1];
// Configure the cell...
return cell;
}
- (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...
if(indexPath.row < 8)
{
CGRect textRect = CGRectMake(10, 10, 300, 31);
UITextField *myfield = [[UITextField alloc]initWithFrame:textRect];
myfield.borderStyle = UITextBorderStyleRoundedRect;
myfield.font = [UIFont systemFontOfSize:22.0];
myfield.adjustsFontSizeToFitWidth = YES;
myfield.minimumFontSize = 2.0;
myfield.clearButtonMode = UITextFieldViewModeWhileEditing;
myfield.keyboardType = UIKeyboardTypeDefault;
myfield.autocorrectionType= UITextAutocorrectionTypeNo;
myfield.autocapitalizationType=UITextAutocapitalizationTypeNone;
myfield.returnKeyType=UIReturnKeyDone;
[self. addSubview:myfield];
}
return cell;
}
I wrote the above code for displaying textboxes in UITableViewCell (up to eight cells ) but the text box is displaying in the first cell only is there anything wrong in the above code?
The problem is this line:
[self.addSubview:myfield];
Change this to:
[cell.contentView addSubview: myfield];
[myfield release];
However, I agree with Björn Marschollek about design, so here is how this method should look:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifierWithTextBox = #"CellWithTextBox";
static NSString *CellIdentifierOther = #"CellOther";
UITableViewCell *cell;
if(indexPath.row < 8)
{
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifierWithTextBox];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifierWithTextBox] autorelease];
CGRect textRect = CGRectMake(10, 10, 300, 31);
UITextField *myfield = [[UITextField alloc]initWithFrame:textRect];
myfield.borderStyle = UITextBorderStyleRoundedRect;
myfield.font = [UIFont systemFontOfSize:22.0];
myfield.adjustsFontSizeToFitWidth = YES;
myfield.minimumFontSize = 2.0;
myfield.clearButtonMode = UITextFieldViewModeWhileEditing;
myfield.keyboardType = UIKeyboardTypeDefault;
myfield.autocorrectionType= UITextAutocorrectionTypeNo;
myfield.autocapitalizationType=UITextAutocapitalizationTypeNone;
myfield.returnKeyType=UIReturnKeyDone;
[cell.contentView addSubview: myfield];
[myfield release];
}
}
else
{
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifierOther];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifierOther] autorelease];
}
}
return cell;
}