iphone - iOS NSMutableArray 卡住

标签 iphone ios objective-c uitableview nsmutablearray

我正在尝试从包含目录中文件的数组中填充 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/

相关文章:

iphone - iOS:获取用户移动的速度

ios - UITableView endUpdates 在 CoreData 更新时花费很长时间

iphone - 使用条件语句以编程方式切换UIButton文本

iphone - 我怎样才能摆脱这个警告?

iPhone Store Kit 返回无效产品 ID 错误

javascript - 延迟加载图像在 iPhone 浏览器中不起作用

c++ - 从继承 CCSprite 的类创建 Sprite

ios - 使用新的 @3x 框架正确构图

iPhone 应用程序崩溃在后台被杀死

objective-c - 如何将 undoManager 与核心数据实体一起使用