2013-03-28 89 views
0

我在UIView的控制器产生一个UITableView无法reloaddata(UITableView的)

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 470, self.view.frame.size.height) style:UITableViewStylePlain]; 
    self.tableView.dataSource = self; 
    self.tableView.delegate = self; 
    [self.view addSubview:self.tableView]; 
    NSLog(@"tableView is '%@'",self.tableView); 
} 

所述的NSLog结果:“是的tableView'<的UITableView:0x7c60c00;帧=(0 0; 470 1004); clipsToBounds = YES; gestureRecognizers =;层=; contentOffset:{0,0}>'”

的refreshTableView方法将被调用,当itemArray被其他控制器改性

**SideBarViewController *sideBarViewController = [[SideBarViewController alloc]init]; 
[sideBarViewController refreshTableView];** 

-(void)refreshTableView { 
    [self createItemArray]; 
    NSLog(@"reload itemArray->%@",self.itemArray); 
    NSLog(@"tableView is '%@'",self.tableView); 
    [self.tableView reloadData]; 
} 

不知何故,NSLog的结果变成:“tableView是'(null)'”。和tableView将不会被重新加载。

为什么?请帮忙。谢谢。

更新:

itemArray的NSLog的很好:

改变之前 重装itemArray - >( “test.md” )

变更后 重装itemArray-> ( “test.md”, “test2.md” )

UPDATE2:

#pragma mark - Table view data source 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return 1; 
} 

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 

    cell.textLabel.text = [self.itemArray objectAtIndex:indexPath.row]; 
    return cell; 
} 

更新3:

.h 
@interface SideBarViewController : UIViewController<IIViewDeckControllerDelegate,UITableViewDataSource,UITableViewDelegate> 

.m 
@interface SideBarViewController() 
@property (nonatomic,strong) UITableView *tableView; 
@end 

@implementation SideBarViewController 
@synthesize tableView = _tableView; 

更新4:

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath*)indexPath 

方法工作正常,之后删除的行的的tableview是重新加载。

+0

重载表之前,其工作正常? – iPatel 2013-03-28 06:34:07

+0

是的,工作正常,只是无法重新加载tableview – 2013-03-28 06:35:09

+1

您提供的代码看起来很好 - 错误必须在代码的其他地方。 – ColinE 2013-03-28 06:36:08

回答

0

你可以尝试使用通知

  1. 添加到观察你的viewDidLoad在Sidebarvc

    [NSNotificationCenter defaultCenter]的addObserver:自 选择:@选择(refreshTableView :) 名:@“dataChanged” object:nil];

  2. 创建一个方法来刷新

    - (空)refreshTableView:(NSNotification *)通知符{ }

  3. 在其他VC,只是等待通知

    [NSNotificationCenter defaultCenter] postNotificationName:@“dataChanged” object:nil];

4.don't忘记清洁

add [[NSNotificationCenter defaultCenter] removeObserver:self]; to ViewDidUnload 

应该工作。

1

分享一些更多的代码,以便我们可以找到确切的问题。

你可以检查你的tableView的实际名称?共享您在.h文件中声明的表的一些代码。

检查数组,数据在重新加载或更新之前可用并且表视图方法正常工作。

检查您是否在.h文件中定义tableView委托?并实施的tableView的委托方法在你的班上正确..

@interface MyViewController:UIViewController中

还有一件事检查已正确实施您的数据源和delagate方法。

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 

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

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

如果你已经正确这个东西,然后加载之前检查你的数组包含数据或不。可能这会帮助你。

+0

谢谢,更新了我的文章。 – 2013-03-28 06:48:03

+0

已更新,请检查更新3,谢谢 – 2013-03-28 06:54:03

+0

检查是否在xcode中收到任何警告? – Nirav 2013-03-28 07:01:36

0
// 
    // ViewController.h 
    // tableData 
    // 
    // Created by Nirav Jain on 3/28/13. 
    // Copyright (c) 2013 SI. All rights reserved. 
    // 

    #import <UIKit/UIKit.h> 

    @interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate> 
    { 
     int rows; 
    } 
    @end 


// tableData 
// 
// Created by Nirav Jain on 3/28/13. 
// Copyright (c) 2013 SI. All rights reserved. 
// 

#import "ViewController.h" 

//@implementation SideBarViewController 


@interface ViewController() 
@property (nonatomic,strong) UITableView *tableView; 
@end 

@implementation ViewController 

@synthesize tableView = _tableView; 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 470, self.view.frame.size.height) style:UITableViewStylePlain]; 
    self.tableView.dataSource = self; 
    self.tableView.delegate = self; 
    [self.view addSubview:self.tableView]; 
    NSLog(@"tableView is '%@'",self.tableView); 
    rows = 5; 


    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonItemStyleDone target:self action:@selector(refreshTableView)]; 
} 


-(void)refreshTableView { 

    rows = 12; 
    NSLog(@"tableView is '%@'",self.tableView); 
    [self.tableView reloadData]; 
} 
#pragma mark - Table view data source 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return rows; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 

    cell.textLabel.text = [ NSString stringWithFormat: @"%d", indexPath.row]; 
    return cell; 
} 

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

@end 
+0

Tom检查此代码。代码对我来说工作正常。 – Nirav 2013-03-28 07:10:05

+0

我完成了我的asnwers ... :) – Nirav 2013-03-28 07:12:28

+0

非常感谢,我试图 – 2013-03-28 07:14:10