国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > iOS > 正文

iOS開發(fā)的UI制作中動態(tài)和靜態(tài)單元格的基本使用教程

2019-10-21 18:56:41
字體:
供稿:網(wǎng)友
這篇文章主要介紹了iOS開發(fā)的UI制作中動態(tài)和靜態(tài)單元格的基本使用教程,代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下
 

靜態(tài)單元格的使用
一、實現(xiàn)效果與說明

iOS開發(fā)的UI制作中動態(tài)和靜態(tài)單元格的基本使用教程

說明:觀察上面的展示效果,可以發(fā)現(xiàn)整個界面是由一個tableview來展示的,上面的數(shù)據(jù)都是固定的,且?guī)缀醪粫淖儭?/p>

要完成上面的效果,有幾種方法:

(1)可以直接利用代碼,返回三組,在判斷每組有多少行,展示些什么數(shù)據(jù),這樣寫“死”的代碼建議絕不要使用。

(2)稍微靈活一些的,可以把plist文件一懶加載的方式,加載到程序中,動態(tài)獲取。但是觀察界面結(jié)構(gòu),很容易看出這樣需要進行模型嵌套,很麻煩。

(3)storyboard提供了靜態(tài)單元格這個功能,可以很方便的完成上面的界面展示效果。(提示:在實際的開發(fā)中很少這樣使用)

二、使用靜態(tài)單元格完成簡單界面展示的過程

在類似的開發(fā)中,如果整個界面都是tableview,那么直接讓控制器繼承自UItableviewcontroller.

修改主控制器,讓其繼承自UItableviewcontroller

iOS開發(fā)的UI制作中動態(tài)和靜態(tài)單元格的基本使用教程

把storyboard中默認的uiview刪掉,直接拖一個viewcontroller

iOS開發(fā)的UI制作中動態(tài)和靜態(tài)單元格的基本使用教程

當拖入一個viewcontroller的時候,它上面默認就會有一個cell,默認情況下,這個cell是動態(tài)的,也就是默認是看不見的。

把cell設(shè)置成靜態(tài)的,在屬性面板的content  中設(shè)置為static cell(靜態(tài)cell)所見即所得  注意必須更改這里的這個屬性。

iOS開發(fā)的UI制作中動態(tài)和靜態(tài)單元格的基本使用教程

讓它和主控制器關(guān)聯(lián)

iOS開發(fā)的UI制作中動態(tài)和靜態(tài)單元格的基本使用教程

接下來,可以依次設(shè)置顯示的圖片和文字。

設(shè)置標題有兩種方式:

1是雙擊更改

iOS開發(fā)的UI制作中動態(tài)和靜態(tài)單元格的基本使用教程

2是點擊子控件  lable修改

iOS開發(fā)的UI制作中動態(tài)和靜態(tài)單元格的基本使用教程

按照界面需要,設(shè)置輔助視圖

iOS開發(fā)的UI制作中動態(tài)和靜態(tài)單元格的基本使用教程

設(shè)置有多少組,每組有多少行。
設(shè)置組:
點擊tableview   設(shè)置屬性面板的sections屬性。

iOS開發(fā)的UI制作中動態(tài)和靜態(tài)單元格的基本使用教程

設(shè)置每組多少行:

iOS開發(fā)的UI制作中動態(tài)和靜態(tài)單元格的基本使用教程

小技巧:如果寫的單元格千年不變,那么可以先寫一組中的一行,再拷貝,稍作修改即可。
注意:靜態(tài)單元格是實際開發(fā)中,很少用到,此處只當知識點介紹。


在UITableview的應(yīng)用中使用動態(tài)單元格來完成app應(yīng)用程序管理界面的搭建
一、實現(xiàn)效果

iOS開發(fā)的UI制作中動態(tài)和靜態(tài)單元格的基本使用教程

說明:該示例在storyboard中使用動態(tài)單元格來完成。

二、實現(xiàn)

1.項目文件結(jié)構(gòu)和plist文件

iOS開發(fā)的UI制作中動態(tài)和靜態(tài)單元格的基本使用教程

2.實現(xiàn)過程以及代碼

iOS開發(fā)的UI制作中動態(tài)和靜態(tài)單元格的基本使用教程

在tableview的屬性選擇器中選擇動態(tài)單元格。

