ios - 为什么只调用一次 UITableView Delegate 方法?

标签 ios objective-c uitableview uitabbarcontroller

我正在尝试使用 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/

相关文章:

ios - 关闭 SearchBar 上的自动建议

ios - 归档时 : A struct gives "Redundant protocol error", 而不是运行时

ios - 奇怪的 UITable Separator 问题 - 右移

ios - 当值长度== 1时,如何在值前添加 `0`

ios - Objective-C : Segue back to root from NSObject class

c++ - 自己线程中的 NSURLConnection 在没有自旋循环的情况下无法工作

ios - NSLog 输出消息在控制台中重复两次(重复)

ios - 以编程方式将 UILabel 添加到 UITableView 未正确加载

ios - UITableView 尾部滑动短与长或长滑动后返回单元格

ios - 在保持向后兼容性的同时采用 os_log API