有關此問題的解決方案,請參閱本文的底部。原型自定義單元類在推後不顯示對象
單元格會在初始視圖中加載對象,自定義單元格包含UIImage視圖和兩個UILabels。但是,在選中單元格並推入新的單元格之後,單元格上的對象不會出現。它們只是表現爲空白單元格。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"FileCell";
FileCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[FileCell alloc] initWithReuseIdentifier:CellIdentifier];
}
NSString *file = [self.files objectAtIndex:indexPath.row];
NSString *path = [self pathForFile:file];
BOOL isdir = [self fileIsDirectory:file];
[[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:&isdir];
cell.lblFileName.hidden = false;
if (isdir == true){
// directory
cell.lblFileSize.hidden = true;
CGRect frame = cell.lblFileName.frame;
frame.origin.y = cell.frame.size.height/2 - cell.lblFileName.frame.size.height /2;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.imgIcon.image = [UIImage imageNamed:@"FBFolderIcon"];
}else{
// file
NSFileManager *man = [NSFileManager defaultManager];
NSDictionary *attrs = [man attributesOfItemAtPath: path error: NULL];
float result = [attrs fileSize];
cell.lblFileSize.text = [self stringFromFileSize:result];
cell.accessoryType = UITableViewCellAccessoryNone;
NSString *cellExt = [[file pathExtension] uppercaseString];
UIImage *tempImage = [UIImage imageNamed:[NSString stringWithFormat:@"FB-%@", cellExt]];
if (!tempImage) {
tempImage = [UIImage imageNamed:[NSString stringWithFormat:@"FB-%@", @"UNKNOWNTYPE"]];
}
cell.imgIcon.image = tempImage;
}
return cell;
}
更新:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.files count];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *file = [self.files objectAtIndex:indexPath.row];
NSString *path = [self pathForFile:file];
if ([self fileIsDirectory:file]) {
DirectoryBrowserTableViewController *dbtvc = [[DirectoryBrowserTableViewController alloc] init];
dbtvc.path = path;
[self.navigationController pushViewController:dbtvc animated:YES];
} else {
if(path == nil){
return;
}
else{
NSURL *URL = [NSURL fileURLWithPath:path];
if (URL)
{
self.documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:URL];
self.documentInteractionController.delegate = self;
[self.documentInteractionController presentOptionsMenuFromRect:CGRectZero inView:self.tableView animated:YES];
}}
}
}
截圖:
Initial view, when file browser tab is loaded
After selecting the folder cell
解決方案:
好的,所以問題似乎是我分配當前表視圖的一個新實例,並推動它的一個單元格被按下。爲了解決標籤沒有出現在我的第二個視圖中的問題,我將其從原型單元格更改爲NIB文件形式的UITableCell。
只記得補充一點:
- (void)viewDidLoad
{
[super viewDidLoad];
[self.tableView registerNib:[UINib nibWithNibName:@"FileManagerCell" bundle:nil]
forCellReuseIdentifier:@"FileManagerCell"];
}
而在表視圖:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//The name of the cell that we registered in the viewDidLoad method.
static NSString *CellIdentifier = @"FileManagerCell";
//FileCell is the name of the class that I have associated with the NIB file.
FileCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[FileCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
return cell;
}
好運。
顯示uitableview的其餘代碼 –
你是什麼意思**單元格在初始視圖上加載對象?** ... –
請參閱編輯過的文章,我已添加屏幕截圖和其餘代碼。 – Terry