http://www.sufeinet.com/plugin.php?id=keke_group

苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

分布式系统框架(V2.0) 轻松承载百亿数据,千万流量!讨论专区 - 源码下载 - 官方教程

HttpHelper爬虫框架(V2.7-含.netcore) HttpHelper官方出品,爬虫框架讨论区 - 源码下载 - 在线测试和代码生成

HttpHelper爬虫类(V2.0) 开源的爬虫类,支持多种模式和属性 源码 - 代码生成器 - 讨论区 - 教程- 例子

查看: 4363|回复: 6

[新手开发之旅] iOS新手开发之旅-UITableView动态添加数据显示

[复制链接]
发表于 2018-12-4 10:58:09 | 显示全部楼层 |阅读模式
本帖最后由 竹林风 于 2018-12-5 18:15 编辑

文章导航  

【iOS新手开发之旅】   http://www.sufeinet.com/thread-24000-1-1.html


上篇中初识了UITableView,今天就来看看怎么使用动态数据源来显示我们的数据吧!

直接上代码 :

[Objective-C] 纯文本查看 复制代码
#import "firstVC.h"


@interface firstVC ()<UITableViewDelegate,UITableViewDataSource>{
    
    NSArray *aryData;
}

@end

@implementation firstVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor yellowColor];
    self.title = @"水果";
    
    aryData = @[@{@"icon":@"apple",@"name":@"苹果"},@{@"icon":@"banana",@"name":@"香蕉"},@{@"icon":@"orange",@"name":@"桔子"},@{@"icon":@"pear",@"name":@"梨"},@{@"icon":@"peach",@"name":@"桃子"}];
    
    UITableView *tableView =[[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
//    设置数据源代理
    tableView.delegate = self;
    tableView.dataSource = self;
    [self.view addSubview:tableView];
    
}

//数据源代理方法

/**
 *  告诉tableView一共有多少组数据,默认为1组
 */
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}

/**
 *  告诉tableView第section组有多少行
 */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return aryData.count;
}

/**
 *  告诉tableView第indexPath行显示怎样的cell
 */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    // 0.重用标识
    // 被static修饰的局部变量:只会初始化一次,在整个程序运行过程中,只有一份内存
    static NSString *ID = @"cell";
    
    // 1.先根据cell的标识去缓存池中查找可循环利用的cell
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    // 2.如果cell为nil(缓存池找不到对应的cell)
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    // 3.覆盖数据
    NSDictionary *dic = aryData[indexPath.row];
    cell.imageView.image = [UIImage imageNamed:dic[@"icon"]];
    cell.textLabel.text = dic[@"name"];
    
    return cell;
}

/**
 *  告诉tableView第section组的头部标题
 */
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    
    return @"水果分类";
}

/**
 *  告诉tableView第section组的尾部标题
 */
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
    
    return @"尾部标题:这些都是我爱吃的哦!你呢?";
}


运行效果:

09D36E6F79A90FF25FAFFBBA46438820.png

快动手试试吧!

附件: Test-UITableView.zip (153.38 KB, 下载次数: 0)


1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
发表于 2018-12-4 11:00:25 | 显示全部楼层
是不是忘记加目录链接了
 楼主| 发表于 2018-12-4 12:03:28 | 显示全部楼层
站长苏飞 发表于 2018-12-4 11:00
是不是忘记加目录链接了

刚加上了,谢谢提醒
发表于 2018-12-4 14:57:52 | 显示全部楼层
楼主加油,我们都看好你哦。
发表于 2018-12-4 15:04:59 | 显示全部楼层
我只是路过打酱油的。
发表于 2018-12-4 16:34:28 | 显示全部楼层
强烈支持楼主ing……
发表于 2018-12-5 11:56:36 | 显示全部楼层
强烈支持楼主ing……
您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

QQ|手机版|小黑屋|手机版|联系我们|关于我们|广告合作|苏飞论坛 ( 豫ICP备18043678号-2)

GMT+8, 2024-4-19 13:32

© 2014-2021

快速回复 返回顶部 返回列表