在UITableViewController上不起作用reloadData

时间:2015-02-13 13:34:20

标签: ios objective-c xcode uitableview reloaddata

我想将textField中的数据添加到UITableViewController中。 委派和传输数据工作正常。我的mutableArray正在修改,他看起来很核心但是tableView没有重新加载。

tableViewController.h
#import <UIKit/UIKit.h>
#import "ViewController.h"

@interface TableViewController : UITableViewController <UITableViewDelegate,UITableViewDataSource, trecereDateDelegate>
@property (strong, nonatomic) NSMutableArray *todoitems;

tableViewController.m

- (void)viewDidLoad {
    [super viewDidLoad];

    _todoitems = [[NSMutableArray alloc]initWithArray:[NSArray arrayWithObjects:@"abc",@"adasda",@"asdadq", nil]];
}
- (void)trans

ferData:(NSString *)textNou{
        [_todoitems addObject:textNou];
        [self.tableView reloadData];

    }- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

        return 1;
    }

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

        return _todoitems.count;
    }


    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"tableCell" forIndexPath:indexPath];

        NSInteger row=[indexPath row];
        cell.afiseaza.text = _todoitems[row];
        // Configure the cell...


        return cell;
    }

ViewController.h

@class ViewController;

@protocol trecereDateDelegate <NSObject>

-(void) transferData:(NSString *)data;

@end

@interface ViewController : UIViewController <UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UITextField *introdu;

- (IBAction)returnButton:(id)sender;
- (IBAction)saveButton:(id)sender;

@property (weak, nonatomic) id<trecereDateDelegate>delegate;


@end

ViewController.m

- (void)viewDidLoad {
    [super viewDidLoad];

    self.introdu.delegate = self;

}

- (IBAction)saveButton:(id)sender{



    NSString *ceva=_introdu.text;
    [_delegate transferData:ceva];

    TableViewController *xyz=[[UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:@"LISTA"];


    [self showViewController:xyz sender:nil];

}

2 个答案:

答案 0 :(得分:0)

尝试添加一个按钮作为操作:

[_tableView reloadData]; 

查看表格是否重新加载。 如果它重新加载,请尝试在程序的其他部分中移动[_tableView reloadData];来添加数据。

你在任何地方都有[self transferData];吗?

答案 1 :(得分:0)

试试这个: 添加NSString对象以获取输入到文本字段的结果。

<强> TableViewController.h

@interface TableTableViewController : UITableViewController<UITableViewDataSource,UITableViewDelegate>

@property (strong, nonatomic) NSMutableArray *todoitems;

@property (strong,nonatomic) NSString *textFieldString;

@end

<强> TableViewController.m

#import "TableTableViewController.h"

@interface TableTableViewController ()

@end

@implementation TableTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    _todoitems = [[NSMutableArray alloc]initWithArray:[NSArray arrayWithObjects:@"abc",@"adasda",@"asdadq", nil]];
//add object passed for TextField.
    [_todoitems addObject:_textFieldString];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void) transferData:(NSString *)textNou{
    [_todoitems addObject:textNou];
    [self.tableView reloadData];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    // Return the number of rows in the section.
     return _todoitems.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    // Configure the cell...

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"tableCell" forIndexPath:indexPath];

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

    NSInteger row=[indexPath row];
    cell.textLabel.text = _todoitems[row];
    // Configure the cell...

    return cell;    
}

<强> ViewController.h

@protocol trecereDateDelegate <NSObject>

-(void) transferData:(NSString *)data;

@end

@interface ViewController : UIViewController<UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UITextField *introdu;

- (IBAction)returnButton:(id)sender;

- (IBAction)saveButton:(id)sender;

@property (weak, nonatomic) id<trecereDateDelegate>delegate;

@end

<强> ViewController.m

#import "ViewController.h"
#import "TableTableViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
     self.introdu.delegate = self;

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)saveButton:(id)sender{


    NSString *ceva=_introdu.text;
//    [_delegate transferData:ceva];

    TableTableViewController *xyz=[[UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:@"LISTA"];

   // just pass the string from textfield to tableViewController
   xyz.textFieldString = ceva;

    [self showViewController:xyz sender:nil];

}

您不需要委托将数据传递给另一个视图控制器。