zoukankan      html  css  js  c++  java
  • UITableView的简单用法

    #import "AppDelegate.h"

    @interface AppDelegate ()

    @end

    @implementation AppDelegate

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

        //初始化一个ViewController的一个对象

        ViewController *view = [[ViewController alloc] init];

        //把导航栏添加到上面

        UINavigationController *navc = [[UINavigationController alloc] initWithRootViewController:view];

       // self.window.rootViewController = navc;

        //初始化第二个页面

       SecondViewController *secondVc = [[SecondViewController alloc] init];

        //

        secondVc.tabBarItem.title = @"second";

        secondVc.tabBarItem.badgeValue = @"2";

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

        tabbarcon.tabBar.tintColor = [UIColor greenColor];

        [tabbarcon setViewControllers:@[navc,secondVc]];

        

        self.window.rootViewController =tabbarcon;

        

        

        return YES;

    }

    #import <UIKit/UIKit.h>

    #import "SecondViewController.h"

    @interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>

    @property (nonatomic ,strong) UITableView *tableV;

    @property (nonatomic ,strong) NSMutableArray *arry;

    @end

    #import "ViewController.h"

    @interface ViewController ()

    @end

    @implementation ViewController

    - (void)viewDidLoad {

        [super viewDidLoad];

       //设置导航栏的biaot

        self.title = @"first";

       //设置触发跳到下一页的按钮

        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"arrow_normal"] style:UIBarButtonItemStylePlain target:self action:@selector(tableView:didSelectRowAtIndexPath:)];

        //初始化可变数组

        self.arry = [NSMutableArray array];

        //循环给数组赋值

        for (int i = 0; i< 100; i++) {

            [self.arry addObject:[NSString stringWithFormat:@"%i%i",i,i]];

        }

        //初始化tableV的大小及风格

        self.tableV = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];

        //指定self.tableV.delegate代理为self

        self.tableV.delegate = self;

        //给分隔栏设置颜色

        self.tableV.separatorColor = [UIColor blueColor];

        // //指定self.tableV.dataSource代理为self

        self.tableV.dataSource = self;

        //把tableV加载到view上

        [self.view addSubview:self.tableV];

        //指定单元格的唯一标识

        [self.tableV registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];

       

        

    }

    //定义一个全局变量来接收索引indexPath.row

        int num;

    - (void)didReceiveMemoryWarning {

        [super didReceiveMemoryWarning];

        // Dispose of any resources that can be recreated.

    }

    //设置每个分区显示的行数

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

        return self.arry.count;

    }

    //每个单元格显示的内容

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

        static NSString *cellIdentity = @"cell";

        //单元格重用机制

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentity forIndexPath:indexPath];

        cell.textLabel.text = self.arry[indexPath.row];

        cell.textLabel.textAlignment = NSTextAlignmentCenter;

        //给cell添加背景色

        cell.backgroundColor = [UIColor yellowColor];

        return cell;

    }

    // 代理方法,显示选中单元格的信息

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

        //初始化一个SecondViewController的对象

        SecondViewController *second = [[SecondViewController alloc] init];

       //把self.arry[indexPath.row]的值赋值给second.str

        second.str = self.arry[indexPath.row];

        //用num接收相应的索引

        num = (int)indexPath.row;

        //定义一个字符串str接收self.arry[indexPath.row]的值

        NSString *str =self.arry[indexPath.row];

        //初始化一个提示栏UIAlertController的对象并把他赋值

        UIAlertController *alertC= [UIAlertController alertControllerWithTitle:@"你确定要点击它" message:str preferredStyle: UIAlertControllerStyleAlert];

        //定义两个 UIAlertAction ,*action2取消

        UIAlertAction  *action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil];

        //*action1确定

        UIAlertAction  *action1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

            //点击确定进入下一页

        [self.navigationController pushViewController:second animated:YES];

        }];

         //把action1,action2添加到alertC上

        [alertC addAction:action1];

        [alertC addAction:action2];

        //显示alertC

        [self presentViewController:alertC animated:YES completion:nil];

       

    }

    //在窗体未加载之前添加监听

    -(void)viewWillAppear:(BOOL)animated{

        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(myPostValue:) name:@"lamco" object:nil];

    }

    //添加观察者

    -(void)myPostValue:(NSNotification *)noti{

        

        [self.arry replaceObjectAtIndex:num withObject:noti.object];

        [self.tableV reloadData];

    }

    //移除观察者

    -(void)dealloc{

        [[NSNotificationCenter defaultCenter] removeObserver:self name:@"lamco" object:nil];

        NSLog(@"销毁通知,移除观察者");

        

    }

    @end

    #import <UIKit/UIKit.h>

    @protocol postValuedelegate <NSObject>

    -(void)postValue:(NSString *)Value;

    @end

    @interface SecondViewController : UIViewController<UITextFieldDelegate>

    @property (nonatomic ,strong) NSString *str;

    @property (nonatomic ,strong) UITextField *textfld;

    @property (nonatomic ,assign) id<postValuedelegate> delegate;

    @end

    #import "SecondViewController.h"

    @interface SecondViewController ()

    @end

    @implementation SecondViewController

    - (void)viewDidLoad {

        [super viewDidLoad];

        //添加背景图片

        UIImageView *imageV= [[UIImageView alloc] initWithFrame:self.view.frame ];

        imageV.image = [UIImage imageNamed:@"82r58PICYeM_1024.jpg"];

        self.view.backgroundColor = [UIColor colorWithPatternImage:imageV.image];

       // 初始换文本框textfld

        self.textfld = [[UITextField alloc] initWithFrame:CGRectMake(100, 200, 200, 100)];

        //给文本框设置背景图片

        UIImageView *imageV1 = [[UIImageView alloc] initWithFrame:self.textfld.frame];

        imageV1.image = [UIImage imageNamed:@"27u58PICvXI_1024.jpg"];

        self.textfld.backgroundColor = [UIColor colorWithPatternImage:imageV1.image];

        //指定代理

        self.textfld.delegate = self;

        //通过属性str给文本框传值

        self.textfld.text = self.str;

        //把文本居中

        self.textfld.textAlignment = NSTextAlignmentCenter;

        //在view上显示textfld

        [self.view addSubview:self.textfld];

        

        

    }

    - (void)didReceiveMemoryWarning {

        [super didReceiveMemoryWarning];

        // Dispose of any resources that can be recreated.

    }

    //隐藏键盘

    -(BOOL)textFieldShouldReturn:(UITextField *)textField{

        

        //1.通知

        NSNotification *noti = [[NSNotification alloc] initWithName:@"lamco" object:textField.text userInfo:nil];

        //单列,

        [[NSNotificationCenter defaultCenter] postNotification:noti];

        

        

        if ([textField isFirstResponder]) {

            [textField resignFirstResponder];

        }

        [self.navigationController popToRootViewControllerAnimated:YES];

        

        return YES;

    }

    @end

  • 相关阅读:
    魅族--魅蓝metal
    小米2015.11.24 雷军:我所有的向往
    微软2015.10.4发布会
    创意app1
    奇怪的想法2
    奇怪想法1
    对不起,我爱你(悲剧,慎入)
    聊聊台式机1
    聊聊笔记本1--人生第一台笔记本
    聊聊路由器1
  • 原文地址:https://www.cnblogs.com/liumu/p/5285564.html
Copyright © 2011-2022 走看看