0

有关此问题的解决方案,请参阅本文底部。

单元格确实在初始视图上加载对象,自定义单元格包含一个 UIImage 视图和两个 UILabel。但是,在选择了一个单元格并推送了一批新的单元格后,该单元格上的对象不会出现。它们仅显示为空白单元格。

- (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];
            }}
    }
}

截图:

初始视图,加载文件浏览器选项卡时

选择文件夹单元格后

解决方案:

好的,所以问题似乎是我正在分配当前表视图的一个新实例并将其推送到被按下的单元格之一上。为了解决标签没有出现在第二个视图中的问题,我将原型单元格更改为 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;
}

祝你好运。

4

1 回答 1

0

在没有看到更多代码的情况下很难回答。例如,DirectoryBrowserTableViewController 的外观如何?我猜 ViewController 是您选择文件夹时显示的那个?

在不了解上下文的情况下,我只能猜测,但我会开始查看您对 numberOfRowsInSection 的实现(在 DirectoryBrowserTableViewController 中)。它是否返回您目录中当前的文件数量?如果它返回零,我猜你最终会得到一个空的 tableview。

于 2013-10-17T06:44:38.563 回答