我正在尝试使用 UITableView 在我的应用程序中实现选项卡栏 Controller 。我有三个选项卡项。这是 Product Tab 的屏幕截图.我正在将我的产品添加到我的购物车。在我的购物车中添加两个产品后,这是我的 MyCart Tab .但是在返回到“产品”选项卡并删除或添加任何产品并返回到“我的购物车”选项卡后,它没有变化。我在 viewWillAppear() 方法中加载了所有数据。使用断点,我发现我的 UITableView 委托(delegate)方法只被调用一次,而不是每次我更改选项卡项时都被调用。
-(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;
}
}
最佳答案
在所有三个选项卡中的 viewController 的 viewWillAppear 方法中使用以下代码。
[tableView reloadData];
关于ios - 为什么只调用一次 UITableView Delegate 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38823052/