2012-08-17 37 views
2

我想我的Dropbox文件加載到一個UITableView,但他們都沒有顯示出來。我完成了在dropbox.com上註冊我的應用程序並在我的應用程序中實施會話委託的每一步。這裏是我的代碼,任何人都可以告訴我它有什麼問題。林相當肯定,我宣佈一切正常,但我似乎無法找到問題。另外我知道它不是連接問題,因爲我添加了NSLog並記錄了這些文件。添加的Dropbox文件要UITableView的

#import <UIKit/UIKit.h> 
#import <DropboxSDK/DropboxSDK.h> 

@interface testViewController : UITableViewController <DBRestClientDelegate> 
{ 
    DBRestClient *restClient; 
    NSMutableArray *dropboxURLs; 
} 
@end 

#import "testViewController.h" 

@interface testViewController() 

@end 




@implementation testViewController 

- (id)initWithStyle:(UITableViewStyle)style 
{ 
    self = [super initWithStyle:style]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 

- (DBRestClient *)restClient { 
    if (!restClient) { 
     restClient = 
     [[DBRestClient alloc] initWithSession:[DBSession sharedSession]]; 
     restClient.delegate = self; 
    } 
    return restClient; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    dropboxURLs = [[NSMutableArray alloc] init]; 
    [[self restClient] loadMetadata:@"/"]; 
} 

- (void)restClient:(DBRestClient *)client loadedMetadata:(DBMetadata *)metadata { 
    if (metadata.isDirectory) { 
     for (DBMetadata *file in metadata.contents) { 
      if (!file.isDirectory) 
      { 
       NSLog(@"%@", file.filename); 
       [dropboxURLs addObject:file.filename]; 
      } 
     } 
    } 
} 

- (void)restClient:(DBRestClient *)client 
loadMetadataFailedWithError:(NSError *)error { 

    NSLog(@"Error loading metadata: %@", error); 
} 


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

#pragma mark - Table view data source 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    // Return the number of sections. 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    // Return the number of rows in the section. 
    return dropboxURLs.count; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"FileCell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if(cell == nil) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 

    cell.textLabel.text = [dropboxURLs objectAtIndex:indexPath.row]; 

    return cell; 
} 

@end 
+0

你說你記錄的文件 - 你在哪裏呢?您是否記錄了dropBoxURLs以查看它是否有任何內容?從你的錯誤消息看來,你的數組是空的。 – rdelmar 2012-08-19 15:37:58

+0

@rdelmar您好我登錄之前,我將他們添加到在陣列中的文件:的NSLog(@「%@」,file.filename); [dropboxURLs addObject:file.filename]; – Cristian 2012-08-22 01:21:14

+0

嘗試登錄dropboxURLs.count在numberOfRowsInSection方法,看看它返回 – rdelmar 2012-08-22 01:38:33

回答

0

將內容添加到dropboxURLs數組後,重新載入表視圖。

[self.tableView reloadData]; 
+0

嗨當​​我補充說,我得到:2012年8月17日11:55:46.938分享我的[5987:907] ***終止應用程序由於未捕獲的異常'NSRangeException',原因: '*** - [__ NSArrayM objectAtIndex:]:索引0超出界限爲空數組' ***第一擲調用堆棧: (0x3acfe6c3 0x34d7e97f 0x3ac4a055 0x60065 0x39d4ab81 0x39d2f93b 0x39d46e17 0x39d02f2b 0x39bdde9b 0x39bdda39 0x39bde975 0x39bde353 0x39bde161 0x39bddfc1 0x3acd3aed 0x3acd1de1 0x3acd2137 0x3ac4539d 0x3ac45229 0x3780231b 0x39d538f9 0x59ce9 0x352bab20) libC++ abi.dylib:terminate調用拋出異常 – Cristian 2012-08-17 15:56:47

0

嘗試這樣的事情你ADDOBJECT調用後立即:

[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationFade]; 
+0

嗨,當我添加這個我得到:2012-08-17 11:55:46.938分享我[5987:907] ***終止應用程序,由於未捕獲的異常'NSRangeException',原因:'*** - [__ NSArrayM obj ectAtIndex:]:索引0超出界限爲空數組」 ***第一擲調用堆棧: (0x3acfe6c3 0x34d7e97f 0x3ac4a055 0x60065 0x39d4ab81 0x39d2f93b 0x39d46e17 0x39d02f2b 0x39bdde9b 0x39bdda39 0x39bde975 0x39bde353 0x39bde161 0x39bddfc1 0x3acd3aed 0x3acd1de1 0x3acd2137 0x3ac4539d 0x3ac45229 0x3780231b 0x39d538f9 0x59ce9 0x352bab20) 的libc + + abi.dylib:terminate調用拋出異常 – Cristian 2012-08-17 15:57:25

0
-(void)restClient:(DBRestClient *)client loadedMetadata:(DBMetadata *)metadata { 
    if (metadata.isDirectory) { 
     for (DBMetadata *file in metadata.contents) { 
      if (!file.isDirectory) 
      { 
       NSLog(@"%@", file.filename); 
       [dropboxURLs addObject:file.filename]; 
      } 
     } 
    } 
    [self. tableView reloadData]; 
    } 

不要忘了的UITableView的委託和數據源設置爲自我。

0
  • 我們必須
  • 元數據之後被加載從DropBoxRoot
  • 加載數據,重新加載的tableview
  • (無效)viewDidLoad中{
    [超級viewDidLoad中];
    dropboxURLs = [[NSMutableArray裏的alloc] INIT];
    [self loadData];
    }
-(void) loadData{ 
if ([DBSession sharedSession].root == kDBRootDropbox) { 
    photosRoot = @"/";//can specify any folder like /Photos,/Public etc 
} 
[self.restClient loadMetadata:photosRoot withHash:photosHash]; 
} 


- (void)restClient:(DBRestClient*)client loadedMetadata:(DBMetadata*)metadata { 

photosHash = metadata.hash; 

NSArray* validExtensions = [NSArray arrayWithObjects:@"jpg", @"jpeg",@"png",@"txt",@"pdf",@"doc",@"mp3",@"mp4", nil]; 
for (DBMetadata* child in metadata.contents) { 

    NSString* extension = [[child.path pathExtension] lowercaseString]; 

    if (!child.isDirectory && [validExtensions indexOfObject:extension] != NSNotFound) { 
     [directory addObject:child.filename]; 
    }else{ 
     [allFileAndDirectory addObject:child.filename]; 
    } 
    [myTableView reloadData]; 
}