这是我的代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSArray *filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:documentsDirectory error:nil];
NSMutableArray *finalArray = [(NSArray*)filePathsArray mutableCopy];
NSFileManager *fm = [NSFileManager defaultManager];
NSArray *files = [fm contentsOfDirectoryAtPath:documentsDirectory error:nil];
NSString *fileFirst = [files objectAtIndex:0];
NSString *fullPath = [NSString stringWithFormat:@"%@/%@", documentsDirectory, fileFirst];
/* NSUInteger index = [finalArray indexOfObject:@".DS_Store"];
[finalArray removeObjectAtIndex: index]; */
//NSLog(@"files array %@", finalArray);
NSString *title = [[[finalArray objectAtIndex:indexPath.row] lastPathComponent]stringByDeletingPathExtension];
NSString *image = [[finalArray objectAtIndex:indexPath.row] lastPathComponent];
NSString *newimage = [[image pathExtension]stringByAppendingPathExtension:@"png"];
//NSLog(@"%@",newimage);
if (![UIImage imageNamed:newimage]) {
newimage = @"notFound.png";
}
NSDictionary *fileAttribs = [[NSFileManager defaultManager] attributesOfItemAtPath:fullPath error:nil];
////NSLog(@"Here: %@",fileAttribs);
//long long fileSize = [fileAttribs fileSize];
NSDate *result = [fileAttribs valueForKey:NSFileCreationDate]; //or NSFileModificationDate
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateStyle:NSDateFormatterLongStyle];
NSString *dateString = [dateFormatter stringFromDate:result];
[dateFormatter setDateStyle:NSDateFormatterNoStyle];
cell.textLabel.text = title;
cell.imageView.image = [UIImage imageNamed:newimage];
//cell.detailTextLabel.text = [NSString stringWithFormat:@"File extenstion: %@",[[image pathExtension]uppercaseString]];
cell.detailTextLabel.text = dateString;
cell.detailTextLabel.font = [UIFont systemFontOfSize:15];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
正如您在上面看到的,我在用户 Documents 文件夹中获取了文件的扩展名,并找到了具有该扩展名的图像。这有效 - 当我 NSLog 字符串时,我得到了正确的图像(例如 PDF.png)但是,如果找不到图像,我希望 newimage 变量为 notFound.png。
这适用于模拟器,但不适用于我在设备上测试时。在设备上,所有的 cell.imageView.image 都显示为 notFound.png,即使单元格 NSLogs PDF.png(例如)。有人对问题所在有任何想法吗?谢谢。