从 iOS 中的另一个类重新加载表数据 [英] Reloading table data from another class in iOS

查看:21
本文介绍了从 iOS 中的另一个类重新加载表数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从我的问题可以看出,我是 iOS 开发的初学者.我看过几个教程和几个问题,但似乎没有一个涵盖我的情况(我可能遗漏了一些东西).我只是尝试从一个类向数组添加元素,然后调用从另一个类重新加载表数据的方法.但是,当我从另一个类调用相关方法后尝试重新加载数据时,无法加载表(似乎没有添加新数据).事实上,甚至表中数组的先前值似乎都没有了.

As will be seen from my question, I'm a beginner to iOS development. I have looked at several tutorials and several questions but none seem to be covering my case (I'm probably missing something). I am simply trying to add elements to an array from one class, then call the method that reloads the table data from another class. However, when I try to reload the data after calling the relevant method from another class, the table fails to be loaded (no new data seems to be added). In fact, even the previous values of the array in the table seem to be gone.

A 类:

MyClassA.h

@interface MyClassA : UIViewController <UITableViewDelegate, UITableViewDataSource>
{
     UITableView *tableView;
     NSMutableArray *Elements;
}
@property UITableView *tableView;
@property NSMutableArray *Elements;

-(void) addElement: (NSString *) ElementName;

@end

MyClassA.m

@implementation MyClassA
{
    NSString *ElementName[10];
}

- (void) viewWillAppear:(BOOL)animated
{
    NSLog(@"MyClassA: viewWillAppear");

    ElementName[0] = @" 1 ";
    ElementName[1] = @" 2 ";
    ElementName[2] = @" 3 ";

    Elements = [[NSMutableArray alloc]initWithObjects:ElementName[0], ElementName[1],  ElementName[2],nil];
    self.tableView.dataSource = self;  // The table successfully loads with the data element
}

-(void) addElement: (NSString *) ElementName
{
    NSLog(@"Entered addElement");  // This method is successfully accessed
    Elements = [[NSMutableArray alloc]initWithObjects:ElementName[0], ElementName[1], ElementName[2],nil];   // The problem is here, printing data in this array shows they have no value
    [self.tableView reloadData];    // The problem is here. This does not load the data

}

<小时>

B 类:


Class B:

MyClassB.h

@class MyClassA
@interface MyClassB : UIViewController

@property (nonatomic, strong) MyClassA *MyClassACall

-(IBAction) MyButtonClicked: (id) sender;
@end

MyClassB.m

@implementation MyClassB
@synthesize MyClassACall;

-(id) init
{
    self = [super init]
    if (self) {
    NSLog(@"MyClassB init");
    MyClassACall = [[MyClassA alloc] init];
    }
    return self;
}
-(IBAction)MyButtonClicked:(id)sender
{
    NSLog("My button is clicked");
    [self.MyClassAcall addElement:@"NewElement"];
}

我成功获取了所有 NSlog,并且使用提供的数据第一次正确加载了表.但是,当尝试通过从另一个类加载方法来添加数据时,表数据保持不变并且不会重新加载.我在这里错过了什么?

I successfully get all the NSlogs, and the table loads the first time correctly with the provided data. However, when trying to add data by loading a method from another class, the table data remains the same and does not get reloaded. What am I missing here?

为了保持简单,我没有加载整个代码.我希望这是有道理的,如果我能澄清,请告诉我.如果存在类似问题,请向我指出,感谢您的帮助.

I have not loaded the whole code to keep this simple. I hope that this makes sense and please let me know if I could clarify. If a similar question exists, please point me to it and I appreciate your help.

推荐答案

我在这里为您制作了一个迷你教程.

I've made a mini tutorial here for you.

好的,假设我们有这样的场景:

OK, so let's say we got a scenario like this:

有两个视图控制器 - ViewControllerAViewControllerB.

There are two view controllers - ViewControllerA and ViewControllerB.

ViewControllerA 将负责添加项目.

ViewControllerA will be in charge of adding items.

ViewControllerB 将负责显示项目.

ViewControllerB will be in charge of displaying the items.

因此 ViewControllerA 将如下所示:

So ViewControllerA will look like this:

ViewControllerB 将如下所示:

and ViewControllerB will look like this:

#import <UIKit/UIKit.h>

@interface ViewControllerA : UIViewController

@property (nonatomic, strong) NSMutableArray *arrItems;

@end

在这里,我们将名为 arrItems 的数据源存储为 NSMutableArray.稍后我们会将这个数组传递给 ViewControllerB.

Here, we store the data source called arrItems as a NSMutableArray. We will pass this array to ViewControllerB later.

#import "ViewControllerA.h"
#import "ViewControllerB.h"

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    [self initViews];
}

