gpt4 book ai didi

ios - 如何根据 TableView 中重新加载的数据启用/禁用 UIBarButton?

转载 作者:行者123 更新时间:2023-11-29 02:24:47 25 4
gpt4 key购买 nike

我正在开发文件管理器应用程序,我在 TableView 中列出所有目录。根据用户选择的内容进行选择,我正在重新加载表格 View 的内容。 (例如,当用户点击音乐目录时,将显示目录列表,我将加载音乐目录中的所有目录。

现在我真正需要做的是我在顶部有后退按钮但是我希望当用户不在根目录中时启用它。我应该在哪里放置我的代码来执行此操作?

编辑:

我的代码如下

#import "ViewController.h"
#import "AddDirectory.h"
@interface ViewController ()
@property NSArray *fileList;
@property NSFileManager *manager;
@property (weak, nonatomic) IBOutlet UITableView *tableView1;
@property (weak, nonatomic) IBOutlet UIBarButtonItem *bkbutton;
@property NSMutableString *currentDir;
@property NSString *rootpath;
@end

@implementation ViewController
- (void)viewWillAppear:(BOOL)animated{

[self.tableView1 reloadData];
}
- (void)viewDidLoad {
[super viewDidLoad];
self.manager=[NSFileManager defaultManager];
NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory=[paths objectAtIndex:0];
self.fileList=[self.manager contentsOfDirectoryAtPath:documentDirectory error:nil];
self.currentDir=[[NSMutableString alloc] init];
[self.currentDir appendString:documentDirectory];
self.rootpath= self.currentDir;
//NSLog(@"%@",self.fileList);
// self.fileList=contents;

// Do any additional setup after loading the view, typically from a nib.
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
- (IBAction)backDirectory:(id)sender {
self.currentDir = [NSMutableString stringWithFormat:@"%@", [self.currentDir stringByDeletingLastPathComponent]];
self.fileList = [self.manager contentsOfDirectoryAtPath:self.currentDir error:nil];
[self.tableView1 reloadData];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
// NSLog(@"%i",[self.fileList count]);
return [self.fileList count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell1"];
NSString *cfname=[self.fileList objectAtIndex:[indexPath row]];
// NSString *fname=[[cfname lastPathComponent] stringByDeletingPathExtension];
NSString *fname=[cfname lastPathComponent];
// NSLog(@"%@",fname);
cell.textLabel.text=fname;

return cell;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if([segue.identifier isEqualToString:@"adddir"]){
AddDirectory *ad=segue.destinationViewController;
ad.cpath=self.currentDir;

}
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

if(self.currentDir!=nil){
[self.currentDir appendString:[NSString stringWithFormat:@"/%@",[self.fileList objectAtIndex:indexPath.row]]];
self.fileList=[self.manager contentsOfDirectoryAtPath:self.currentDir error:nil];
NSLog(@"%@",self.currentDir);
[tableView reloadData];

}
if([self.currentDir isEqualToString:self.rootpath]){
self.navigationItem.leftBarButtonItem.enabled = NO;
}
}

最佳答案

这就是您问题的解决方案。假设您有一个带有可见导航 Controller 的 View Controller ,并且您位于根目录中。在您的类中创建一个 iVar 作为 BOOL isRoot。最初将其设置为 True。

@property(nonatomic) BOOL isRoot;

现在编写自定义 setter 。

-(void)setIsRoot:(BOOL)isRoot{
_isRoot = isRoot;
self.navigationItem.hidesBackButton = !isRoot;
}

现在,根据您深入子目录或进入层次结构的逻辑,只需更新此 isRoot 值即可。

您还可以按照 NSNotificationCenter 方法发送通知,以便在每次玩具重新加载您的 tableVIew 时调用此方法。

希望对您有所帮助。

关于ios - 如何根据 TableView 中重新加载的数据启用/禁用 UIBarButton?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27686492/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com