我試圖以編程方式創建UITableView
,但它讓我迷惑,因爲UITableViewDataSource
的協議的實現方法永遠不會被調用UITableViewDataSource實現的方法,不叫
的ViewController
@interface viewController : UIViewController <UITableViewDataSource>
@end
#import "viewController.h"
@interface viewController()
@property (strong, nonatomic) NSArray *tweetsArray;
@end
@implementation viewController {
UITableView *tableView;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.tweetsArray = [[NSArray alloc] initWithObjects:
@"This is the first line",
@"This is the second line",
@"This is the third line",
@"This is the fourth line",
@"This is the fifth line",
nil];
tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
tableView.dataSource = self;
[self.view addSubview:tableView];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.tweetsArray count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"SettingsCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
}
NSString *tweet = [self.tweetsArray objectAtIndex:indexPath.row];
[cell.textLabel setText:@"Via Cuong Doan"];
[cell.detailTextLabel setText:tweet];
return cell;
}
@end
的AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
viewController *view = [[viewController alloc] init];
[self.window addSubview:view.view];
return YES;
}
一切看起來正常,但-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
和-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
永遠不會叫
是ViewDidLoad被調用嗎?我相信不是 – Injectios 2014-09-04 14:49:09
'viewDidLoad'被調用,但其他兩個不是,這就是爲什麼它讓我困惑 – 2014-09-04 14:49:54
'numberOfSecion'沒有設置? – Larme 2014-09-04 14:52:31