【问题标题】:iOS how to acces [tableView reloadData] from another class [closed]iOS如何从另一个类访问[tableView重新加载数据] [关闭]
【发布时间】:2014-03-10 21:38:55
【问题描述】:

我有一个viewController,它有一个属性:UIScrollView,并且同一个控制器包含一个tableView。我的UIScrollView 包含按钮。我如何通过触摸这些按钮来使用[tableView reloadData]? tableView 数据源和委托在 viewController 上。

MyViewController:

@property (strong, nonatomic) MyScrollView* scrollView; 
@property (strong, nonatomic) IBOutlet UITableView *table; 

MyScrollView 类:

没有以编程方式创建的属性和 5 个按钮。

【问题讨论】:

    标签: ios iphone objective-c tableview


    【解决方案1】:

    在更好地了解您要做什么之后,我想我会使用委托。虽然还有其他方法可以做到这一点,但我认为代理很灵活,如果您还没有使用过它们,那么现在是了解它们的好时机。

    如果一个类希望采用我们的协议,我们定义它需要实现的委托方法: MyScrollView.h

    @protocol MyScrollViewDelegate <NSObject>
    -(void)reloadTableData;
    @end
    
    @interface MyScrollView : UIScrollView
    @property (nonatomic,weak) id<MyScrollViewDelegate> delegate;
    @end
    

    然后在实现中我们执行以下操作:

    MyScrollView.m

    // Set the button's target
    [button addTarget:self action:@selector(reloadData) forControlEvents:UIControlEventTouchUpInside];
    
    // Here we check if our delegate responds to the reloadTableData selector and if so we call it
    -(void)reloadData
    {
        if ([self.delegate respondsToSelector:@selector(reloadTableData)])
        {
            [self.delegate reloadTableData];
        }
    }
    

    在MyViewController的实现中我们添加MyScrollViewDelegate并实现reloadTableData方法:

    MyViewController.m

    @interface MyViewController () <MyScrollViewDelegate>
    @property (strong, nonatomic) MyScrollView* scrollView; 
    @property (strong, nonatomic) IBOutlet UITableView *table; 
    @end
    
    // Set the delegate after you've created scrollView
    self.scrollView.delegate = self;
    
    // Once the delegate is called we simply reload the table data
    -(void)reloadTableData
    {
        [self.table reloadData]
    }
    

    【讨论】:

    • 选择器在所有项目中都被识别?因为这个按钮在另一个类中并且 [self.tableView reloadData] 必须在 viewC 中
    • 不,只有当它是self 的方法时才会识别选择器。但是,您可以将指针传递给包含表格视图的视图控制器,而不是 self。我会更新我的答案。
    • 我用另一种方式告诉你:我有@property...*scrollView。在这个类中,您可以找到每个按钮的按钮 + [addTarget]。我不明白如何退出此属性并对包含此属性的类 (myVC) 执行某些操作?
    • 你能用你的课程结构更新你的问题,以便更容易帮助你吗?
    • 与我的项目不同:我的 ScrollView 是 UIScrollView 的子类而不是 NSObject,那么你的第一组行有什么区别?
    【解决方案2】:

    这很简单-

    viewController.m

    // set up an action for your button
    -(IBAction)yourButtonClick{
    
       [yourtablename reloadData];
    
       }
    

    【讨论】:

    • 我不明白。我有一个 UIScrollView 的子类:“scrollView”MyVc:@property ....scrollView,这是程序化制作的,我有一个来自界面生成器的表格。
    • 那么问题是什么??
    【解决方案3】:

    请按照以下方式重新加载表格数据

    .h 文件

    @interface ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>{
        IBOutlet UITableView *exTable1;
        IBOutlet UITableView *exTable2;
    }
    @property (nonatomic, retain) UITableView *exTable1;
    @property (nonatomic, retain) UITableView *exTable2;
    

    给出与.xib的关系

    .m 文件

    @synthesize exTable1;
    @synthesize exTable2;
    
    // reload data when you need 
    [self.exTable1 reloadData];
    
    // reload data when you need 
    [self.exTable2 reloadData];
    

    【讨论】:

      【解决方案4】:

      如果您的视图控制器同时包含表格视图和带有许多按钮的滚动视图,您可以简单地将表格视图连接到也声明为属性的 IBOutlet,然后调用“@ 987654321@" 在该表视图属性上。

      如果表格视图与包含按钮的对象位于不同的视图控制器或类中,您只需要有某种指向包含表格视图的单独视图控制器的指针。如果该表视图被声明为属性,您仍然可以通过执行“[theOtherViewController.tableView reloadData];”之类的操作来重新加载数据(假设该属性名为 tableView 并且指向另一个 VC 的指针名为 theOtherViewController)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-08-24
        • 1970-01-01
        • 2014-12-17
        • 1970-01-01
        • 2014-03-07
        • 2023-03-22
        • 1970-01-01
        相关资源
        最近更新 更多