iOS 第14课 TableView <一> 创建以及数据源方法

来源:互联网 发布:软件测试英文工作报告 编辑:程序博客网 时间:2024/06/05 20:31


0:首先还是通过纯的代码来实现

0:删除3个文件ViewController.hViewController.mMain.storyboard

1:修改点击左边的蓝色按钮,然后选择general-》developer info-》main interface ,将这个main interface 晴空

2:然后再创建一个MainUIViewController ,它继承自UIViewController

1:Appdelegate.m

////  AppDelegate.m//  FourteenTableView////  Created by 章银珊 on 16/10/29.//  Copyright © 2016年 Kodulf. All rights reserved.//#import "AppDelegate.h"#import "MainViewController.h"@interface AppDelegate ()@end@implementation AppDelegate- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    // Override point for customization after application launch.    self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];    [self.window makeKeyAndVisible];    [self.window setRootViewController:[[MainViewController alloc]init] ];    return YES;}- (void)applicationWillResignActive:(UIApplication *)application {    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.}- (void)applicationDidEnterBackground:(UIApplication *)application {    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.}- (void)applicationWillEnterForeground:(UIApplication *)application {    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.}- (void)applicationDidBecomeActive:(UIApplication *)application {    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.}- (void)applicationWillTerminate:(UIApplication *)application {    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.}@end

2: MainViewController 创建

////  MainViewController.m//  FourteenTableView////  Created by 章银珊 on 16/10/29.//  Copyright © 2016年 Kodulf. All rights reserved.//#import "MainViewController.h"@interface MainViewController () <UITableViewDataSource,UITableViewDelegate>@end@implementation MainViewController- (void)viewDidLoad {    [super viewDidLoad];    [self.view setBackgroundColor:[UIColor grayColor]];    UITableView *tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];//先运行的时候使用UITableViewStyleGrouped 这个style 可以看到如果是5个view的时候,可以明显看到效果,但是如果是UITableViewStylePlain的话,是看到的很多的view的就是铺满了屏幕的view的那个5设置是没有用的,需要深入的去调查    //调查结果是这样的,返回一共多少组是numberOfSectionsInTableView:,返回每一组多少个元素的是numberOfRowsInSection:        //UITableViewStylePlain 和 UITableViewStyleGrouped的区别    //首先是header 部分,plain是没有考虑顶部的,就是wifi信号那一栏的,    //然后plain 有一个顶部滞留的效果的,group是没有的,这是它两最大的区别        tableView.dataSource = self;    tableView.delegate = self;    [self.view addSubview:tableView];    //这里可以不用CGRectMake(0, 0, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame))而直接使用self.view.bounds            // Do any additional setup after loading the view.}//datasource 是数据源的设置,delegate 主要是用来设置view的样式的,//必须要实现的方法,可以点击上面<UITableViewDataSource>就可以看到了#pragma mark - TableView Initilize update udpate-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{    return 9;}//类似于android的adapter 里面的getview-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{    return 5;}//设置头部的名字-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{    return @"header";}//-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{//    UILabel *view = [[UILabel alloc]init];//    view.text = @"Header";//    return view;//}-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    UITableViewCell *cell = [[UITableViewCell alloc]init];    cell.textLabel.text=@"text";    cell.imageView.image =[UIImage imageNamed:@"a"];//图片下载地址:http://download.easyicon.net/png/3887/48/ 拷贝进来后,放到assets.xcassets 里面,然后放在2x里面,        UITextField *uiTextField = [[UITextField alloc]initWithFrame:CGRectMake(100, 2, 100, 40)];    uiTextField.borderStyle = UITextBorderStyleRoundedRect;//指定一下border的样式    [cell addSubview:uiTextField];        return cell;}- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}/*#pragma mark - Navigation// In a storyboard-based application, you will often want to do a little preparation before navigation- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {    // Get the new view controller using [segue destinationViewController].    // Pass the selected object to the new view controller.}*/@end



0 0
原创粉丝点击