gpt4 book ai didi

iphone - iOS NSMutableArray 卡住

转载 作者:行者123 更新时间:2023-11-28 20:21:14 25 4
gpt4 key购买 nike

我正在尝试从包含目录中文件的数组中填充 UITableView

//in my header
@property (strong, nonatomic) NSMutableArray *files;
//in my tableView:cellForRow:atIndexPath:
static NSString *cellid = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid];
cell.textLabel.text = [_files objectAtIndex:indexPath.row];
}
return cell;

(_files 设置为等于 [[NSFileManager defaultManager] contentsOfDirectoryAtPath:[self downloadsDir]; 在调用此方法之前)这有效,并显示正确的文件.问题是如果我将一个文件添加到目录,然后使用 tableView reloadData,将添加一个新文件,但标题将是另一个文件的副本。示例

添加文件前的表格 View

++++++++++++++++++++++++++
text.txt
++++++++++++++++++++++++++
testing.txt
++++++++++++++++++++++++++

添加文件 othertest.txt 后的表格 View

++++++++++++++++++++++++++
text.txt
++++++++++++++++++++++++++
testing.txt
++++++++++++++++++++++++++
testing.txt
++++++++++++++++++++++++++

应该是

++++++++++++++++++++++++++
text.txt
++++++++++++++++++++++++++
testing.txt
++++++++++++++++++++++++++
othertest.txt
++++++++++++++++++++++++++

如果我重新启动应用程序,它将显示正确的文件。但出于某种原因,它这样做了。任何人都知道可能出了什么问题?

最佳答案

if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid];
cell.textLabel.text = [_files objectAtIndex:indexPath.row];
}
return cell;

除非分配新单元格,否则您不会设置单元格标签文本。试试这个:

if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid];
}
cell.textLabel.text = [_files objectAtIndex:indexPath.row];
return cell;

相同的代码,但我移动了设置单元格文本的行,以便在您重复使用单元格以及创建新单元格时执行它。

关于iphone - iOS NSMutableArray 卡住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15892593/

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