需要填充一个数组的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,
添加新文件但是标题会复制原来的。
比如:
添加文件之前的tableView
++++++++++++++++++++++++++
text.txt
++++++++++++++++++++++++++
testing.txt
++++++++++++++++++++++++++
添加了文件othertest.txt之后:
```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;
因为你只有分配新单元时才设置单元标签text,试试这样:
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid];
}
cell.textLabel.text = [_files objectAtIndex:indexPath.row];
return cell;
同样的代码,不过我删除了设置单元文本那行,这样在你创建新的或者重新使用cell都能执行。