问题
I'm writing an app hat has many views and I used sliding views library (ECSlidingViews
). The problem is when I fill an array with objects and fill the table with the objects in tableView:cellForRowIndexPath:
method, it does present the data in the table, but when I go to other view and come back the data disappears because tableView:cellForRowIndexPath:
is not called. Here is the code:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"begin of cellForRowAtIndexPath");
SchedualeCell *cell = (SchedualeCell *)[tableView dequeueReusableCellWithIdentifier:@"Cell"];
Course *temp = [array objectAtIndex:indexPath.row];
cell.nameLb.text = temp.name;
cell.hourLb.text = [NSString stringWithFormat:@"%d",temp.hour];
cell.startTimeLb.text = temp.startTime;
cell.endTimeLb.text = temp.endTime;
NSLog(@"End of cellForRowAtIndexPath");
return cell;
}
tableView:numberOfRowsInSection:
and numberOfSectionsInTableView:
is invoked when I come back to the view that has the table view.
P.S.: The view is UIViewController
that has table view inside of it, and I searched all StackOverflow before posting my problem.
EDIT : this is where I set the delegate and datasource
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(@"Did Appear");
// Do any additional setup after loading the view.
self.view.layer.shadowOpacity = 0.75f;
self.view.layer.shadowRadius = 10.0f;
self.view.layer.shadowColor= [UIColor blackColor].CGColor;
if (![self.slidingViewController.underLeftViewController isKindOfClass:[MenuViewController class]])
{
self.slidingViewController.underLeftViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"Menu"];
}
[self.view addGestureRecognizer:self.slidingViewController.panGesture];
if (array == nil)
{
array = [[NSMutableArray alloc]init];
}
table.delegate = self;
table.dataSource = self;
[table reloadData];
}
and I did included The delegate and datasource in the header
@interface MainViewController : UIViewController<UITableViewDataSource , UITableViewDelegate,addDelegate>
回答1:
First of all , it's not numberOfRowsAtSection
and numberOfTableView
. It's numberOfSectionsInTableView
and numberOfRowsInSection
.
Things you can do :
1) NSLog
your numberOfRowsInSection
. Note that , If it's "0" then your cellForRowAtIndexPath
is never going to be called.
2) If you are using your UITableView
inside some UIView
then you should add :
[self.view addSubview:table];
3) Don't Forget to include :
@interface yourViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>
回答2:
Once check that you reframed the tableview
in ViewWillAppear
.
Some times if we set out of frame, CellForRowAtIndexPath
will not call.
来源:https://stackoverflow.com/questions/16171899/cellforrowatindexpath-is-not-invoked