zoukankan      html  css  js  c++  java
  • IOS开发中tableView显示列表内容数据(storyboard版)

    这是第一次写博客这类东西,且同为菜鸟级自学IOS,若有哪些不正确的希望您指正,谢谢。。。

     

    先写一个大家自学时都会用到的东西——列表展示,或许您不认为这是问题,那是因为您聪慧,刚学时倒是困扰到我了,特意写一下;

    第一步:创建工程IOS--》single view application

         ——》 Product Name:tableViewDemo    

                   Language:Objective—C    

                   Devices:iPhone,

                                    点击NEXT,选择您的文件夹,Create

         ——》单击打开Main.storyboard,  (咱们就用生成项目时自带的视图控制器)

                  在控件栏中找到UITableView控件,拖拽到试图控制器上(您可以全部覆盖,也可以覆盖一部分区域)

                  

    第二步:设置tableview的dataSource和delegate

            ——》点击上图最上面的第一个黄色标志;然后点击如下图最上边的最后一个按钮;出现如下图界面;

        ——》在上图的Referencing Outlets一栏,从空心圆圈中拖拽一根线到我们刚刚覆盖上去的UITableView控件上;弹出dataSource与delegate;选择delegate;

        ——》重复上个过程的拖拽,并选择dataSource;

    第三步:编码实现显示

         ——》打开ViewController.h文件

     #import <UIKit/UIKit.h>
    
     @interface ViewController : UIViewController<UITableViewDelegate, UITableViewDataSource>
    
    @end

     

         ——》打开ViewController.m文件,并实现tableview的代理方法

           (1)定义全局变量arrayData,用于装我们要显示在列表上的内容文字

           (2)在ViewDidLoad()中,对数组变量进行初始化;

           (3)实现代理方法

    复制代码
    #import "ViewController.h"
    
    @interface ViewController ()
    @property (nonatomic, strong) NSArray *arrayData;
    @end
    
    @implementation ViewController
    @synthesize arrayData;
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        arrayData = [NSArray arrayWithObjects:@"王小虎",@"郭二牛",@"宋小六",@"耿老三",@"曹大将军", nil];
    }
    
    #pragma mark -- delegate方法
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
        return 1;
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        return arrayData.count;
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *indentifier = @"cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:indentifier];
        
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:indentifier];
        }
    
        cell.textLabel.text = [arrayData objectAtIndex:indexPath.row];
        
        return cell;
    }
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    @end
    复制代码

      点击运行:则可出先如下效果

  • 相关阅读:
    华为手机内核代码的编译及刷入教程【通过魔改华为P9 Android Kernel 对抗反调试机制】
    AndroidStudio升级到4.0之后,出现Warning: Default Activity not found解决办法
    Python解决gensim加载doc2vec或work2vec向量训练模型文件太慢甚至无法访问的情况
    layui 上传插件控制上传文件个数(换个角度思考问题)
    CDN访问异常重定向
    sql 语句优化
    swipper全屏垂直滚动获取高度问题
    tp6.0相对于tp5.1的变化
    项目打包文件ipa包瘦身
    load和initialize的区别
  • 原文地址:https://www.cnblogs.com/czjie2010/p/4538049.html
Copyright © 2011-2022 走看看