Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UITableViewCell数据在滚动后消失_Ios_Objective C_Automatic Ref Counting_Uitableview - Fatal编程技术网

Ios UITableViewCell数据在滚动后消失

Ios UITableViewCell数据在滚动后消失,ios,objective-c,automatic-ref-counting,uitableview,Ios,Objective C,Automatic Ref Counting,Uitableview,我在arc项目中有一个tableView。当我滚动它一秒钟,所有的数据都会隐藏或消失 我通过强属性从另一个控制器传递数据 CTableViewController* cTableVc=[[CTableViewController alloc] initWithNibName:@"CTableViewController" bundle:nil]; cTableVc.cArray=[[NSArray alloc] initWithArray:[MyVC pathForAllCardsInDocum

我在arc项目中有一个tableView。当我滚动它一秒钟,所有的数据都会隐藏或消失

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

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];
这是我的财产

@property(strong, nonatomic) NSArray* cArray;
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;
}

问题在于创建视图控制器并将新视图控制器的视图添加为子视图的代码。目前,您没有保留对视图控制器的引用。因此,视图控制器被解除分配,表中没有委托或数据源

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

电话:

之后:

self.view addSubview:cTableVc.view];

查看
UIViewController
的文档,因为除了这一行,还需要做更多的工作。

您的
cellforrowatinex:
不使用
cArray
中的数据。为什么不呢?事实上,它是用我删除了这一行来创造简单性的。编辑以使其清晰。什么是
[self.view cTableVc.view]在第一段代码中?您所说的“所有数据隐藏或消失”是什么意思?你的意思是细胞完全变为空白?没有图像,没有显示“我的卡X”的标签?您是否保留对
cTableVc
视图控制器的引用?只要将视图控制器的视图作为当前视图控制器的子视图放置,就需要保留对该视图控制器的强引用。更好的是,将视图控制器添加到当前视图控制器(请参阅
UIViewController
docs)。@rmaddy,谢谢。你救了我一天。
self.view addSubview:cTableVc.view];