• IOS学习之路五(代码实现UITableView)


    先展示一下运行结果:

    代码实现:

    1.先创建一个空项目:

    2.创建一个Controller:(TableViewController)

    在AppDelegate.h中声明属性:

     

    1. //  AppDelegate.h  
    2. //  UITableViewDemo  
    3. //  
    4. //  Created by WildCat on 13-8-6.  
    5. //  Copyright (c) 2013年 wildcat. All rights reserved.  
    6. //  
    7.   
    8. #import <UIKit/UIKit.h>  
    9. @class TableViewController;  
    10. @interface AppDelegate : UIResponder <UIApplicationDelegate>  
    11. @property (nonatomic,strong) TableViewController *tableViewController;  
    12. @property (strong, nonatomic) UIWindow *window;  
    13.   
    14. @property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;  
    15. @property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;  
    16. @property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;  
    17.   
    18. - (void)saveContext;  
    19. - (NSURL *)applicationDocumentsDirectory;  
    20.   
    21. @end  



    在.m文件中实现:关键代码:

     

    1. #import "AppDelegate.h"  
    2. #import "TableViewController.h"  
    3. @implementation AppDelegate  
    4. @synthesize tableViewController=_tableViewController;  
    5. @synthesize managedObjectContext = _managedObjectContext;  
    6. @synthesize managedObjectModel = _managedObjectModel;  
    7. @synthesize persistentStoreCoordinator = _persistentStoreCoordinator;  
    8.   
    9. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions  
    10. {  
    11.     self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];  
    12.     // Override point for customization after application launch.  
    13.     self.window.backgroundColor = [UIColor whiteColor];  
    14.     [self.window makeKeyAndVisible];  
    15.       
    16.     self.tableViewController=[[TableViewController alloc] initWithNibName:nil bundle:NULL];  
    17.     [self.window addSubview:self.tableViewController.view];  
    18.       
    19.       
    20.       
    21.       
    22.     return YES;  
    23. }  



    在TableViewController中声明:

     

     

    1. #import <UIKit/UIKit.h>  
    2.   
    3. @interface TableViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>  
    4. @property (nonatomic, strong) UITableView *myTableView;  
    5. @end  



    在其.m文件中实现:

     

      1. //  
      2. //  TableViewController.m  
      3. //  UITableViewDemo  
      4. //  
      5. //  Created by WildCat on 13-8-6.  
      6. //  Copyright (c) 2013年 wildcat. All rights reserved.  
      7. //  
      8.   
      9. #import "TableViewController.h"  
      10.   
      11. @interface TableViewController ()  
      12.   
      13. @end  
      14.   
      15. @implementation TableViewController  
      16. @synthesize myTableView;  
      17.   
      18. #pragma  mark - 实现UITableViewDelegate的方法  
      19.   
      20. //当cell被点击时调用该方法  
      21. - (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{  
      22.     if ([tableView isEqual:self.myTableView]){  
      23.         NSLog(@"%@",  
      24.               [NSString stringWithFormat:@"Cell %ld in Section %ld is selected",  
      25.                (long)indexPath.row, (long)indexPath.section]); }  
      26. }  
      27.   
      28.   
      29.   
      30. - (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{  
      31.     CGFloat result = 20.0f;  
      32.     if ([tableView isEqual:self.myTableView]){  
      33.         result = 40.0f;  
      34.     }  
      35.     return result;  
      36. }  
      37. #pragma mark - 实现datasource的方法  
      38. //确定section的个数  
      39. -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{  
      40.     NSInteger result=0;  
      41.     if ([tableView isEqual:self.myTableView]) {  
      42.         result=3;  
      43.     }  
      44.     return result;  
      45.   
      46.   
      47. }  
      48. //每个section的行数  
      49. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{  
      50.     NSInteger result = 0;  
      51.     if ([tableView isEqual:self.myTableView]){  
      52.         switch (section){ case 0:{  
      53.             result = 3;  
      54.             break;  
      55.         } case 1:{  
      56.             result = 5; break;  
      57.         } case 2:{  
      58.             result = 8; break;  
      59.         } }  
      60.     }  
      61.     return result;  
      62. }  
      63.   
      64. //设置每个静态的cell  
      65. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{  
      66.     UITableViewCell *result = nil;  
      67.     if ([tableView isEqual:self.myTableView]){  
      68.         static NSString *TableViewCellIdentifier = @"MyCells";  
      69.         result = [tableView dequeueReusableCellWithIdentifier:TableViewCellIdentifier];  
      70.         if (result == nil){  
      71.             result = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:TableViewCellIdentifier];  
      72.         }  
      73.         result.textLabel.text = [NSString stringWithFormat:@"Section %ld, Cell %ld", (long)indexPath.section,  
      74.                                  (long)indexPath.row];  
      75.        // result.backgroundColor=[UIColor greenColor];  //背景色  
      76.           
      77.        result.detailTextLabel.text = @"详细内容";  
      78.         if (indexPath.section==0) {  
      79.             result.accessoryType = UITableViewCellAccessoryDisclosureIndicator;  //设置右边的样式  
      80.   
      81.         }else if(indexPath.section==1){  
      82.              result.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;  //设置右边的样式  
      83.         }else{  
      84.             result.accessoryType = UITableViewCellAccessoryCheckmark;  //设置右边的样式  
      85.         }  
      86.         /* 
      87.          这两个附件的区别在于 disclosure indicator 不产生事件,而 detail disclosure 按钮在被点击时会向委托触发一个事件;也就是说,点击 cell 上的按键会 有不同效果。因此,detail disclosure 按钮允许用户在同一行上执行两个独立但相关的操 作。 
      88.           
      89.           
      90.          */  
      91.                 
      92. //        typedef enum {  
      93. //            UITableViewCellAccessoryNone,                   // don't show any accessory view  
      94. //            UITableViewCellAccessoryDisclosureIndicator,    // regular chevron. doesn't track  
      95. //            UITableViewCellAccessoryDetailDisclosureButton, // blue button w/ chevron. tracks  
      96. //            UITableViewCellAccessoryCheckmark               // checkmark. doesn't track  
      97. //        } UITableViewCellAccessoryType;  
      98.           
      99.     }  
      100.     return result;   
      101.   
      102. }  
      103. //实现DetailDisclosure被点击出发的方法  
      104. - (void) tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{  
      105.     /* Do something when the accessory button is tapped */  
      106.     NSLog(@"Accessory button is tapped for cell at index path = %@", indexPath);  
      107.     UITableViewCell *ownerCell = [tableView cellForRowAtIndexPath:indexPath];  
      108.     NSLog(@"Cell Title = %@", ownerCell.textLabel.text);  
      109. }  
      110.   
      111.   
      112. #pragma mark - TableViewController 的方法  
      113. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil  
      114. {  
      115.     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];  
      116.     if (self) {  
      117.         // Custom initialization  
      118.     }  
      119.     return self;  
      120. }  
      121.   
      122. - (void)viewDidLoad  
      123. {  
      124.     [super viewDidLoad];  
      125.     self.view.backgroundColor = [UIColor whiteColor];  
      126.     self.myTableView =  
      127.     [[UITableView alloc] initWithFrame:self.view.bounds  
      128.                                  style:UITableViewStyleGrouped];//另一种样式:UITableViewStyleGrouped  
      129.     self.myTableView.dataSource = self;  
      130.     self.myTableView.delegate = self;  
      131.     self.myTableView.autoresizingMask = UIViewAutoresizingFlexibleWidth |UIViewAutoresizingFlexibleHeight;  
      132.       
      133.     [self.view addSubview:self.myTableView];  
      134.       
      135.       
      136. }  
      137.   
      138. - (void)viewDidUnload  
      139. {  
      140.     [super viewDidUnload];  
      141.     // Release any retained subviews of the main view.  
      142.     self.myTableView = nil;  
      143. }  
      144.   
      145. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation  
      146. {  
      147.     return (interfaceOrientation == UIInterfaceOrientationPortrait);  
      148. }  
      149.   

    转载请注明:

    新浪微博:http://weibo.com/u/3202802157

  • 相关阅读:
    美丽的姑娘,你何时才能重现?
    成功三步曲:有勇、有谋、有德
    比目标远一点点,结果也许令你惊讶
    走别人的路,让别人无路可走!
    白领们注意啦:“过劳死”27个危险信号!
    企业发展靠创新,还是靠美女?
    学会和同事相处的30个原则
    上海雄联机械配件有限公司
    C# Keywords>Access Keywords
    Feature event receviers
  • 原文地址:https://www.cnblogs.com/lixingle/p/3279115.html
Copyright © 2020-2023  润新知