我正在開發文件管理器應用程序,我在列表視圖中列出所有目錄。根據用戶選擇的內容進行選擇我正在重新加載表視圖的內容。 (例如,用戶將顯示目錄列表當他點擊可以說音樂目錄我會加載所有目錄在音樂目錄如何基於表視圖中重新加載的數據啓用/禁用UIBarButton?
現在我真的需要做的是我有返回按鈕上但我希望它當用戶不在根目錄中啓用我應該在哪裏把我的代碼做
編輯:
我的代碼下
#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;
}
}
最好的方法可以讓你將與子目錄的路徑推動同一類的其他的viewController(使用一個UINavigationController)。後退按鈕將自動被觸發。 – ZeMoon
在您的cellForRowAtIndexPath委託方法中檢查它是否是根目錄並相應地執行操作。 –
Dheeraj我已經試過,沒有運氣:( –