rafana rafana - 4 months ago 9
Objective-C Question

Why UITableView Delegate methods are called only single time?

I am trying to implement tab bar controller in my app with UITableView.I have three tab items.And this is the screenshot of Product Tab. I am adding my product to mycart. And after adding two products in my cart this is my MyCart Tab. But after going back to Product Tab and removing or adding any product and returning to the MyCart Tab then it is unchanged. I loaded all my data in viewWillAppear() method. Using breakpoints i found that my UITableView delegate methods are called only one time not every time I changing my tab items.

-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];

AppDelegate *delegate = (AppDelegate *) [[UIApplication sharedApplication] delegate];
handler = [delegate getHandler];

allProducts = [handler getAllProducts];

}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
productDataModel = [allProducts objectAtIndex:indexPath.row];
addToCartFlagNumber = (NSNumber*)productDataModel.productAvailability;

if([addToCartFlagNumber integerValue]){

static NSString *simpleTableIdentifier = @"ProductListTableViewCell";
ProductListTableViewCell *cell = (ProductListTableViewCell*) [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

cell.productListNameLabel.text = productDataModel.productName;
cell.productListImageView.image = [UIImage imageNamed:productDataModel.productImageName];
cell.productListDescriptionLabel.text = productDataModel.productDescription;
cell.addToCartButton.tagValue = [productDataModel.productID integerValue];
cell.addToCartButton.flagValue = 0;


return cell;
}

else{
static NSString *simpleTableIdentifier = @"UpcomingProductListTableViewCell";
UpcomingProductListTableViewCell *cell = (UpcomingProductListTableViewCell*) [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];


cell.productListNameLabel.text = productDataModel.productName;
cell.productListImageView.image = [UIImage imageNamed:productDataModel.productImageName];
cell.productListDescriptionLabel.text = productDataModel.productDescription;

return cell;
}

}

Answer

Use the following code in viewWillAppear method of viewController in all three tab.

[tableView reloadData];