UITableview sections overwrite in iPhone? - iphone

I have one problem with two sections in UITableview grouped style. First section having 6 rows and second section having 3 rows. When i scroll the tableview up and down the last row content of section 2 is adding in first section first row and the first row content in first section is adding in last row of second section. I check my level best whether the cell become empty to load the content but, it is not happening while debugging. My code is ,
- (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];
cell.backgroundColor = [UIColor whiteColor];
if (indexPath.section == 0 && indexPath.row == 0)
{
UILabel *Nameabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, 140, 30)];
Nameabel.text = #"Name";
Nameabel.font = [UIFont fontWithName:#"Helvetica" size:15];
Nameabel.backgroundColor = [UIColor clearColor];
Nameabel.font = [UIFont boldSystemFontOfSize:15];
[cell.contentView addSubview:Nameabel];
textField = [[UITextField alloc] initWithFrame:CGRectMake(145, 5, 150, 30)];
textField.placeholder = #"Student Name";
textField.borderStyle = UITextBorderStyleNone;
textField.font = [UIFont fontWithName:#"Helvetica" size:14];
[cell.contentView addSubview:paymentCatTextField];
}
else if (indexPath.section == 1 && indexPath.row == 0)
{
UILabel *RLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, 140, 30)];
RLabel.text = #"Class";
RLabel.backgroundColor = [UIColor clearColor];
RLabel.font = [UIFont fontWithName:#"Helvetica" size:15];
RLabel.font = [UIFont boldSystemFontOfSize:15];
[cell.contentView RLabel];
RTextField = [[UITextField alloc] initWithFrame:CGRectMake(145, 5, 150, 30)];
RTextField.placeholder = #"class Name";
RTextField.borderStyle = UITextBorderStyleNone;
RTextField.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
RTextField.font = [UIFont fontWithName:#"Helvetica" size:14];
[cell.contentView addSubview:RTextField];
}
}
return cell;
}
This is for sample. By this way the section 0 have 6 rows and section 1 have 3 rows. Where i am doing wrong. Please help my to solve this problem.
Thanks in advance.

Change your code to the following and try again:
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.backgroundColor = [UIColor whiteColor];
}
if (indexPath.section == 0 && indexPath.row == 0) {
//bla bla
} else if (indexPath.section == 1 && indexPath.row == 0) {
//bla bla
}
return cell;
}
The reason is that you are setting the cell content only when you are not reusing the cell. That's wrong in most cases.

In addition to dasdom's answer I would also suggest removing the subviews before adding the labels.
if ([[cell.contentView subviews] count] > 0) {
UIView *labelToClear = [[cell.contentView subviews] objectAtIndex:0];
[labelToClear removeFromSuperview];
UIView *textToClear = [[cell.contentView subviews] objectAtIndex:1];
[textToClear removeFromSuperview];
}
and autoreleasing the subviews:
UILabel *Nameabel = [[[UILabel alloc] initWithFrame:CGRectMake(5, 5, 140, 30)] autorelease];
UITextField *textField = [[[UITextField alloc] initWithFrame:CGRectMake(145, 5, 150, 30)] autorelease];

Related

Iphone : UITableView Header row repeating after 6 rows

