2011-12-05 7 views
0

私のドキュメントディレクトリに保存されたファイルのリストを使って自分のUITableViewをロードするにはどうしたらいいですか?私はちょっとした例や単純なオンラインチュートリアルが必要です。私は髪の毛を引き出しています。すべてのファイルはiOSのドキュメントディレクトリに保存されます。私が行う必要があるのは、それらを私のUITableViewにリストすることだけです。私のドキュメントディレクトリに保存されたファイルのリストを使って自分のUITableViewをロードするにはどうしたらいいですか?

答えて

3
NSArray *filePathsArray; 

- (void)setUpTheFileNamesToBeListed 
{ 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:documentsDirectory error:nil]; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return [filePathsArray count]; 
} 

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


    //Insert this line to add the file name to the list 
    cell.textLabel.text = [documentsDirectory stringByAppendingPathComponent:[filePathsArray objectAtIndex:indexPath.row]]; 
} 
+0

ありがとう!本当に役に立ちました。行をタップすると、UIWebViewでファイルを開くことができますか? – pixelbitlabs

1

あなたはこの配列は、ディレクトリとファイルが含まれています

NSArray *dirContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectory error:nil]; 

を使用して、その中にファイルのリストを取得します

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 

を使用してドキュメントディレクトリのパスを取得することができます。 dirContentsを列挙し、それはファイルが存在しない場合は

- (BOOL)fileExistsAtPath:(NSString *)path 

を使用してファイルがあるかどうかを確認、いずれかdirContentsからそれを除外するか、他に存在しないファイルのみのパスと使用して新しい配列を作成することテーブルビューのデータソースとして使用します。

+0

もう一度、本当に役に立ちました - ありがとう!行をタップすると、UIWebViewでファイルを開くことができますか? – pixelbitlabs

1

上記の2つの回答から、ドキュメントディレクトリからファイルを取得する方法がわかります。

ここで、ユーザーがte cellをタップしたときにファイルを開く方法が必要です。

あなたは今、あなたのmyURLがファイルに格納されているリンクを持っていると、あなたはあなたのWebViewでそのURLを使用することができます

 -(void)tableView(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
     { 
       //do all your file opening logic here. 
       //your file data is stored in the NSArray myFileData(refer the previous answer posted above) 
       //and suppose you want to fetch the data which is in the file, is of type NSURL, means the file data is just a URL link. 
     //just declare a NSURL in your .h(say myURL) (synthesize it etc) 


     myURL=[NSURL URLWithString:[myFileData objectAtIndex:indexPath.row]] 
     } 

のUITableViewデリゲート関数すなわちを使用する必要があります。

+0

私はそれを理解していますが、特定の行をタップしたときに特定のファイルを開くにはどうすればよいですか?あなたは小さな例を教えてもらえますか? – pixelbitlabs

+2

@watermouse私は私の答えを更新しました。それは助けます –

+0

私はそれを試してみます、ありがとう! :-) – pixelbitlabs

関連する問題