• iphone开发 有关 Navigation Bar 和 UITableView 的用法(代码加说明 呵呵)


     1 #import <UIKit/UIKit.h>
     2 
     3 @class ViewController;
     4 
     5 @interface AppDelegate : UIResponder <UIApplicationDelegate>
     6 
     7 @property (strong, nonatomic) UIWindow *window;
     8 
     9 @property (strong, nonatomic) ViewController *viewController;
    10 
    11 
    12 @property (strong,nonatomic)UINavigationController *navigation;
    13 
    14 @end

    首先 在AppDelegate.h中   加入 UINavigationController   这个 属性   ,在程序一加载就    初始话 一个UINavigation Bar  导航栏 

    那么在AppDelegate.m 实现中

     1 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
     2 {
     3     self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
     4     // Override point for customization after application launch.
     5     self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
     6     
     7     self.navigation=[[UINavigationController alloc] initWithRootViewController:self.viewController];
     8     
     9     
    10     self.window.rootViewController = self.viewController;
    11     
    12     [self.window addSubview:self.navigation.view ];
    13     
    14     
    15     
    16     [self.window makeKeyAndVisible];
    17     return YES;
    18 }

    在实现的  didFinishLaunchingWithOptions方法  中  修改 代码    

    我们用的是 单视图的模板     原来 方法中是    初始化 window 和 viewController    这些 属性  

    我们要加的就是  初始话 我们的navigation   这里我们的  RootViewController 是   viewControler  来初始化

    然后就是 把 view 和navigation  加载到  window中      顺序是 window ------view

                                                                                                       navigation  

    接下来 在视图xib文件 中     拖入view中   添加连接   

    viewConroller.h 中    要实现  tableView 中的两个委托 UITableViewDelegate    和   UITableViewDataSource    应为我们继承的是UIViewController  

    如果我们继承了UITableViewController的话    就会自动帮我们生成  委托里的方法   

     1 #import <UIKit/UIKit.h>
     2 
     3 @interface  ViewController:UIViewController<UITableViewDelegate,UITableViewDataSource>  
     4 
     5 @property ( nonatomic,retain) IBOutlet UITableView *tableView;
     6 
     7 
     8 
     9 @property ( nonatomic,retain)NSMutableArray *arr;
    10 @property(nonatomic,retain)UITextField *text;
    11 
    12 @end

    viewController.m 中

    这里需要初始话 我们的 tableView  

    委托里的方法 都是 用SELF.tableView  来调用方法的

      1 #import "ViewController.h"
      2 @implementation ViewController
      3 @synthesize tableView;
      4 @synthesize arr ;
      5 @synthesize text;
      6 
      7 #pragma mark - view lifeCycle
      8 
      9 
     10 UIBarButtonItem *edit=nil;
     11 - (void)viewDidLoad
     12 {
     13     [super viewDidLoad];
     14     // Do any additional setup after loading the view, typically from a nib.
     15     if (!tableView) {
     16         tableView=[[UITableView alloc]initWithFrame:CGRectMake(320.0, 460.0, 320.0, 460.0)];
     17     }
     18      
     19     //[self.tableView setEditing:YES animated:YES];
     20 //[self.tableView  setEditing:NO animated:YES];
     21     
     22         
     23     //self.navigationItem.rightBarButtonItem=self.editButtonItem;
     24     
     25     arr=[[NSMutableArray alloc] initWithObjects:@"Row0", @"Row1",@"Row2",@"Row3",@"Row4",nil];
     26     
     27     UIBarButtonItem *add=[[UIBarButtonItem alloc ] initWithTitle:@"Add" style:UIBarButtonItemStyleBordered target:self action:@selector(AddAction)];
     28     
     29     
     30     self.navigationItem.leftBarButtonItem=add;
     31     
     32     self.title=@"xiao表格";
     33     
     34     
     35     
     36     
     37     edit=[[UIBarButtonItem alloc] initWithTitle:@"编辑" style:UIBarButtonItemStyleBordered target:self action:@selector(editAction)];
     38     
     39     self.navigationItem.rightBarButtonItem=edit;
     40     
     41     
     42     
     43 }
     44 
     45 -(void)editAction
     46 {
     47     if (edit.title==@"编辑") {
     48         [edit setTitle:@"确定"];
     49         [edit setStyle:UIBarButtonItemStyleBordered];
     50         
     51         [self.tableView setEditing:YES animated:YES];
     52   
     53     }else {
     54         [edit setTitle:@"编辑"];
     55         [edit   setStyle:UIBarButtonItemStylePlain  ];
     56         
     57         [self.tableView setEditing:NO animated:YES];
     58     }
     59 
     60 
     61 
     62 
     63 
     64 
     65 
     66 }
     67 
     68 //复用  uialertview的方法   
     69 
     70 
     71 NSUInteger a=0;
     72 
     73 
     74 
     75 NSIndexPath * indext=nil;
     76 -(void)MyAlertView:(NSUInteger)t
     77 {
     78     UIAlertView *alert=nil;
     79     if (t==0) {
     80         alert=[[UIAlertView alloc] initWithTitle:@"添加行" message:@"" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
     81         a=1;
     82         
     83     }
     84     if (t==1) {
     85         alert=[[UIAlertView alloc] initWithTitle:@"选中行的内容" message:@"" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
     86         
     87         a=2;
     88     }
     89 
     90 
     91 
     92     text=[[UITextField alloc] initWithFrame:CGRectMake(30.0, 35.0, 220.0, 25.0)];
     93     
     94     [text setBackgroundColor:[UIColor  whiteColor]];
     95     
     96     [alert addSubview:text];
     97     
     98     
     99     
    100     
    101     [alert show];
    102 
    103 }
    104 
    105 //  添加  点击背景  关闭键盘  方法
    106 -(IBAction)backgroundTap:(id)sender
    107 {
    108     [text resignFirstResponder];
    109 
    110 
    111 }
    112 
    113 
    114 
    115 
    116 
    117 
    118 
    119 
    120 -(void)AddAction
    121 {
    122     NSLog(@"%@",@"tian  jia   an  niu  ");
    123     
    124     NSUInteger  b=0;
    125     [self MyAlertView:b];
    126     
    127 
    128 }
    129 
    130 
    131 -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex   
    132 {
    133   
    134     
    135         if (a==1) {
    136             if (buttonIndex!=[alertView cancelButtonIndex ]&&text.text!=nil) {
    137                 
    138             [arr insertObject:text.text atIndex:arr.count];
    139             
    140             //NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    141             //[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    142                      [self.tableView reloadData];
    143             }
    144             
    145             
    146                  
    147         }
    148         if (a==2) {
    149         
    150             text.text=[arr objectAtIndex:(NSUInteger)indext.row];
    151             
    152             NSLog(@"indext.row=%d",indext.row);
    153             
    154             NSLog(@"text.text=%@",text.text);
    155             
    156              if (buttonIndex!=[alertView cancelButtonIndex ]&&text.text!=nil&&indext!=nil) {
    157             
    158               [arr insertObject:text.text atIndex:indext.row];
    159                  
    160               [arr removeObjectAtIndex:(NSUInteger)indext.row ];
    161             
    162             NSLog(@"indext.row=%d", indext.row  );
    163            
    164             [self.tableView reloadData  ];
    165                  
    166                  
    167                  }
    168             
    169             
    170         }
    171     
    172 }
    173 
    174 
    175 
    176 
    177 
    178 
    179 
    180 #pragma mark - Table view data  source
    181 
    182 -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView    
    183 {
    184 
    185     return 1;
    186 
    187 }
    188 
    189 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
    190 {
    191     NSLog(@"%d",section);
    192     
    193     return  arr.count;
    194 }
    195 
    196 
    197 -(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
    198 {
    199 NSString *title=@"MyTableView";
    200         
    201     return title;
    202 }
    203 
    204 
    205 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath   
    206 {
    207 
    208  static NSString *cellInditified=@"Cell";
    209     
    210     UITableViewCell *cell=[[UITableViewCell  alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellInditified];
    211     
    212      /*
    213     if (indexPath.section==0) {
    214         cell.textLabel.text=[NSString stringWithFormat:@"Catagory1  Row%d",indexPath.row];
    215     }
    216     if (indexPath.section==1) {
    217         cell.textLabel.text=[NSString stringWithFormat:@"Catagory2  Row%d",indexPath.row];
    218     }*/
    219   
    220     
    221     
    222     cell.textLabel.text=[arr objectAtIndex:indexPath.row ];
    223     
    224     
    225     
    226     
    227     return cell;
    228 
    229 }
    230 
    231 //启用编辑
    232 
    233 -(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
    234 {
    235     
    236     return  YES;
    237 }
    238 
    239 
    240 
    241 //编辑      插入和删除
    242 -(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
    243 {
    244 
    245     if (editingStyle==UITableViewCellEditingStyleDelete) {
    246         
    247         
    248         NSLog(@"%@",@"删除");
    249         
    250         [arr removeObject:[arr objectAtIndex:(NSUInteger)indexPath.row]];
    251         
    252         
    253         
    254         [self.tableView  deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    255         
    256         [self.tableView reloadData];
    257     }else if (editingStyle==UITableViewCellEditingStyleInsert) {
    258         
    259         NSLog(@"%@",@"编辑");
    260         
    261     }
    262 
    263 
    264     
    265     
    266 
    267 }
    268 
    269 //允许用户调换行 位置
    270 -(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
    271 {
    272 
    273     return  YES;
    274 }
    275 
    276 
    277 
    278 //  调换行的位置时     让 arr中的源  元素 插入到目标元素 的位置     移除源元素 重新让tableview加载数据
    279 -(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
    280 {
    281     
    282 
    283     [arr insertObject:[arr objectAtIndex:sourceIndexPath.row] atIndex:destinationIndexPath  .row];
    284 
    285     //[arr removeObject:[arr objectAtIndex:destinationIndexPath.row]];
    286     [arr removeObjectAtIndex:(NSUInteger)destinationIndexPath.row+1];
    287 
    288     
    289     //[self.tableView reloadData];
    290 }
    291 
    292 
    293 
    294 
    295 #pragma mark - 
    296 
    297  #pragma mark   Table  view delegate
    298 
    299 
    300 
    301 
    302 //当tableview中的  行被选中后    触发事件  
    303  -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
    304 {
    305     indext=indexPath;
    306 
    307     [self.tableView deselectRowAtIndexPath:indexPath animated:YES];//选中后的反显颜色即刻消失
    308      
    309     [self MyAlertView:1];
    310     
    311     
    312   
    313 
    314 }
    315 
    316 
    317 
    318 
    319 
    320 
    321 
    322 
    323 
    324 
    325 
    326 
    327 
    328 
    329 
    330 
    331 
    332 
    333 
    334 
    335 
    336 
    337 
    338 
    339 
    340 
    341 - (void)viewDidUnload
    342 {
    343     
    344     [self setTableView:nil];
    345     [super viewDidUnload];
    346     [self setArr:nil];
    347     [self setText:nil];
    348     // Release any retained subviews of the main view.
    349 }
    350 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    351 {
    352     return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
    353 }
    354 
    355 @end

                          

  • 相关阅读:
    15--k8s之安全认证
    14--k8s之StorageClass,ConfigMap,Secret
    13--基本存储、高级存储、存储配置
    10--k8s之数据持久化
    9--k8s之Endpoints、健康服务检查、高可用
    8--k8s之service和ingress详解
    7--k8s之Pod控制器详解
    6--k8s之Pod结构、配置、生命周期、调度
    索引原理和慢查询优化
    pymysql模块及sql注入
  • 原文地址:https://www.cnblogs.com/DamonTang/p/2567403.html
Copyright © 2020-2023  润新知