仅当文件存在时显示 cell.button

Show a cell.button only if file exists

我有一个 table 视图,其中一些单元格由 2 个标签填充。
如果存在名为 "label1-label2-name" 的 mp3 文件,我将播放该文件。

 NSArray *final;
 NSString *element;
 final = [NSArray arrayWithObjects: @"a", @"b", @"c", @"d", nil];

现在,在我的 cellForRowAtIndexPath 中,我正在尝试做同样的事情,但只是为了在文件存在时显示播放按钮(按钮最初是隐藏的)。

- (CustomCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

 for (element in final) {

    buttonA = [NSString stringWithFormat:@"%@-%@-AAA-%@", current[indexPath.row][0],current[indexPath.row][1], element];
    fileA = [[NSBundle mainBundle] pathForResource:buttonA ofType:@"mp3"];
    BOOL fileExistsA = [[NSFileManager defaultManager] fileExistsAtPath:fileA];

    if (fileExistsA) {

        cell.playA.hidden = false;

    }
 }
}

这里发生的事情是,即使名为 "label1-label-2-AAA-a" 的文件存在,如果文件 "label1-label-2-AAA-d" 不存在,播放按钮将被隐藏。

如何show/hide特定单元格播放?

从您共享的代码片段中不清楚您是否正在重复使用单元格,但假设您是重复使用单元格,您可能希望在这两种情况下更新 cell.playA.hidden 状态(例如,如果文件存在或不存在),否则一旦屏幕上出现具有现有 mp3 文件的单元格,您将看不到 "play" 按钮,并且在配置它时,您将具有先前隐藏按钮的单元格出队。

- (CustomCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    /// Get the cell

    cell.playA.hidden = true;
    for (element in final) {
        buttonA = [NSString stringWithFormat:@"%@-%@-AAA-%@", current[indexPath.row][0],current[indexPath.row][1], element];
        fileA = [[NSBundle mainBundle] pathForResource:buttonA ofType:@"mp3"];
        BOOL fileExistsA = [[NSFileManager defaultManager] fileExistsAtPath:fileA];
        if (fileExistsA) {
            cell.playA.hidden = false;
            break;
        }
    }
}

假设这是你想要的,我们检查这个单元格的所有可能的文件名 ("label1-label-2-AAA-a", "label1-label-2-AAA-b", ...),如果 至少有一个 存在,我们显示该按钮,否则我们隐藏它。