我有一个问题,我似乎无法在我的 tableView 中重新加载数据。
我想要做的是在我的tableView中填充带有csv扩展名的文档目录中的文件(我已经完成了),然后按下一个按钮,删除所有显示的文件并更新tableView。
按下该按钮会删除文档目录中的文件,但不会使用更新的内容重新加载 tableView。
reloadData 使用 NSLog 来确认数组是空的,但是这个
不更新表。
运行 reloadData,这会用空白数据覆盖数组,但
不更新表。
任何成功:
dispatch_sync(dispatch_get_main_queue(), ^{
[self.tableView reloadData];
});
正确,(tableView 中的所有其他功能都有效,包括
选择/取消选择多个项目并删除单个项目
并且每次tableView都会更新)。
有人可以检查我下面的方法并告诉我为什么 tableView 没有正确重新加载吗?我确定这是我在某处做错了,但我找不到它。
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [filePathsArray count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MainCell"];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MainCell"];
}
_docPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [_docPath objectAtIndex:0];
_fileList = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectory error:nil];
NSPredicate *fltr = [NSPredicate predicateWithFormat:@"self ENDSWITH '.csv'"];
_csvFilesCell = [_fileList filteredArrayUsingPredicate:fltr];
NSLog(@"Contents of directory: %@", _csvFilesCell);
filePathsArray = [[NSFileManager defaultManager]contentsOfDirectoryAtPath:documentsDirectory error:nil];
cell.textLabel.text = [_csvFilesCell objectAtIndex:indexPath.row];
return cell;
}
-(IBAction) deleteStoredData:(id)sender
{
NSLog(@"Delete data button pressed");
UIAlertController*alert = [UIAlertController alertControllerWithTitle:@"Delete stored Data" message:@"Are you sure?" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* OK = [UIAlertAction actionWithTitle:@"Delete" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
NSFileManager *manager = [NSFileManager defaultManager];
// the preferred way to get the apps documents directory
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
// grab all the files in the documents dir
NSArray *allFiles = [manager contentsOfDirectoryAtPath:documentsDirectory error:nil];
// filter the array for only csv files
NSPredicate *fltr = [NSPredicate predicateWithFormat:@"self ENDSWITH '.csv'"];
NSArray *csvFiles = [allFiles filteredArrayUsingPredicate:fltr];
// use fast enumeration to iterate the array
for (NSString *csvFile in csvFiles) {
NSLog(@"PATH %@ : FILE %@", documentsDirectory, csvFile);
NSError *error = nil;
[[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@/%@", documentsDirectory, csvFile]
error:&error];
NSLog(@"Error: %@", error);
NSLog(@"OK button selected, all data deleted");
[self updateDeleteButton];
[self.tableView reloadData];
[alert dismissViewControllerAnimated:YES completion:nil];
}
}];
UIAlertAction* cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
NSLog(@"Cancel button selected, no data deleted");
[alert dismissViewControllerAnimated:YES completion:nil];
}];
[alert addAction:OK];
[alert addAction:cancel];
[self presentViewController:alert animated:YES completion:nil];
}
最佳答案
看到您的代码后,我想出了以下解决方案,希望对您有所帮助!
_docPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [_docPath objectAtIndex:0];
_fileList = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectory error:nil];
NSPredicate *fltr = [NSPredicate predicateWithFormat:@"self ENDSWITH '.csv'"];
_csvFilesCell = [_fileList filteredArrayUsingPredicate:fltr];
NSLog(@"Contents of directory: %@", _csvFilesCell);
filePathsArray = [[NSFileManager defaultManager]contentsOfDirectoryAtPath:documentsDirectory error:nil];
上面的代码应该在单击删除按钮时触发,而不是在 tableview 的 data-source 方法中。之后你应该重新加载你的 tableview 和你的 cellForRowAtIndexPath 方法应该是这样的:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MainCell"];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MainCell"];
}
cell.textLabel.text = [_csvFilesCell objectAtIndex:indexPath.row];
return cell;
}
关于ios - tableView reloadData 在文档目录中删除后不重新加载数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31537309/