2013-06-05 29 views
0

我有一个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 

回答

1

TableView使用数据源(NSArray S或NSMutableArray多个)来填充本身。它使用各种委托方法来知道它将如何被填充。所以在你的情况下,你正在使用你的currentNames,这是一个NSMutableArray的实例。当您操作数组内的数据时,您会希望TableView显示现在编辑的数组。

所以,刷新你的表视图编辑完成后您的按钮的操作方法后阵,你应该调用此方法

[self.tableView reloadData] 

那里。

注意: - 你会希望自己的数据源阵列有一个范围,直到您的视图控制器持续,所以如果你已经在你的Interface文件中创建一个NSMutableArray财产,不重建的同名NSMutableArray实例中您的Implementation文件(不要忽略编译器警告)。

在你viewDidLoad方法,使用

self.currentNames = [[NSMutableArray alloc] init]; 

虽然它的方式创建一个内存泄漏,但严重的是,经过TableViewdocumentation你前进之前,请先彻底。

+0

谢谢!我已经尝试在我的添加操作方法中重新加载,但希望通过这些初始化修复它应该可以工作。阅读更多文档并试用后,我会尽快与您联系! –

+0

嘿,我做了一些改变,我相信我非常接近,但表格视图不会在新数据中加载。你有什么其他的建议?我更新了由** –

+0

@NetworkIntern包围的主要文章的更改在'add:'方法的表视图中重新载入数据之前,是否获得更新的'currentNames'?在重新加载数据之前NSLog数组,并且我认为你会看到你的数组没有更新,这就是为什么table view不显示更新的数据。 – Zen