我有一个使用可变数组中的数据设置的表格视图,但我无法弄清楚如何重新加载它,或者即使它首先被加载。
当用户按下按钮时,可变数组会发生变化。当我用测试数据初始化数组时,表格视图工作正常。我想我需要重新加载表格,但我在 cellForRowAtIndexPath 中没有成功。我还尝试通过 .h 合成 tableView 并在从单击添加按钮的方法添加到数组后重新加载(尝试未反映在代码中,但如果您认为我做错了,请继续并发布正确的方法)。
没有错误,但是当我单击添加按钮时没有任何反应。
任何帮助是极大的赞赏。
相关代码:
。H:
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITextField *name;
@property (weak, nonatomic) IBOutlet UITextField *company;
- (IBAction)add:(id)sender;
**@property (nonatomic, retain) NSMutableArray *currentNames;
@property (nonatomic, retain) NSMutableArray *currentCompanies;
@property (nonatomic, retain) NSMutableArray *names;
@property (nonatomic, retain) NSMutableArray *companies;
@property (weak, nonatomic) IBOutlet UITableView *preExisting;**
@end
米:
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
**@synthesize name, company, currentNames, currentCompanies, names, companies;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSMutableArray *currentNames = [[NSMutableArray alloc] init];
NSMutableArray *currentCompanies = [[NSMutableArray alloc] init];
NSMutableArray *names = [[NSMutableArray alloc] init];
NSMutableArray *companies = [[NSMutableArray alloc] init];
self.preExisting = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame] style:UITableViewStylePlain];**
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
this.tableView = tableView;
return [currentNames count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"SimpleTableItem";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
cell.textLabel.text = [currentNames objectAtIndex:indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView reloadData];
}
- (IBAction)add:(id)sender {
BOOL exists = [names containsObject:name.text];
if(exists == FALSE){
[currentNames addObject:[name text]];
[currentCompanies addObject:[company text]];
[names addObject:[name text]];
[companies addObject:[company text]];
}
else{
[currentNames addObject:[name text]];
[currentCompanies addObject:[company text]];
}
[currentNames addObject:[name text]];
**[_preExisting reloadData];**
name.text=@"";
company.text=@"";
}
@end