2015-07-09 98 views
0

我似乎無法找到可用於將單元格添加到xcode中的表格而沒有Storyboard(如果可能的話)的方法/代碼行。這似乎是一個非常快速的解決方案,我只是沒有找到它。 如果你知道這個例子,請指出。以編程方式向UITableViewController添加表格單元格

#import "AppDelegate.h" 
#import "SubViewController.h" 
#import "SubTableViewController.h" 
@implementation AppDelegate 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
    // Override point for customization after application launch. 
    self.window.backgroundColor = [UIColor whiteColor]; 

    //subviewcontroller = tabbarcontroller 

    UITabBarController * tabBarController = [[UITabBarController alloc] init]; 

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
    SubViewController * firstTab= [[SubViewController alloc] init ]; 
    [firstTab.view setBackgroundColor:[UIColor greenColor]]; 
    firstTab.title = @"first"; 

    UINavigationController *navigationController1 = [[UINavigationController alloc] initWithRootViewController:firstTab]; 

    SubViewController *secondTab = [[SubViewController alloc] init]; 
    [secondTab.view setBackgroundColor:[UIColor redColor]]; 
    secondTab.title = @"second"; 
    UINavigationController *navigationController2 = [[UINavigationController alloc] initWithRootViewController:secondTab]; 

    SubTableViewController * table = [[SubTableViewController alloc] init]; 
    UINavigationController * nav3 = [[UINavigationController alloc]initWithRootViewController:table]; 

    table.title = @"third"; 

    tabBarController.viewControllers = @[navigationController1,navigationController2, nav3]; 

    [self.window setRootViewController:tabBarController]; 

    [self.window makeKeyAndVisible]; 
    return YES; 
} 
+2

的代碼放在您的自定義表v在控制器類中實現各種'UITableViewDataSource'方法。 – rmaddy

回答

0

如果您使用的是Xcode中提供tableViewController模板有

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 

- 在每一節

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 

中的行數的tableView節你在哪裏n各自細胞

I'ld而是建議你應該看看一些基本教程appcode tutorial

0

請嘗試以下的

代碼編寫的設置文本

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

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"]; 

if (cell == nil) { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"]; 
} 
cell.textLabel.text = @"Test"; 
[cell.imageView setImage:[UIImage imageNamed:@"test.png"]]; 
cell.textLabel.font = [UIFont systemFontOfSize:16.0]; 
return cell; 

而且你可以添加自定義視圖上面的代碼

相關問題