How does [self.tableView reloadData] know what data needs to be reloaded?

It seems to me that my view manager, which is a tableViewController, knows that it is not informed that its NSArray or NSDictionary property contains data that must be loaded into the table for display.

It seems like I should explicitly say something like:

[self.tableView useData:self.MyArray];

I want to have more than one array inside my TableViewController and switch between one and the other programmatically.

I notice that when tableViewController uses searchViewController, you can do this:

if (tableView == self.searchDisplayController.searchResultsTableView) {

I was even able to do this:

self.tableView =  self.searchDisplayController.searchResultsTableView;
[self.tableView reloadData];

But nowhere can I find how to set self.tableView back to the main data source!

+4
source share
2 answers

, , iPhone . beeline , , , - !

Model-View-Controller, API iPhone/ Cocoa. -, , , , . , Apple Documentation.

. , , . , . ( , , .) , , .

-, , tableview. , , , .

. . , , , , :

@interface MyDataModel : NSObject {
@protected
    NSArray *arrayOne;
    NSArray *arrayTwo;
@public
    NSArray *currentlyUsedArray;

}
@property(nonatomic, retain)  NSArray *currentlyUsedArray;

-(void) switchToArrayOne;
-(void) switchToArrayTwo;
-(void) toggleUsedArray;

@end

#import "MyDataModel.h"

@interface MyDataModel ()
@property(nonatomic, retain)  NSArray *arrayOne;
@property(nonatomic, retain)  NSArray *arrayTwo;

@end


@implementation MyDataModel

- (id) init{
    if (self=[super init]) {
        self.arrayOne=//... initialize array from some source
        self.arrayTwo=//... initialize array from some source
        self.currentlyUsedArray=self.arrayOne; //whatever default you want
    }
    return self;
}

-(void) switchToArrayOne{
    self.currentlyUsedArray=self.arrayOne;
}

-(void) switchToArrayTwo{
    self.currentlyUsedArray=self.arrayTwo;
}

- (void) toggleUsedArray{
    if (self.currentlyUsedArray==self.arrayOne) {
        self.currentlyUsedArray=self.arrayTwo;
    }else {
        self.currentlyUsedArray=self.arrayOne;
    }
}

( , currentlyUsedArray. , .)

. - , - .

, tableview :

MyDataModel *theDataModel;
@property (nonatomic, retain) MyDataModel *theDataModel;

@synthesize theDataModel;

-(MyDataModel *) theDataModel; {
    if (theDataModel; !=nil) {
        return theDataModel; ;
    }
    id appDelegate=[[UIApplication sharedApplication] delegate];
    self.theDataModel=appDelegate.theDataModelProperty;
    return theDataModel;
}

datasource tableview:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ...
    cell.textLabel.text=[self.theDataModel.currentlyUsedArray objectAtIndex:indexPath.row];
    return cell;
}

- , .

id appDelegate=[[UIApplication sharedApplication] delegate];
[appDelegate.theDataModelProperty toggleUsedArray];

, - , .

? . , -, . , , - . .

. , . , . , .

, , , .

, , , ? , . tableview - , . what-data tableview. ( , , , URL-, reloadData , .)

MVC, , , .

, , , .

+41

" " - , , . , , .

. Apple:

UITableView . Model-View-Controller , ( ) ; , , . ( ) , UITableViewController.

; , . :

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

, , . , // .

, , , ? , , . , , UITableViewController XCode "" . .m , .


: , . , "tableView", , tableView:cellForRowAtIndexPath:, , , . , /, , . .

+2

All Articles