zoukankan      html  css  js  c++  java
  • 编写简单的TableView

    在iOS下编写TableView是很容易的。

    最简单的TableView

    比如实现这样的效果:

    image

    首先创建一个view based application:

    image

    接着,修改view xib文件,将TableView拖拽到view中:

    image image

    右侧图是拖拽后的效果。然后是建立关联,需要建立两个:

    • dataSource,连接到数据源,这样TableView才知道显示数据的信息
    • delegate,连接到TableView delegate,回调它来和应用的逻辑部分交互

    都连接到Controller即可,即file’s owner:

    image

    然后,需要让Controller实现两个protocol:

    @interface TableDemoViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>

    其中UITableViewDataSource要求两个方法必须实现:

    image

    这里要先在h文件中声明用于dataSource的数据结构,这是使用的是固定数组:

        NSArray *dataItems;
    }

    @property(nonatomic,retain) NSArray *dataItems;

    然后,在m文件中实例化数组:

    @synthesize dataItems;

    - (void)viewDidLoad {
        dataItems= [[NSArray alloc] initWithObjects:@"张三",@"李四",nil];
        [super viewDidLoad];
    }

    现在可以实现上面的两个方法了:

    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
        return [dataItems count];
    }

    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
        static NSString *simpleTableIdentifier=@"SimpleTableIdentifier";
        UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
        if(cell==nil){
            cell=[[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:simpleTableIdentifier] autorelease];
        }
        NSUInteger row=[indexPath row];
        cell.textLabel.text=[dataItems objectAtIndex:row];
        return cell;
    }

    这里要注意,我写的例子是参照《iPhone开发基础教程》,里面的写法是:

    cell.text=…

    已经不建议使用了:

    image

    在TableView条目前添加图片

    如果想实现这样的效果:

    image

    这里的图片,来自项目自身,可以将图片拖拽到项目的Resources目录下:

    image

    然后在代码中只需增加一行:

    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
        static NSString *simpleTableIdentifier=@"SimpleTableIdentifier";
        UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
        if(cell==nil){
            cell=[[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:simpleTableIdentifier] autorelease];
        }
        NSUInteger row=[indexPath row];
        cell.textLabel.text=[dataItems objectAtIndex:row];
        cell.imageView.image=[UIImage imageNamed:@"tag.png"];
        return cell;

    为TableView增加交互功能

    只需增加一个函数即可,这是UITableViewDelegate protocol中的一个函数,用于在选择表条目后回调。

    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
        NSLog(@">>choose:%@",[dataItems objectAtIndex:[indexPath row]]);
    }

    这样当点击条目后,日志中就会打印条目中的数据。

    http://marshal.easymorse.com/archives/3511
  • 相关阅读:
    CentOS下使用crontab命令来定时执行任务
    java调用wkhtmltopdf生成pdf文件,美观,省事
    ElasticSearch-5.3.1集群环境搭建,安装ElasticSearch-head插件,安装错误解决
    Jfinal QuartzPlugin 简单使用案例
    tampermonkey,采用js解析自定义脚本,实现网页列表数据采集分析
    安装Elasticsearch5.0 部署Head插件
    Springmvc中配置Quartz使用,实现任务实时调度。
    IDEA异常解决: org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)
    js jquery 实现html页面之间参数传递(单一参数、对象参数传递)
    ajaxfileupload 实现多文件上传
  • 原文地址:https://www.cnblogs.com/Snowfun/p/2046338.html
Copyright © 2011-2022 走看看