I currently have a UITableView showing my app’s document directory. How can I get the file size of the current row’s document?
My code is currently as follows:
- (UITableViewCell *)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"cellID";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
// layout the cell
cell.textLabel.text = [self.drive.filesArray objectAtIndex:indexPath.row];
NSInteger iconCount = [docInteractionController.icons count];
cell.imageView.image = [docInteractionController.icons objectAtIndex:iconCount - 1];
NSString *fileURLString = [self.drive.filesArray objectAtIndex:indexPath.row];
NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:fileURLString error:nil];
NSString *fileSizeNumber = [fileAttributes objectForKey:NSFileSize];
long long fileSize = [fileSizeNumber longLongValue];
cell.detailTextLabel.text = [NSString stringWithFormat:@"%@ - %@",
fileSizeNumber, [self.drive.filesArray objectAtIndex:indexPath.row]];
return cell;
}
Unfortunately this doesn’t work though. Any ideas?
I don’t know what self.drive.filesArray is, but if you do it with NSDocumentDirectory, get path and then files in that path, it will work.
Here is sample which work for me and I think this is the way how to deal with document directory for application on iOS:
You will end up with something similar like
I think you still have wrong path, mine (on iPhone with iOS 5.0.1) is
point here is Documents. Correct your code to get right path, save files to right path and you should be able to get attributes from files. Your file probably doesn’t exist or fm just doesn’t have rights to get what you want. I don’t know, because you send errors from file manager to nil object. Also question, how are you collecting self.drive.filesArray array?
Try to collect your self.drive.filesArray that way, I wrote above. It works.