iOS開發(fā)的UI制作中動態(tài)和靜態(tài)單元格的基本使用教程

說明:在storyboard中直接使用其自帶的動態(tài)單元格完成tableviewcell的定義,并創(chuàng)建了一個管理該cell的類,進行了連線。

實現(xiàn)代碼:

數(shù)據(jù)模型部分:

YYappInfo.h文件

 

復(fù)制代碼代碼如下:

//
//  YYappInfo.h
//  01-使用動態(tài)單元格來完成app應(yīng)用程序管理界面的搭建
//
//  Created by 孔醫(yī)己 on 14-6-2.
//  Copyright (c) 2014年 itcast. All rights reserved.
//

 

#import <Foundation/Foundation.h>

@interface YYappInfo : NSObject
@property(nonatomic,copy)NSString *size;
@property(nonatomic,copy)NSString *download;
@property(nonatomic,copy)NSString *icon;
@property(nonatomic,copy)NSString *name;

 

-(instancetype)initWithDict:(NSDictionary *)dict;
+(instancetype)appInfoWithDict:(NSDictionary *)dict;
@end


YYappInfo.m文件
復(fù)制代碼代碼如下:

//
//  YYappInfo.m
//  01-使用動態(tài)單元格來完成app應(yīng)用程序管理界面的搭建
//
//  Created by 孔醫(yī)己 on 14-6-2.
//  Copyright (c) 2014年 itcast. All rights reserved.
//

 

#import "YYappInfo.h"

@implementation YYappInfo

-(instancetype)initWithDict:(NSDictionary *)dict
{
    if (self=[super init]) {
        //使用KVC
        [self setValuesForKeysWithDictionary:dict];
    }
    return self;
}


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

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


視圖部分

 

 YYappCell.h文件

復(fù)制代碼代碼如下:

//
//  YYappCell.h
//  01-使用動態(tài)單元格來完成app應(yīng)用程序管理界面的搭建
//
//  Created by 孔醫(yī)己 on 14-6-2.
//  Copyright (c) 2014年 itcast. All rights reserved.
//

 

#import <UIKit/UIKit.h>


@class YYappInfo,YYappCell;

@protocol YYappCellDelegate <NSObject>
-(void)btnDidClick:(YYappCell *)cell;


@end
@interface YYappCell : UITableViewCell

@property(nonatomic,strong)YYappInfo *app;
//@property(nonatomic,strong)YYViewController *controller;
@property(nonatomic,strong)id <YYappCellDelegate> delegate;

@end


YYappCell.m文件
復(fù)制代碼代碼如下:

//
//  YYappCell.m
//  01-使用動態(tài)單元格來完成app應(yīng)用程序管理界面的搭建
//
//  Created by 孔醫(yī)己 on 14-6-2.
//  Copyright (c) 2014年 itcast. All rights reserved.
//

 

#import "YYappCell.h"
#import "YYappInfo.h"

@interface YYappCell ()
@property (weak, nonatomic) IBOutlet UIImageView *appimg;

@property (weak, nonatomic) IBOutlet UILabel *apptitle;
@property (weak, nonatomic) IBOutlet UILabel *appdownload;
@property (weak, nonatomic) IBOutlet UIButton *appbtn;

@end


復(fù)制代碼代碼如下:

@implementation YYappCell

 


-(void)setApp:(YYappInfo *)app
{
    _app=app;
    self.apptitle.text=_app.name;
    self.appdownload.text=[NSString stringWithFormat:@"大小 %@ | 下載量 %@次",_app.size,_app.download];
    self.appimg.image=[UIImage imageNamed:_app.icon];
    
}

#pragma mark- 完成按鈕點擊事件

- (IBAction)btnOnclick:(UIButton *)sender
{
    //按鈕被點擊后,變?yōu)椴豢捎脿顟B(tài)
    sender.enabled=NO;
    
    //通知代理,完成提示下載已經(jīng)完成的動畫效果
    if ([self.delegate respondsToSelector:@selector(btnDidClick:)]) {
        //一般而言,誰觸發(fā)就把誰傳過去
        [self.delegate  btnDidClick:self];
    }
}

@end


主控制器

 

YYViewController.m文件

復(fù)制代碼代碼如下:

