利用代码实现控件的添加

以前都是使用IB,今天在CC上看了使用代码添加控件,我就试试了,成功了。保存步骤和代码了。

 

1.创建一个View-Based Application,然后把里面的xib文件全部删掉。(我创建的工程为IP_codeTest)

 

2.打开IP_codeTest-info.plist文件,删除最下面的一行。Main nib file base name

 

3.打开main函数,把int retVal = UIApplicationMain(argc, argv, nil, nil)修改为:

int retVal = UIApplicationMain(argc, argv, nil, @"IP_codeTestAppDelegate");

 

4.打开IP_codeTestAppDelegate.m文件,修改成如下内容:

 window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen]bounds]];
 viewController = [[IP_codeTestViewController alloc] init];
    [window addSubview:viewController.view];
    [window makeKeyAndVisible];

 

5.打开IP_codeTestViewController.h,修改代码:

#import <UIKit/UIKit.h>

@interface IP_codeTestViewController : UIViewController
<UITableViewDelegate, UITableViewDataSource>
{
 UITableView *table;
 NSArray *array;
}
@property (nonatomic, retain) UITableView *table;
@property (nonatomic, retain) NSArray *array;
@end

 

6.打开IP_codeTestViewController.m,添加如下代码:

-(void)viewDidLoad
{
 self.view = [[[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]]autorelease];
 table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 460) style: UITableViewStylePlain];
 [table setDelegate:self];
 [table setDataSource:self];
 [self.view addSubview:table];
 array = [[NSArray alloc] initWithObjects:@"a",@"b",@"c",@"d",@"e",@"f",@"g",nil];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
 return 1;
}

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

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
 static NSString *RootViewControllerCell = @"RootViewControllerCell";
 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:RootViewControllerCell];
 if(cell == nil)
 {
  cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:RootViewControllerCell]autorelease];
 }
 cell.textLabel.text = [NSString stringWithFormat:@"<a href="mailto:%@" ,[array"="" style="text-decoration: none; color: rgb(9, 67, 130); ">%@",[array objectAtIndex:indexPath.row]];
 return cell;
}

我的实验仅仅是使用代码添加一个tableView,添加其他东西原理应该一样。以后继续摸索!!

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值