Tom H
Tom H

Reputation: 517

Unable to reloaddata(UITableView)

I'm creating a UITableView in an UIView controller

- (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);
}

the NSLog result :"tableView is '< UITableView: 0x7c60c00; frame = (0 0; 470 1004); clipsToBounds = YES; gestureRecognizers = ; layer = ; contentOffset: {0, 0}>'"

the refreshTableView method will be called, when the itemArray is modified by other controller

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

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

somehow, the NSLog result become:"tableView is '(null)'". and the tableView won't be reloaded.

why? please help. thanks.

Update:

the NSLog of itemArray was fine:

before change reload itemArray->( "test.md" )

after change reload 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;
}

update 3:

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

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

@implementation SideBarViewController
@synthesize tableView = _tableView;

update 4: the

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

method is working fine, after deleted rows the tableview was reloaded.

Upvotes: 0

Views: 339

Answers (3)

user843910
user843910

Reputation:

You can try to use Notification

  1. add Observer into you viewDidLoad in Sidebarvc

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshTableView:) name:@"dataChanged" object:nil];

  2. create a method to refresh

    -(void)refreshTableView:(NSNotification *)notif{ }

  3. on the other VC,just wait for the notification

    [[NSNotificationCenter defaultCenter] postNotificationName:@"dataChanged" object:nil];

4.don't forget to clean it

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

Should work.

Upvotes: 0

Nirav Jain
Nirav Jain

Reputation: 5107

  //
    //  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

Upvotes: 0

Nirav Jain
Nirav Jain

Reputation: 5107

Share some more code so we can find the exact issue.

can you check the actual name of your tableView? share some code of the table you have declared in .h file.

Check array, data is available before reload or update and table view methods are working fine.

Check you define tableView delegates in .h file or not? And implement delegate methods of tableView in your class properly..

@interface MyViewController:UIViewController

And one more thing check you have properly implemented your data source and delagate methods.

- (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.

If you have done this thing correctly then check your array before load it contains data or not. May be this will help you.

Upvotes: 1

Related Questions