简体   繁体   中英

UITableView Delegates

I am trying to call reloadData on my UITableView . I am making my app without interface builder.

This code would works with Interface Builder, but does not seem to without.

When I compare my code to what I used to do in interface builder, I am missing the bit in code where I would drag the View Controller to the UITableView . I have the delegates and datasource set and working. Is there something I am missing in my code?

EDIT: This is set up in my viewDidLoad The _tableView has the property set and is Synthesized.

//Data is set into an NSArray then copied into an NSMutableArray
_nsarray = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:docPath error:NULL];

_nsmArray = [[NSMutableArray alloc] init];
[_nsmArray addObjectsFromArray:_nsarray];

_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 416)];
_tableView.delegate = self;
_tableView.dataSource = self;
[self.view addSubview:_tableView];


-(void)refreshTable {
    //_tableView = [[UITableView alloc] initWithFrame:CGRectZero];
    [self.tableView reloadData];
}

How are you managing your table data? NSArray?

You need to provide these details as what you have posted should work: setting the dataSource and delegate and calling reloadData . When you simply " reloadData " you need to ensure there were changes to the dataSource. If no changes were made to the dataSource (ie add / remove an object from the array) , then nothing is going to happen.

Are you sure you set correctly the delegate and the data source? Like this:

    [tableView setDelegate:self];
    [tableView setDataSource:self];

And then you have to call the method "reloadData" in the main thread, so you should call your method "refreshTable" like this:

    [self performSelectorOnMainThread:@selector(refreshTable) withObject:nil waitUntilDone:NO];

Strictly saying you need only two things to be able to display data in the table view:

1) self.tableView must be pointing to the table view, so you can call reloadData when required.

2) table view's dataSource property must be assigned to your class, which must implement required methods from the UITableViewDataSource protocol.

Just make sure that you have added UITableViewDelegate and UITableViewDataSource protocols in your .h file.

@interface YourClassName : UIViewController<UITableViewDelegate, UITableViewDataSource>

and then set your tableview delegate and dataSource to self.

EX:

    [yourTableView setDelegate:self];
    [yourTableView setDataSource:self];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM