UITabelView图片加 label 显示

本文深入探讨了Objective-C与Swift两种编程语言在iOS开发领域的应用,包括它们的优势、特点及如何高效地进行iOS应用开发。通过实际案例,展示了这两种语言在解决实际问题时的差异与优势。
//UITabelView 显示图片和 label (可以画一个图, 分好层)
#import "ImageViewController.h"
#import "Data.h"

@interface ImageViewController () <UITableViewDataSource>
//写成属性
@property (nonatomic, copy )NSArray *imageArray, *labelArray;

@property (nonatomic, copy)NSArray *dataArray;//将图片与 label, 合在一起
@end
//防止写错
static NSString *identifier = @"abc";

@implementation ImageViewController
//属性写完, 每次都会释放.
- (void)dealloc
{
    [_dataArray release];
    [_labelArray release];
    [_imageArray release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    UITableView *tabView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
    //    tabView.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:tabView];
    tabView.dataSource = self;
    [tabView registerClass:[UITableViewCell class] forCellReuseIdentifier: identifier];
    //太小的话, 需要设置一下行高, rowheight
    tabView.rowHeight = 100;
    [tabView release];

    //三种不同的方法, 2.3是将 OC 与 UI 结合
    //1.因为如果设置为, 全局 或者 实例变量 数组也不知道什么时候释放, 所以设置为属性, 就可以避免这种问题. 每次程序结束时, 就会调用 dealloc 方法.
    self.imageArray = [[[NSArray alloc] initWithObjects:@"picture1.jpg",@"picture2.jpg",@"picture3.jpg",@"picture4.jpg",@"picture5.jpg",nil] autorelease];//加上@""就会变成对象
    //或者写成语法糖, 便利构造器(3种方法)

    //不同文字

    self.labelArray = @[@"你好, 我叫小芳", @"good morning", @"你真的好棒呀", @"yes, yes, you are right", @"好好学习. 天天向上"];

    //2.一一对应(为了防止图片与 label 不能对应)
    /*. 语法糖创建的 key value, 一一对应  key:value
    NSDictionary *dic1 = @{@"image":@"picture1.jpg", @"label":@"你好, 我叫小芳"};
    NSDictionary *dic2 = @{@"image":@"picture2.jpg", @"label":@"good morning"};
    NSDictionary *dic3 = @{@"image":@"picture3.jpg", @"label":@"你真的好棒呀"};
    NSDictionary *dic4 = @{@"image":@"picture4.jpg", @"label":@"yes, yes, you are right"};
    NSDictionary *dic5 = @{@"image":@"picture5.jpg", @"label":@"好好学习. 天天向上"};
    //数组里面放字典

    self.dataArray = @[dic1, dic2, dic3, dic4, dic5];
    //注: 字典,可以归类; 数组, 有序; 所以, 通常将两者结合在一起
    */

    //3.将字典转换为对象设置为对象  (字典与对象, 两者是可以相互转换的), 对象里面有两个属性, 所以创建了一个类
    Data *data1 = [Data dataWithLabel:@"你好, 我叫小芳" image:@"picture1.jpg"];
    Data *data2 = [Data dataWithLabel:@"good morning" image:@"picture2.jpg"];
    Data *data3 = [Data dataWithLabel:@"你真的好棒呀" image:@"picture3.jpg"];
    Data *data4 = [Data dataWithLabel:@"yes, yes, you are right" image:@"picture4.jpg"];
    Data *data5 = [Data dataWithLabel:@"好好学习. 天天向上" image:@"picture5.jpg"];

    self.dataArray = @[data1, data2, data3, data4, data5];


    //4.从属性列表中, 读取数据.注意:最外层的类型(和第2种方法一样)!!
    //图片, label; 外面是数组; 数组里面每个元素是字典, 建立了 Data文件资源

    //获取文件里面的所有资源,
    NSString *path = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"];//第二步
    //因为最外层是:数组, 用数组结束文件里面的内容
    self.dataArray = [NSArray arrayWithContentsOfFile:path];//第一步
    NSLog(@"%@", self.dataArray);


}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - UITableViewDataSource
//行数, 单元格, 是必须实现的!! 所以, 这两个方法必须都要写
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    //注意: 参数 section, 就是分区, 也就是数组的下标, 获取属性值的时候, 一般用 self.属性
    return self.dataArray.count;//会显示10行, 后面的点不了.优化, 以后再改, 不要在多次修改了
}

//该方法中, 返回值 UITableView内容; 参数1: 就是该tableView; 参数2:第几行
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];//方法 dequeueReuseableCellWithIdetifier

    //为了展示不同的图片
    //2.注意: index 就是位置(有 section, row)
    NSDictionary *dic = self.dataArray[indexPath.row];//获取到字典(里面有很多的 key-value 对)
//    [dic objectForKey:@"image"];//通过 key 值, 找到值
//    dic[@"image"];
//    Data *data = self.dataArray[indexPath.row];
    cell.imageView.image = [UIImage imageNamed:dic[@"image"]];//语法糖的特点
//    cell.imageView.image = [UIImage imageNamed:data.image];
    cell.imageView.layer.cornerRadius = 50;//图片成为圆角
    cell.imageView.clipsToBounds = YES;//多余的减掉
    cell.textLabel.text = dic[@"label"];
//    cell.textLabel.text = data.label;//这是对象调用的方法
    cell.textLabel.numberOfLines = 0;//自动换行

    return cell;
}
@end
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值