如何在uiviewcontroller中使用uitableview?下面是我想要做的(除了這僅僅是UITableview
在我的故事板)的例子:UIViewController中的UITableView
我已經想通了,我需要委託和數據源添加到我的標題:
//MyViewController.h
@interface MyViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
我在執行文件,我已經添加了必需的方法:
//MyViewController.m
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"cellForRowAtIndexPath");
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FileCell"];
NSLog(@"cellForRowAtIndexPath");
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSArray *fileListAct = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectory error:nil];
cell.textLabel.text = [NSString stringWithFormat:@"%@",[fileListAct objectAtIndex:indexPath.row]];
return cell;
}
的delegate
,datasource
,並UITableView
都迷上了我的故事板:
我不能得到的TableView加載,我告訴它的內容。它總是空白。爲什麼TableView不會填充我在cellForRowAtIndexPath方法中告訴它的內容?我在這裏錯過了什麼?
我不再獲得編譯錯誤,但是當我試圖與該表進行交互的應用程序崩潰。 cellForRowAtIndex方法是填充表視圖的正確位置嗎? – 2012-04-29 23:23:37
崩潰時的錯誤信息是什麼?在Objective-C異常中設置一個斷點,以便您可以找出它正在崩潰的線路。 – jsd 2012-04-30 16:10:01
我連接dataSource和委託插座到我的ViewController(正確),我不再會得到構建錯誤或崩潰。當我在cellForRowAtIndexPath和numberOfRowsInSection中設置斷點時,會發現它們,但它們的內容永遠不會執行。此外,它甚至沒有進入cellForRowAtIndexPath,這個方法被忽略。這是爲什麼發生? – 2012-04-30 19:37:41