user2515030
user2515030

Reputation:

custom label value is disappearing when scrolling table view

I am new to iphone. my cutomcell label value is disappearing when I scroll the table view.

Again it appears when I click on that cell.

Can anyone help me?

Thanks in Advance.

//table view in view controller created in xib


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

static NSString *CellIdentifier = @"ListOfProductsCell";

ListOfProductsCell *cell = (ListOfProductsCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell==nil) {
        NSArray *nib =[[NSBundle mainBundle] loadNibNamed:@"ListOfProductsCell" owner:self options:nil];
    cell = [nib objectAtIndex:0];

    productItemDit=[productListArry objectAtIndex:indexPath.row];
    NSString *offerStr= [NSString stringWithFormat:@"%.2f",[[productItemDit objectForKey:@"offer"] floatValue]];
    NSString *fullCostStr=[[currencyCodeStr stringByAppendingString:@" "] stringByAppendingString:offerStr];
    NSLog(@"%@",fullCostStr);
    cell.itemCostLbl.text=fullCostStr;

} else {
    cell.itemStepper = (UIStepper *) [cell viewWithTag:2];
    cell.itemAddedLbl =(UILabel*)[cell viewWithTag:1];

}

if (tableView == self.searchDisplayProduct.searchResultsTableView) {
    searchProductItemDit=[searchProductListArry objectAtIndex:indexPath.row];
    NSLog(@"searchdit:%@",searchProductItemDit);
    cell.itemNameLbl.text= [searchProductItemDit objectForKey:@"name"];
    self.searchDisplayProduct.searchResultsTableView.separatorColor=[UIColor colorWithRed:200.0 green:0.0 blue:0.0 alpha:1.0];
} else {
    productItemDit=[productListArry objectAtIndex:indexPath.row];
    NSLog(@"dit:%@",productItemDit);
    cell.itemNameLbl.text=[productItemDit objectForKey:@"name"];
}

cell.itemAddedLbl.text = [[NSString alloc] initWithFormat:@"%d",itemCount];

cell.itemImg.image = [UIImage imageNamed:@"profp.jpg"];

return cell;

}

Upvotes: 2

Views: 824

Answers (2)

user2515030
user2515030

Reputation:

Solved by doing like this

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

          static NSString *CellIdentifier = @"ListOfProductsCell";

          ListOfProductsCell *cell = (ListOfProductsCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

         if (cell==nil) {
                   NSArray *nib =[[NSBundle mainBundle] loadNibNamed:@"ListOfProductsCell" owner:self options:nil];
                   cell = [nib objectAtIndex:0];

         } else {
                cell.itemStepper = (UIStepper *) [cell viewWithTag:2];
                cell.itemAddedLbl =(UILabel*)[cell viewWithTag:1];

         }

        if (tableView == self.searchDisplayProduct.searchResultsTableView) {
                  cell.itemNameLbl.text= [[searchProductListArry objectAtIndex:indexPath.row] objectForKey:@"name"];
        } else {
               cell.itemNameLbl.text=[[productListArry objectAtIndex:indexPath.row] objectForKey:@"name"];
        } 

        cell.itemCostLbl.text=[NSString stringWithFormat:@"%@  %.2f", currencyCodeStr , [[[productListArry objectAtIndex:indexPath.row] objectForKey:@"offer"] floatValue]];

        cell.itemAddedLbl.text = [[NSString alloc] initWithFormat:@"%d",itemCount];

        cell.itemImg.image = [UIImage imageNamed:@"profp.jpg"];

        return cell;

 }

Upvotes: 1

V-Xtreme
V-Xtreme

Reputation: 7333

As you haven't post much of the information this is my guess.when you scroll table view it internally calls the reloadData method . You need to use reuse Identifier to preserve the state of the cell. You can initialize the cell in cellForRowAtIndexPath like :

static NSString *cellIdentifier = @"cellID";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil)
{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
 // or cell initializing logic here for the custom cell
}

in case of custom cell add reuse identifier in the xib and use it in your cellForRowAtIndexPath method

Upvotes: 0

Related Questions