gpt4 book ai didi

ios - UITableViewCell 滚动后数据消失

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:49:07 24 4
gpt4 key购买 nike

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

我正在通过 Strong 属性从另一个 Controller 传递数据。

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)

@property(strong, nonatomic) NSArray* cArray;

CTableViewController.m 表格 View 方法

- (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;
}

最佳答案

问题出在您创建 View Controller 并将新 View Controller 的 View 添加为 subview 的代码中。就目前而言,您不会保留对 View Controller 的引用。因此, View Controller 被释放,表中没有委托(delegate)或数据源。

正确的解决方案是使用 UIViewController 容器方法并将新的 View Controller 添加到当前 View Controller 。

调用:

[self addChildViewController:cTableVc];

之后:

self.view addSubview:cTableVc.view];

请参阅 UIViewController 的文档,因为需要完成的不仅仅是这一行。

关于ios - UITableViewCell 滚动后数据消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23595495/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com