-(void)initViews
{
    self.view.backgroundColor = [UIColor whiteColor];

    self.navigationItem.title = @"View Controller A";

    UIButton *btnAddItem = [[UIButton alloc] initWithFrame:CGRectMake(60, 200, 200, 50)];
    [btnAddItem setTitle:@"Add Item" forState:UIControlStateNormal];
    btnAddItem.backgroundColor = [UIColor greenColor];
    btnAddItem.layer.cornerRadius = 5.0;

    [btnAddItem addTarget:self action:@selector(addItem) forControlEvents:UIControlEventTouchUpInside];




    UIButton *btnViewData = [[UIButton alloc] initWithFrame:CGRectMake(60, 300, 200, 50)];
    [btnViewData setTitle:@"View Data" forState:UIControlStateNormal];
    btnViewData.backgroundColor = [UIColor blueColor];
    btnViewData.layer.cornerRadius = 5.0;

    [btnViewData addTarget:self action:@selector(viewData) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:btnAddItem];
    [self.view addSubview:btnViewData];


    // init empty array to hold data source items
    self.arrItems = [[NSMutableArray alloc] init];
}

-(void)addItem
{
    [self.arrItems addObject:@"New Element"];

    NSLog(@"added a new element to arrItems, arrItems now has %u items", self.arrItems.count);
}

-(void)viewData
{
    ViewControllerB *vcB = [[ViewControllerB alloc] initWithItems:self.arrItems];

    [self.navigationController pushViewController:vcB animated:YES];
}

对于绿色的添加项目"按钮,我们使用这样的方法将项目添加到我们的数据源:

For the green "Add Item" button, we use a method like this to add item to our data source:

-(void)addItem
{
    [self.arrItems addObject:@"New Element"];

    NSLog(@"added a new element to arrItems, arrItems now has %u items", self.arrItems.count);
}

您会注意到,当您点击绿色按钮时,您会看到一个控制台日志,告诉您阵列中当前有多少项.

You'll notice when you tap on the green button, you'll get a console log telling you how many items is currently in your array.

现在,当我们完成添加项目时,我们有蓝色的查看数据"按钮将 ViewControllerB 推送到导航堆栈上:

Now when we're done adding items, we have the blue "View Data" button which pushes ViewControllerB onto the navigation stack:

-(void)viewData
{
    ViewControllerB *vcB = [[ViewControllerB alloc] initWithItems:self.arrItems];

    [self.navigationController pushViewController:vcB animated:YES];
}

ViewControllerB.h

#import <UIKit/UIKit.h>

@interface ViewControllerB : UIViewController <UITableViewDataSource, UITableViewDelegate>

-(id)initWithItems:(NSArray *)arrItems;

// ----------------------------------------------------------------
// view controller B data source is set from
// view controller A using init method shown above
// ----------------------------------------------------------------
@property (nonatomic, copy) NSArray *arrItems;

@property (nonatomic, strong) UITableView *tableView;


@end

在这里,我们声明了一个带有 NSArray 参数的 init 方法.这个方法将允许我们将数据源数组从 ViewControllerA 注入到 ViewControllerB 中.

Here, we've declared a init method that takes a NSArray parameter. This method will allow us to inject the data source array from ViewControllerA into ViewControllerB.

按照惯例,我们在 ViewControllerB 中也有 tableView.

Per the usual, we also have the tableView in ViewControllerB.

-(id)initWithItems:(NSArray *)arrItems
{
    self = [super init];

    if(self)
    {
        self.arrItems = arrItems;
    }

    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    [self initViews];
}

-(void)initViews
{
    self.navigationItem.title = @"View Controller B";

    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;

    [self.view addSubview:self.tableView];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arrItems.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellID = @"cellID";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];

    if(cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }

    cell.textLabel.text = self.arrItems[indexPath.row];

    return cell;
}

注意 initWithItems: 方法中,我们将传入参数 arrItems 中的数据源存储到 ViewControllerB 的 self.arrItems 中:

Notice in the initWithItems: method, we're storing the data source from the passed in parameter arrItems into ViewControllerB's self.arrItems:

-(id)initWithItems:(NSArray *)arrItems
{
    self = [super init];

    if(self)
    {
        self.arrItems = arrItems;
    }

    return self;
}

ViewControllerB 然后可以使用这些数据并将其显示在 tableView 数据源方法中.

ViewControllerB can then use this data and display it in the tableView data source methods.

因此,如果您单击添加项目"五次,您最终会得到这样的结果:

So you end up with something like this if you click Add Item five times:

Xcode 的控制台还记录了 5 个添加的项目:

Xcode's console also logs 5 items added:

2014-12-08 10:33:39.195 DataPassingDemo[1211:25705] added a new element to arrItems, arrItems now has 1 items
2014-12-08 10:33:40.099 DataPassingDemo[1211:25705] added a new element to arrItems, arrItems now has 2 items
2014-12-08 10:33:40.619 DataPassingDemo[1211:25705] added a new element to arrItems, arrItems now has 3 items
2014-12-08 10:33:41.123 DataPassingDemo[1211:25705] added a new element to arrItems, arrItems now has 4 items
2014-12-08 10:33:41.667 DataPassingDemo[1211:25705] added a new element to arrItems, arrItems now has 5 items

这样更清楚吗?

这篇关于从 iOS 中的另一个类重新加载表数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