【发布时间】:2015-10-10 19:18:04
【问题描述】:
我有一个问题,我似乎无法在我的 tableView 中重新加载数据。
我想要做的是用文件目录中的文件填充我的 tableView,文件目录中带有 csv 扩展名(我已经这样做了),然后按下一个按钮,删除所有显示的文件并更新 tableView。 按下该按钮会删除文档目录中的文件,但不会使用更新的内容重新加载 tableView。
- 我已经使用断点来确认 reloadData 正在运行。
- 我尝试在运行前使用 removeAllObjects 清除我的数组 reloadData 使用 NSLog 来确认数组是空的,但是这个 不更新表格。
- 我尝试在运行 reloadData 之前将数组设置为 nil,但这也不起作用。
- 我已尝试将数组中的数据设置为 myArray = @[];前 运行 reloadData,这会用空白数据覆盖数组,但 不更新表格。
-
我尝试过使用这种方法 任何成功:
dispatch_sync(dispatch_get_main_queue(), ^{ [self.tableView reloadData]; }); - 我已经检查了我的代表的 tableView 并且他们出现了 正确,(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];
}
【问题讨论】:
-
它不会解决您的问题,但您可以将调用移至 for 循环之外的
reloadData和dismissViewControllerAnimated。它效率低下,可能导致未定义的行为。
标签: ios objective-c uitableview reloaddata