I am developing a Universal app. In this app there is a UITableView. Problem is that the header row in TableView is repeating after 6 rows in iphone and 9 rows in iPad.
I googled but not find solution to solve this header issue. please help.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return [tripList count];
}
UiTableview cellForRowAtIndexPath code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UILabel *lblTripNumber = nil;
UILabel *lblTripState = nil;
UIImageView *imgTripState = nil;
UILabel *lblStateTitleText = nil;
UILabel *lblTripNUmberTitleValue = nil;
static NSUInteger const kTripNumberLabelTag = 2;
static NSUInteger const kTripStateLabelTag = 3;
static NSUInteger const kTripImageTag = 4;
BOOL isiPhone = UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(indexPath.row == 0)
{
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
if (!isiPhone) // 1=Ipad
{
lblStateTitleText = [[[UILabel alloc] initWithFrame:CGRectMake(205, 27, 250, (cell.contentView.frame.size.height))] autorelease];
lblStateTitleText.font = [UIFont boldSystemFontOfSize:32];
}
else
{
lblStateTitleText = [[[UILabel alloc] initWithFrame:CGRectMake(65, 10, 130, (cell.contentView.frame.size.height))] autorelease];
lblStateTitleText.font = [UIFont boldSystemFontOfSize:16];
}
lblStateTitleText.textAlignment = UITextAlignmentLeft;
lblStateTitleText.backgroundColor = [UIColor clearColor];
lblStateTitleText.tag = 11;
lblStateTitleText.text = #"Trip State";
lblStateTitleText.textColor = [UIColor cyanColor];
[cell.contentView addSubview:lblStateTitleText];
if (!isiPhone) // 1=Ipad
{
lblTripNUmberTitleValue = [[[UILabel alloc] initWithFrame:CGRectMake(445, 27, 290, (cell.contentView.frame.size.height))] autorelease];
lblTripNUmberTitleValue.font = [UIFont boldSystemFontOfSize:32];
}
else
{
lblTripNUmberTitleValue = [[[UILabel alloc] initWithFrame:CGRectMake(180, 10, 250, (cell.contentView.frame.size.height))] autorelease];
lblTripNUmberTitleValue.font = [UIFont boldSystemFontOfSize:16];
}
lblTripNUmberTitleValue.textAlignment = UITextAlignmentLeft;
lblTripNUmberTitleValue.backgroundColor = [UIColor clearColor];
lblTripNUmberTitleValue.tag = 12;
lblTripNUmberTitleValue.text = #"Confirmation#";
lblTripNUmberTitleValue.textColor = [UIColor greenColor];
[cell.contentView addSubview:lblTripNUmberTitleValue];
}
}
else
{
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
CGFloat xIndex = 6.0;
CGFloat xIpadIndex = 6.0;
CGFloat TripNumberLabelWidth = 130.0;
if (!isiPhone) // 1=Ipad
{
imgTripState = [[[UIImageView alloc] initWithFrame:CGRectMake(xIndex, 18, 50, 64)] autorelease];
}
else
{
imgTripState = [[[UIImageView alloc] initWithFrame:CGRectMake(xIndex, 8, 32, 32)] autorelease];
}
imgTripState.tag = kTripImageTag;
[cell.contentView addSubview:imgTripState];
xIndex +=73;
xIpadIndex +=85;
if (!isiPhone) // 1=Ipad
{
lblTripState = [[[UILabel alloc] initWithFrame:CGRectMake(xIpadIndex, 25, TripNumberLabelWidth, (cell.contentView.frame.size.height))] autorelease];
lblTripState.font = [UIFont boldSystemFontOfSize:38];
}
else
{
lblTripState = [[[UILabel alloc] initWithFrame:CGRectMake(xIndex, 1, TripNumberLabelWidth, (cell.contentView.frame.size.height))] autorelease];
lblTripState.font = [UIFont boldSystemFontOfSize:20];
}
lblTripState.textAlignment = UITextAlignmentLeft;
lblTripState.backgroundColor = [UIColor clearColor];
lblTripState.tag = kTripStateLabelTag;
lblTripState.textColor = [UIColor colorWithRed:(0.0/255) green:(241.0/255) blue:(216.0/255) alpha:1.0f];
[cell.contentView addSubview:lblTripState];
xIndex +=132;
xIpadIndex +=120;
if (!isiPhone) // 1=Ipad
{
lblTripNumber = [[[UILabel alloc] initWithFrame:CGRectMake(xIpadIndex, 25, TripNumberLabelWidth, (cell.contentView.frame.size.height))] autorelease];
lblTripNumber.font = [UIFont boldSystemFontOfSize:35];
}
else
{
lblTripNumber = [[[UILabel alloc] initWithFrame:CGRectMake(xIndex, 0, TripNumberLabelWidth, (cell.contentView.frame.size.height))] autorelease];
lblTripNumber.font = [UIFont boldSystemFontOfSize:18];
}
lblTripNumber.textAlignment = UITextAlignmentLeft;
lblTripNumber.backgroundColor = [UIColor clearColor];
lblTripNumber.tag = kTripNumberLabelTag;
lblTripNumber.textColor = [UIColor greenColor];
[cell.contentView addSubview:lblTripNumber];
}else
{
// A reusable cell was available, so we just need to get a reference to the subviews using their tags.
lblTripNumber = (UILabel *)[cell.contentView viewWithTag:kTripNumberLabelTag];
lblTripState = (UILabel *)[cell.contentView viewWithTag:kTripStateLabelTag];
// imgTripState = (UILabel *)[cell.contentView viewWithTag:kTripImageTag];
}
lblTripNumber.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
lblTripState.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
int indCount = indexPath.row -1;
TripDetails *trip = [tripList objectAtIndex:indCount];
lblTripNumber.text = trip.tripNumber;
lblTripState.text = trip.tripState;
// Configure the cell...
return cell;
}
You probably have only 6 items in your TableData.
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [TableData count];
}
In your case 'TableData' is 'tripList'.
I got that , I removed
if (cell == nil)
and it worked fine. Actually it was looking for its tags in first header row and could not replace its values, so was creating problem.
check your viewFor header in section. if you are using array for your header views, then this array object is not properly allocated memory. it uses last object for all it's occurance and replace the old. Make a memory allocated object of array before you add objects to it. tell me if this is the solution
Your code tries to reuse the cells, but the way you coded that is wrong. See my answer here for the appropriate pattern. In a nut shell, you should only define shared cell attributes in if (cell == nil) section, the row specific attributes, like label text etc. should be defined outside of this section. This is not the case for your row at index 0.

