简体   繁体   English

滚动后UITableViewCell数据消失

[英]UITableViewCell Data disappears after scroll

I have a tableView in an arc project. 我在弧项目中有一个tableView。 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. 我通过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];

Here is my property 这是我的财产

@property(strong, nonatomic) NSArray* cArray;

CTableViewController.m tableView methods 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;
}

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. 正确的解决方案是使用UIViewController容器方法并将新视图控制器添加到当前视图控制器。

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. 请参阅UIViewController的文档,因为除了这一行之外还有更多需要完成的工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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