靜態單元格的使用
一、實現效果與說明
說明:觀察上面的展示效果,可以發現整個界面是由一個tableview來展示的,上面的數據都是固定的,且幾乎不會改變。
要完成上面的效果,有幾種方法:
(1)可以直接利用代碼,返回三組,在判斷每組有多少行,展示些什么數據,這樣寫“死”的代碼建議絕不要使用。
(2)稍微靈活一些的,可以把plist文件一懶加載的方式,加載到程序中,動態獲取。但是觀察界面結構,很容易看出這樣需要進行模型嵌套,很麻煩。
(3)storyboard提供了靜態單元格這個功能,可以很方便的完成上面的界面展示效果。(提示:在實際的開發中很少這樣使用)
二、使用靜態單元格完成簡單界面展示的過程
在類似的開發中,如果整個界面都是tableview,那么直接讓控制器繼承自uitableviewcontroller.
修改主控制器,讓其繼承自uitableviewcontroller
把storyboard中默認的uiview刪掉,直接拖一個viewcontroller
當拖入一個viewcontroller的時候,它上面默認就會有一個cell,默認情況下,這個cell是動態的,也就是默認是看不見的。
把cell設置成靜態的,在屬性面板的content 中設置為static cell(靜態cell)所見即所得 注意必須更改這里的這個屬性。
讓它和主控制器關聯
接下來,可以依次設置顯示的圖片和文字。
設置標題有兩種方式:
1是雙擊更改
2是點擊子控件 lable修改
按照界面需要,設置輔助視圖
設置有多少組,每組有多少行。
設置組:
點擊tableview 設置屬性面板的sections屬性。
設置每組多少行:
小技巧:如果寫的單元格千年不變,那么可以先寫一組中的一行,再拷貝,稍作修改即可。
注意:靜態單元格是實際開發中,很少用到,此處只當知識點介紹。
在uitableview的應用中使用動態單元格來完成app應用程序管理界面的搭建
一、實現效果
說明:該示例在storyboard中使用動態單元格來完成。
二、實現
1.項目文件結構和plist文件
2.實現過程以及代碼
在tableview的屬性選擇器中選擇動態單元格。
說明:在storyboard中直接使用其自帶的動態單元格完成tableviewcell的定義,并創建了一個管理該cell的類,進行了連線。
實現代碼:
數據模型部分:
yyappinfo.h文件
//
// yyappinfo.h
// 01-使用動態單元格來完成app應用程序管理界面的搭建
//
// created by 孔醫己 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文件
//
// yyappinfo.m
// 01-使用動態單元格來完成app應用程序管理界面的搭建
//
// created by 孔醫己 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文件
//
// yyappcell.h
// 01-使用動態單元格來完成app應用程序管理界面的搭建
//
// created by 孔醫己 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文件
//
// yyappcell.m
// 01-使用動態單元格來完成app應用程序管理界面的搭建
//
// created by 孔醫己 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
@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
{
//按鈕被點擊后,變為不可用狀態
sender.enabled=no;
//通知代理,完成提示下載已經完成的動畫效果
if ([self.delegate respondstoselector:@selector(btndidclick:)]) {
//一般而言,誰觸發就把誰傳過去
[self.delegate btndidclick:self];
}
}
@end
主控制器
yyviewcontroller.m文件
//
// yyviewcontroller.m
// 01-使用動態單元格來完成app應用程序管理界面的搭建
//
// created by 孔醫己 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
@implementation yyviewcontroller
- (void)viewdidload
{
[super viewdidload];
}
#pragma mark- 使用懶加載先把plist文件中得數據加載進來
-(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- 設置tableview的數據源方法
//組
-(nsinteger)numberofsectionsintableview:(uitableview *)tableview
{
return 1;
}
//行
-(nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section
{
return self.apps.count;
}
//組-行-數據
-(uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath
{
//創建cell
static nsstring *identifier=@"app";
yyappcell *cell=[tableview dequeuereusablecellwithidentifier:identifier];
//設置cell的數據
yyappinfo *appinfo=self.apps[indexpath.row];
//設置代理
cell.delegate=self;
cell.app=appinfo;
//返回cell
return cell;
}
#pragma mark- 設置代理
-(void)btndidclick:(yyappcell *)cell
{
//取出模型
yyappinfo *app=cell.app;
nslog(@"daili");
uilabel *lab=[[uilabel alloc]init];
//提示的顯示位置
lab.frame=cgrectmake(60, 300, 200, 20);
//設置提示文本
lab.text=[nsstring stringwithformat:@"%@已經下載完成",app.name];
//設置文本背景顏色
[lab setbackgroundcolor:[uicolor graycolor]];
[self.view addsubview:lab];
lab.alpha=1.0;
//設置動畫效果
[uiview animatewithduration:2.0 animations:^{
lab.alpha=0.0;
} completion:^(bool finished) {
//把彈出的提示信息從父視圖中刪除
[lab removefromsuperview];
}];
}
#pragma mark-隱藏狀態欄
-(bool)prefersstatusbarhidden
{
return yes;
}
@end
補充說明
在程序中通過標示符取出對應的cell,是因為在storyboard中已經對cell打上了標示符(app)的標簽。
//組-行-數據
-(uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath
{
//創建cell
static nsstring *identifier=@"app";
yyappcell *cell=[tableview dequeuereusablecellwithidentifier:identifier];
//設置cell的數據
yyappinfo *appinfo=self.apps[indexpath.row];
//設置代理
cell.delegate=self;
cell.app=appinfo;
//返回cell
return cell;
}