Enhance UITableView Scrolling

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;

uitableview - data and if (!cell)

I have a big problem with an UITableView, I want to use a label inside the cell, so I use this method for do it
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"ItemCell";
// If the indexPath is less than the numberOfItemsToDisplay, configure and return a normal cell,
// otherwise, replace it with a button cell.
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
else {
}
if (indexPath.section == 0) {
elemento = [array objectAtIndex:indexPath.row];
UILabel *labelTitle = [[UILabel alloc] initWithFrame:CGRectMake(100, 0, 220, 30)];
labelTitle.text = [elemento objectForKey:#"Titolo"];
labelTitle.backgroundColor = [UIColor clearColor];
labelTitle.textColor = [UIColor whiteColor];
[cell addSubview:labelTitle];
} else {
UILabel *labelTitle = [[UILabel alloc] initWithFrame:CGRectMake(100, 0, 220, 30)];
labelTitle.text = #"Read More";
labelTitle.backgroundColor = [UIColor clearColor];
labelTitle.textColor = [UIColor whiteColor];
[cell addSubview:labelTitle];
}
return cell;
}
in this way I can see all the data on my table, but the label are overlap, than I try to use this method
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"ItemCell";
// If the indexPath is less than the numberOfItemsToDisplay, configure and return a normal cell,
// otherwise, replace it with a button cell.
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
if (indexPath.section == 0) {
elemento = [array objectAtIndex:indexPath.row];
UILabel *labelTitle = [[UILabel alloc] initWithFrame:CGRectMake(100, 0, 220, 30)];
labelTitle.text = [elemento objectForKey:#"Titolo"];
labelTitle.backgroundColor = [UIColor clearColor];
labelTitle.textColor = [UIColor whiteColor];
[cell addSubview:labelTitle];
} else {
UILabel *labelTitle = [[UILabel alloc] initWithFrame:CGRectMake(100, 0, 220, 30)];
labelTitle.text = #"Read More";
labelTitle.backgroundColor = [UIColor clearColor];
labelTitle.textColor = [UIColor whiteColor];
[cell addSubview:labelTitle];
}
}
else {
}
return cell;
}
the label are OK but in this case I can see on my table only 5 data, and this 5 data are repeat for some time...
For example if in the first case on my table I can see: 1,2,3,4,5,6,7,8,9,10,...
in the second case I seee: 1,2,3,4,5,1,2,3,4,5,1,2,3,4,5,...
where is the problem?
add this code
for (UIView *view in [cell.contentView subviews])
{
[view removeFromSuperview];
}
before
if (indexPath.section == 0) {
elemento = [array objectAtIndex:indexPath.row];
UILabel *labelTitle = [[UILabel alloc] initWithFrame:CGRectMake(100, 0, 220, 30)];
labelTitle.text = [elemento objectForKey:#"Titolo"];
labelTitle.backgroundColor = [UIColor clearColor];
labelTitle.textColor = [UIColor whiteColor];
[cell addSubview:labelTitle];
Currently you add a label to the cell and the next time the cell is reused..the label is still there and you add a label on top of it.
The code you posted is specifying UITableViewCellStyleSubtitle as the cell style, which means each cell will a text label and detail text label available in its corresponding properties textLabel, and detailTextLabel. So there's no reason for you to allocate additional instances of UILabel. Instead, just populate the text properties of the existing labels. For example, you could rewrite your implementation like this:
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellID = #"ItemCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellID];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellID];
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.textLabel.textColor = [UIColor whiteColor];
}
cell.textLabel.text = (indexPath.section == 0 ?
[array objectAtIndex:indexPath.row] :
#"ReadMore");
return cell;
}

'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'

my code is
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
/// labels - names of Cities ///
UILabel *lblCity = [[UILabel alloc]initWithFrame:CGRectMake(15, 00, 200, 22)];
lblCity.font = [UIFont systemFontOfSize:14];
lblCity.backgroundColor = [UIColor clearColor];
//lblCity.backgroundColor = [UIColor redColor];
UILabel *lblDate = [[UILabel alloc]initWithFrame:CGRectMake(200, 00, 200, 22)];
lblDate.font = [UIFont systemFontOfSize:14];
lblDate.backgroundColor = [UIColor clearColor];
//lblDate.backgroundColor = [UIColor redColor];
UILabel *lblSchool = [[UILabel alloc]initWithFrame:CGRectMake(350, 00, 400, 22)];
lblSchool.font = [UIFont systemFontOfSize:14];
lblSchool.backgroundColor = [UIColor clearColor];
//lblSchool.backgroundColor = [UIColor redColor];
/// Labels for description of city events ///
UILabel *lblEvent = [[UILabel alloc]initWithFrame:CGRectMake(15, 00, 200, 30)];
lblEvent.font = [UIFont systemFontOfSize:12];
lblEvent.backgroundColor = [UIColor clearColor];
UILabel *lblEventAtDate = [[UILabel alloc]initWithFrame:CGRectMake(200, 00, 200, 30)];
lblEventAtDate.font = [UIFont systemFontOfSize:12];
lblEventAtDate.backgroundColor = [UIColor clearColor];
UILabel *lblEventAtSchool = [[UILabel alloc]initWithFrame:CGRectMake(350, 00, 400, 30)];
lblEventAtSchool.font = [UIFont systemFontOfSize:12];
lblEventAtSchool.backgroundColor = [UIColor clearColor];
if(RequestType == 2)
{
UIImageView *imgEventLabel = [[UIImageView alloc]initWithFrame:CGRectMake(00, 00, 480, 22)];
UIView *viewDescription = [[UIView alloc]initWithFrame:CGRectMake(00, 00, 480, 35)];
if(indexPath.row == 0)
{
static NSString *CellIdentifier = #"Cell11";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
lblCity.text = #"City" ;
// [cell addSubview:lblCity];
lblDate.text = #"Date" ;
// [cell addSubview:lblDate];
lblSchool.text = #"School" ;
// [cell addSubview:lblSchool];
imgEventLabel.image = [UIImage imageNamed:#"city_date_place.png"];
// [cell addSubview:imgEventLabel];
[imgEventLabel addSubview:lblCity];
[imgEventLabel addSubview:lblDate];
[imgEventLabel addSubview:lblSchool];
[cell.contentView addSubview:imgEventLabel];
}
return cell;
}
if(indexPath.row == 1)
{
static NSString *CellIdentifier = #"Cell12";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.selectionStyle=UITableViewCellSelectionStyleNone;
cell.backgroundColor=[UIColor clearColor];
cell.textLabel.numberOfLines = 999;
lblEvent.text = #"Event in City";
lblEventAtDate.text = #"Event on Date";
lblEventAtSchool.text = #"Event at School";
[viewDescription addSubview:lblEvent];
[viewDescription addSubview:lblEventAtDate];
[viewDescription addSubview:lblEventAtSchool];
[cell.contentView addSubview:viewDescription];
}
return cell;
}
}
// Configure the cell...
return cell;
}
I don't know where is fault, Please Help.
After your first call of dequeueReusableCellWithIdentifier: You didn't check for nil and create a new one if there are no available cells in the reuse queue.
In other words, if there are no cells in the reuse queue, your request type is not equal to 2, and row is not equal to 0 or 1, your cell will not be created. This causes an exception you see in the console.
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
you don't need to write that on start as you create the cell according to conditions .
At the end just return nil. Because I think there must be some condition true when you execute your code
Sorry I do not have enough points to comment on your question. Have you tried using Custom Cells? Custom Cell is like a modification of the current TableViewCells. You can do almost anything you want to that the TableViewCells do not provide. From what I see, you want 3 Labels and 1 Image. A normal TableViewCell will not give you that ability to have 3 Labels. Unless you create it yourself. Below is a link for you to do so. I hope this is what you have in mind.
iPhone Custom Cell Tutorial

