ios-day09-03(模仿应用列表。Prototype Cells的使用、代理的使用、模仿Android中显示Toast)

本文介绍如何使用UITableView在iOS应用中实现动态原型单元格的应用列表,包括Storyboard配置、代码实现及下载按钮交互。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

源码下载地址:http://download.youkuaiyun.com/detail/liu537192/8482369


效果图:



步骤:

编辑main.storyboard文件。

删掉原来的控制器,弄一个UITableViewController。如图所示:


选中TableView,设置属性,如图所示:



Content属性有两个值,一个是Dynamic Prototypes,另一个是Static Cells(静态cell,前面的文章已经讲过了)

这个UITableView的顶部有个Prototypes Cell,prototype是”原型“的意思,意思是其他的cell都是以这个cell为原型。

如果将Prototype Cells属性设置为2,我们就有两个原型


选中这个Prototype Cell,设置它的属性Identifer的值为app,如图所示:



然后设置其Class属性为我们自定义的cell,如图所示:



我们在代码中:

[tableView dequeueReusableCellWithIdentifier:@"app"],这句代码会自动帮我们创建cell,而且如果有可以复用的cell,就会复用,没有可以复用的cell,则会重新创建。


核心代码:

//
//  JLViewController.m
//  03- 应用列表
//
//  Created by Mac on 15-3-7.
//  Copyright (c) 2015年 vxinyou. All rights reserved.
//

#import "JLViewController.h"
#import "JLApp.h"
#import "JLAppCell.h"

@interface JLViewController () 
   
    

@property (nonatomic, strong)NSArray *apps;

@end

@implementation JLViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    
}
/**
 *  隐藏状态栏
 */
- (BOOL)prefersStatusBarHidden{

    return YES;
}

- (NSArray *)apps{

    if (_apps == nil) {
        // 获取plist文件路径
        NSString *path = [[NSBundle mainBundle] pathForResource:@"apps_full.plist" ofType:nil];
        // 加载plist文件中的内容到数组
        NSArray *dictArray = [NSArray arrayWithContentsOfFile:path];
        // 字典转模型
        NSMutableArray *tempArray = [NSMutableArray array];
        for (NSDictionary *dict in dictArray) {
            JLApp *app = [JLApp appWithDict:dict];
            
            [tempArray addObject:app];
        }
        
        _apps = tempArray;
    }
    
    return _apps;
}
#pragma mark -JLAppCellDelegate代理方法
- (void)appCellDidClickDownloadBtn:(JLAppCell *)appCell{

//    NSLog(@"点击了下载按钮");
    
    // 像Android一样显示Toast
    
    // 1.添加标签
    UILabel *label = [[UILabel alloc] init];
    label.text = [NSString stringWithFormat:@"成功下载%@", appCell.app.name];
    label.font = [UIFont systemFontOfSize:12];
    label.textAlignment = NSTextAlignmentCenter;
    label.textColor = [UIColor whiteColor];
    label.backgroundColor = [UIColor blackColor];
    label.frame = CGRectMake(0, 0, 150, 25);
    label.center = CGPointMake(160, 240);
    label.alpha = 0.0;
    label.layer.cornerRadius = 5;
    label.clipsToBounds = YES;
    [self.view addSubview:label];
    
    // 2.动画
    [UIView animateWithDuration:0.5 animations:^{
        label.alpha = 0.5;
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.5 delay:0.5 options:UIViewAnimationOptionCurveLinear animations:^{
            label.alpha = 0.0;
        } completion:^(BOOL finished) {
            [label removeFromSuperview];
        }];
    }];
}

#pragma mark -数据源方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return 1;
}

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

    return self.apps.count;
}

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

    static NSString *ID = @"app";
    
    JLAppCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    cell.app = self.apps[indexPath.row];
    
    // 设置代理
    cell.delegate = self;
    
    return cell;
}

@end

   

//
//  JLApp.h
//  03- 应用列表
//
//  Created by Mac on 15-3-7.
//  Copyright (c) 2015年 vxinyou. All rights reserved.
//

#import 
   
    

@interface JLApp : NSObject
/**
 *  应用图标
 */
@property (nonatomic, copy)NSString *icon;
/**
 *  应用名称
 */
@property (nonatomic, copy)NSString *name;
/**
 *  应用大小
 */
@property (nonatomic, copy)NSString *size;
/**
 *  应用下载量
 */
@property (nonatomic, copy)NSString *download;
/**
 *  用户是否已经下载过,如果用户已经下载过,右边的按钮应该显示“已下载”
 */
@property (nonatomic, assign, getter = isDownloaded)BOOL downloaded;

- (instancetype)initWithDict:(NSDictionary *)dict;

+ (instancetype)appWithDict:(NSDictionary *)dict;

@end
//
//  JLApp.m
//  03- 应用列表
//
//  Created by Mac on 15-3-7.
//  Copyright (c) 2015年 vxinyou. All rights reserved.
//

#import "JLApp.h"

@implementation JLApp

- (instancetype)initWithDict:(NSDictionary *)dict{

    if (self = [super init]) {
        [self setValuesForKeysWithDictionary:dict];
    }
    return self;
}

+ (instancetype)appWithDict:(NSDictionary *)dict{

    return [[self alloc] initWithDict:dict];
}

@end
//
//  JLAppCell.h
//  03- 应用列表
//
//  Created by Mac on 15-3-7.
//  Copyright (c) 2015年 vxinyou. All rights reserved.
//

#import 
    
     
@class JLApp,JLAppCell;

@protocol JLAppCellDelegate 
     
      

@optional
- (void)appCellDidClickDownloadBtn:(JLAppCell *)appCell;

@end

@interface JLAppCell : UITableViewCell

@property (nonatomic, strong)JLApp *app;

@property (nonatomic, strong)id
      
        delegate;

@end
//
//  JLAppCell.m
//  03- 应用列表
//
//  Created by Mac on 15-3-7.
//  Copyright (c) 2015年 vxinyou. All rights reserved.
//

#import "JLAppCell.h"
#import "JLApp.h"

@interface JLAppCell()
/**
 *  应用图标
 */
@property (weak, nonatomic) IBOutlet UIImageView *iconView;
/**
 *  应用名称
 */
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
/**
 *  应用下载量
 */
@property (weak, nonatomic) IBOutlet UILabel *downCountLabel;
/**
 *  下载按钮
 */
@property (weak, nonatomic) IBOutlet UIButton *downloadBtn;
/**
 *  下载按钮被点击
 */
- (IBAction)downloadBtnClick;

@end

@implementation JLAppCell


- (void)setApp:(JLApp *)app{
    
    _app = app;
    
    self.iconView.image = [UIImage imageNamed:self.app.icon];
    
    self.nameLabel.text = self.app.name;
    
    self.downCountLabel.text = [NSString stringWithFormat:@"大小:%@ | 下载量:%@", self.app.size, self.app.download];
    
    // 如果不设置这里,“下载”按钮的显示会错乱
    self.downloadBtn.enabled = !self.app.downloaded;
}

- (IBAction)downloadBtnClick {
    
    self.app.downloaded = YES;
    
    // 设置“下载”按钮不可用,已经在storyboard文件中设置当按钮不可用时,显示的文字为“已下载”
    // 所以代码中无需修改按钮的文字
    self.downloadBtn.enabled = NO;
    
    if ([self.delegate respondsToSelector:@selector(appCellDidClickDownloadBtn:)]) {
        
        [self.delegate appCellDidClickDownloadBtn:self];
    }
    
}
@end

      
     
    
   

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值