我有一个tableview设置了来自可变数组的数据,但我无法弄清楚如何重新加载它,或者即使它是在第一个地方加载的。如何重新加载可变数组的ios6 tableView?
可变数组在用户按下按钮时发生更改。当我用测试数据初始化数组时,表视图工作正常。我想我需要重新加载表格,但是我在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
的.m:
#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];**
[email protected]"";
[email protected]"";
}
@end
谢谢!我已经尝试在我的添加操作方法中重新加载,但希望通过这些初始化修复它应该可以工作。阅读更多文档并试用后,我会尽快与您联系! –
嘿,我做了一些改变,我相信我非常接近,但表格视图不会在新数据中加载。你有什么其他的建议?我更新了由** –
@NetworkIntern包围的主要文章的更改在'add:'方法的表视图中重新载入数据之前,是否获得更新的'currentNames'?在重新加载数据之前NSLog数组,并且我认为你会看到你的数组没有更新,这就是为什么table view不显示更新的数据。 – Zen