problem with table view grouped table view

tableview problem: i am using 3 uilable for displaying productname, some description and image. all data displayed but when scrolling the table the labels are filled with another text with the actual text.. how can we handle this?
Code:
- (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];
}
UIImageView *imageview = [[UIImageView alloc]initWithFrame:CGRectMake(2, 2, 41, 41)];
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
NSDictionary *aDict1 = [[NSDictionary alloc]init];
aDict1 = [tableData objectAtIndex:indexPath.row];
NSString *prdStus = [aDict1 objectForKey:#"ProductStatus"];
NSLog(#"product status is %# ",prdStus);
if ([prdStus isEqualToString:#"Orange"]) {
[imageview setImage:[UIImage imageNamed:#"Yellow.png"]];
}
if ([prdStus isEqualToString:#"Green"]) {
[imageview setImage:[UIImage imageNamed:#"Green.png"]];
}
if ([prdStus isEqualToString:#"Red"]) {
[imageview setImage:[UIImage imageNamed:#"Red.png"]];
}
UILabel *label2 = [[UILabel alloc]initWithFrame:CGRectMake(46, 0, 220, 12) ];
label2.font = [UIFont systemFontOfSize:12];
label2.text = #"";
if (indexPath.row <[tableData count]) {
label2.text = [aDict1 objectForKey:#"ProductName"];
}
[cell addSubview:label2];
[cell addSubview:imageview];
label2.backgroundColor =[UIColor clearColor];
UILabel *label3 = [[UILabel alloc]initWithFrame:CGRectMake(46, 13, 220, 30) ];
label3.font = [UIFont systemFontOfSize:10];
label3.text = #"";
label3.text = [aDict1 objectForKey:#"ProductDescription"];
[cell addSubview:label3];
return cell;
}
please tell me how to avoid this..
Grouped Table view.
Here is also i am facing same problem.
I am using 4 section
1st and 3rd sections 1 row each,
2nd sec 3 rows,
4th sec 5
when configure the text the first section label displayed on 4th and 3rd section data also displayed on 4th section.
Code
- (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];
}
cell.userInteractionEnabled =NO;
if (indexPath.section == 0)
{
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(12, 2, 294, 40) ];
label.backgroundColor = [UIColor clearColor];
NSDictionary *aDict1 = [[NSDictionary alloc]init];
aDict1 = [detailsArray objectAtIndex:0];
label.text=#"";
label.text =[aDict1 objectForKey:#"ProductName"];
label.numberOfLines = 2;
label.lineBreakMode = UILineBreakModeWordWrap;
[cell addSubview:label];
[label release];
// [cell addSubview:imgview1];
// [imgview1 release];
}
if (indexPath.section ==1 ) {
if (indexPath.row == 0)
{
imgview = [[UIImageView alloc]initWithFrame:CGRectMake(255,2 , 46, 46)];
[cell addSubview:imgview];
[imgview release];
cell.textLabel.text = #"Actual Halal Rating";
NSDictionary *aDict1 = [[NSDictionary alloc]init];
aDict1 = [detailsArray objectAtIndex:0];
NSString *statStr=[[NSString alloc] init];
statStr = [aDict1 objectForKey:#"ProductHalalStatus"];
NSLog(#"status is %#",statStr);
imgview1 = [[UIImageView alloc]initWithFrame:CGRectMake(255,2 , 20, 20)];
if ([statStr isEqualToString:#"Red"]) {
[imgview1 setImage:[UIImage imageNamed:#"Red.png"]];
}
if ([statStr isEqualToString:#"Orange"] ) {
[imgview1 setImage:[UIImage imageNamed:#"Yellow.png"]];
}
if ([statStr isEqualToString:#"Green"]) {
[imgview1 setImage:[UIImage imageNamed:#"Green.png"]];
}
[cell addSubview:imgview1];
[imgview1 release];
}
if (indexPath.row == 1) {
cell.textLabel.text = #"Halal (Permisible)";
[imgview setImage:[UIImage imageNamed:#"Green.png"]];
}
if (indexPath.row == 2) {
cell.textLabel.text = #"Masbooh (Doubtful)";
[imgview setImage:[UIImage imageNamed:#"Yellow.png"]];
}
if (indexPath.row == 3) {
cell.textLabel.text = #"Haram (Not Permisible)";
[imgview setImage:[UIImage imageNamed:#"Red.png"]];
}
}
if (indexPath.section == 2) {
NSDictionary *aDict2 = [[NSDictionary alloc]init];
aDict2 = [detailsArray objectAtIndex:0];
// NSArray *ingrArr =[aDict2 objectForKey:#"IngredientInfo1"];
textview1 =[[UITextView alloc]initWithFrame:CGRectMake(12, 2, 294, 96)];
//textview1.text = ingrStr;
textview1.editable =NO;
[textview1 setFont:[UIFont systemFontOfSize:15]];
[cell addSubview:textview1];
[textview1 release];
}
if (indexPath.section == 3) {
}
return cell;
}
Remove the line, from you code ....
if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
and add the following line
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]