Reputation: 2413
I have a tableView in an arc project. When i scroll it even for a sec all data gets hidden or disappears.
I am passing a data from another controller via Strong property.
CTableViewController* cTableVc=[[CTableViewController alloc] initWithNibName:@"CTableViewController" bundle:nil];
cTableVc.cArray=[[NSArray alloc] initWithArray:[MyVC pathForAllCardsInDocumentDiretory]];
cTableVc.view.frame=CGRectMake(0, 0, cTableVc.view.frame.size.width, cTableVc.view.frame.size.height);
[self.view addSubview:cTableVc.view];
Here is my property
@property(strong, nonatomic) NSArray* cArray;
CTableViewController.m tableView methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [_cardArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = @"CTableViewCell";
CTableViewCell *cell = (CTableViewCell *) [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CTableViewCell" owner:self options:nil];
for (id currentObject in topLevelObjects){
if ([currentObject isKindOfClass:[UITableViewCell class]]){
cell = (CTableViewCell *) currentObject;
break;
}
}
}
// Configure the cell...
NSString* strPath=[cArray objectAtIndex:indexPath.row];
cell.cardImageView.image=[UIImage imageWithContentsOfFile:strPath];
cell.cardNameLbl.text=[NSString stringWithFormat:@"My Card %d", arc4random() % 9];
cell.selectionStyle=UITableViewCellSelectionStyleNone;
return cell;
}
Upvotes: 2
Views: 2923
Reputation: 318814
The problem is with the code where you created the view controller and add the new view controller's view as a subview. As it stands, you don't keep a reference to the view controller. So the view controller gets deallocated and the table is left with no delegate or data source.
The proper solution is to make use of the UIViewController
container methods and add the new view controller to the current view controller.
Call:
[self addChildViewController:cTableVc];
after:
self.view addSubview:cTableVc.view];
See the docs for UIViewController
since there is more that needs to be done than just this one line.
Upvotes: 21