滚动后UITableViewCell数据消失

NaX*_*Xir 2 objective-c uitableview ios automatic-ref-counting

我在弧项目中有一个tableView.当我滚动它甚至一秒钟时,所有数据都被隐藏或消失.

我通过Strong属性从另一个控制器传递数据.

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];
Run Code Online (Sandbox Code Playgroud)

这是我的财产

@property(strong, nonatomic) NSArray* cArray;
Run Code Online (Sandbox Code Playgroud)

CTableViewController.m tableView方法

- (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;
}
Run Code Online (Sandbox Code Playgroud)

rma*_*ddy 21

问题出在您创建视图控制器的代码中,并将新视图控制器的视图添加为子视图.就目前而言,您不会保留对视图控制器的引用.因此视图控制器被取消分配,并且表没有委托或数据源.

正确的解决方案是使用UIViewController容器方法并将新视图控制器添加到当前视图控制器.

呼叫:

[self addChildViewController:cTableVc];
Run Code Online (Sandbox Code Playgroud)

后:

self.view addSubview:cTableVc.view];
Run Code Online (Sandbox Code Playgroud)

请参阅文档,UIViewController因为除了这一行之外还有更多需要完成的工作.