//
//  YYViewController.m
//  01-使用動態(tài)單元格來完成app應(yīng)用程序管理界面的搭建
//
//  Created by 孔醫(yī)己 on 14-6-2.
//  Copyright (c) 2014年 itcast. All rights reserved.
//

 

#import "YYViewController.h"
#import "YYappInfo.h"
#import "YYappCell.h"

@interface YYViewController ()<UITableViewDataSource,YYappCellDelegate>
@property(nonatomic,strong)NSArray *apps;
@property (strong, nonatomic) IBOutlet UITableView *tableview;

@end


復(fù)制代碼代碼如下:

@implementation YYViewController

 

- (void)viewDidLoad
{
    [super viewDidLoad];
}

#pragma mark- 使用懶加載先把plist文件中得數(shù)據(jù)加載進來
-(NSArray *)apps
{
    if (_apps==Nil) {
        NSString *fullpath=[[NSBundle mainBundle]pathForResource:@"apps_full.plist" ofType:Nil];
        NSArray *arrayM=[NSArray arrayWithContentsOfFile:fullpath];
        
        NSMutableArray *modles=[NSMutableArray arrayWithCapacity:arrayM.count];
        for (NSDictionary *dict in arrayM) {
            YYappInfo *appinfo=[YYappInfo appInfoWithDict:dict];
            [modles addObject:appinfo];
        }
        _apps=[modles copy];
    }
    return _apps;
}


#pragma mark- 設(shè)置tableview的數(shù)據(jù)源方法
//組
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
//行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.apps.count;
}
//組-行-數(shù)據(jù)
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //創(chuàng)建cell
    static NSString *identifier=@"app";
    YYappCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];
    //設(shè)置cell的數(shù)據(jù)
    YYappInfo *appinfo=self.apps[indexPath.row];
    //設(shè)置代理
    cell.delegate=self;
    cell.app=appinfo;
    //返回cell
    return cell;
}

#pragma mark- 設(shè)置代理
-(void)btnDidClick:(YYappCell *)cell
{
    //取出模型
    YYappInfo *app=cell.app;
    NSLog(@"daili");
    UILabel *lab=[[UILabel alloc]init];
    //提示的顯示位置
    lab.frame=CGRectMake(60, 300, 200, 20);
    //設(shè)置提示文本
    lab.text=[NSString stringWithFormat:@"%@已經(jīng)下載完成",app.name];
    //設(shè)置文本背景顏色
    [lab setBackgroundColor:[UIColor grayColor]];
    [self.view addSubview:lab];
    lab.alpha=1.0;
    
    //設(shè)置動畫效果
    [UIView animateWithDuration:2.0 animations:^{
        lab.alpha=0.0;
    } completion:^(BOOL finished) {
        //把彈出的提示信息從父視圖中刪除
        [lab removeFromSuperview];
    }];
}

#pragma mark-隱藏狀態(tài)欄
-(BOOL)prefersStatusBarHidden
{
    return YES;
}

@end


補充說明

 

  在程序中通過標示符取出對應(yīng)的cell,是因為在storyboard中已經(jīng)對cell打上了標示符(app)的標簽。

復(fù)制代碼代碼如下:

//組-行-數(shù)據(jù)
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //創(chuàng)建cell
    static NSString *identifier=@"app";
    YYappCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];
    //設(shè)置cell的數(shù)據(jù)
    YYappInfo *appinfo=self.apps[indexPath.row];
    //設(shè)置代理
    cell.delegate=self;
    cell.app=appinfo;
    //返回cell
    return cell;
}

 

iOS開發(fā)的UI制作中動態(tài)和靜態(tài)單元格的基本使用教程



注:相關(guān)教程知識閱讀請移步到IOS開發(fā)頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 柳州市| 乌拉特后旗| 邮箱| 湘潭县| 定南县| 日土县| 浮山县| 唐河县| 祁阳县| 沐川县| 连平县| 盐边县| 伽师县| 晋宁县| 通化市| 威宁| 浮梁县| 宁都县| 托克托县| 天祝| 滨海县| 公安县| 乌拉特中旗| 定结县| 宜兰县| 阿拉善左旗| 莱芜市| 岳池县| 武冈市| 新巴尔虎右旗| 翼城县| 凭祥市| 绩溪县| 嘉定区| 台中县| 太谷县| 合肥市| 枣阳市| 赤水市| 丹巴县| 大英县|