2013-10-17 46 views
0

有關此問題的解決方案,請參閱本文的底部。原型自定義單元類在推後不顯示對象

單元格會在初始視圖中加載對象,自定義單元格包含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; 
} 

好運。

+0

顯示uitableview的其餘代碼 –

+0

你是什麼意思**單元格在初始視圖上加載對象?** ... –

+0

請參閱編輯過的文章,我已添加屏幕截圖和其餘代碼。 – Terry

回答

0

很難回答,但沒有看到更多的代碼。例如,DirectoryBrowserTableViewController如何看起來像?我猜那個ViewController是你選擇文件夾時顯示的那個?

不知道更多關於上下文我只能猜測,但我會開始看着你的實現numberOfRowsInSection(在DirectoryBrowserTableViewController)。它是否會返回目錄中的文件數量?如果它返回零,我想你最終會得到一個空的tableview。

+0

原始問題中有兩個截圖超鏈接,很遺憾我無法附上由於我的聲望低於閾值,「標準」格式的圖像。 就我所知,無論如何,細胞都存在。我可以點擊一個單元格並調出各自的活動菜單,到目前爲止,它似乎只是一個與外觀有關的問題。 – Terry

+0

您是從DirectoryBrowserTableViewController還是從其他類顯示的代碼? – Daniel

+0

是的,它來自DirectoryBrowserTableViewController,所有提供的代碼都來自該類。 – Terry

相關問題