我有一个问题,我似乎无法重新加载tableView中的数据。
我想做的是用具有csv扩展名的documents目录中的文件填充我的tableView(已完成),然后按一个按钮,该按钮删除所有显示的文件并更新tableView。
按下该按钮会删除文档目录中的文件,但不会使用更新的内容重新加载tableView。
使用NSLog的reloadData确认数组为空,但这
不更新表。
运行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的数据源方法中触发。之后,您应该重新加载表格视图,并且 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/