實現UItableview控制項資料重新整理
一、專案檔結構和plist檔案
二、實現效果
1.說明:這是一個英雄展示介面,點擊選中行,可以修改改行英雄的名稱(完成資料重新整理的操作).
運行介面:
點擊選中行:
修改資料後自動重新整理:
三、程式碼範例
資料模型部分:
YYheros.h檔案
複製代碼 代碼如下:
//
// YYheros.h
// 10-英雄展示(資料重新整理)
//
// Created by apple on 14-5-29.
// Copyright (c) 2014年 itcase. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "Global.h"
@interface YYheros : NSObject
@property(nonatomic,copy)NSString *name;
@property(nonatomic,copy)NSString *icon;
@property(nonatomic,copy)NSString *intro;
//-(instancetype)initWithDict:(NSDictionary *)dict;
//+(instancetype)herosWithDict:(NSDictionary *)dict;
YYinitH(hero)
@end
YYheros.m檔案
複製代碼 代碼如下:
//
// YYheros.m
// 10-英雄展示(資料重新整理)
//
// Created by apple on 14-5-29.
// Copyright (c) 2014年 itcase. All rights reserved.
//
#import "YYheros.h"
@implementation YYheros
//-(instancetype)initWithDict:(NSDictionary *)dict
//{
// if (self=[super init]) {
//// self.name=dict[@"name"];
//// self.icon=dict[@"icon"];
//// self.intro=dict[@"intro"];
//
// //使用KVC
// [self setValuesForKeysWithDictionary:dict];
// }
// return self;
//}
//
//+(instancetype)herosWithDict:(NSDictionary *)dict
//{
// return [[self alloc]initWithDict:dict];
//}
YYinitM(hero)
@end
主控制器 YYViewController.m檔案
複製代碼 代碼如下:
//
// YYViewController.m
// 10-英雄展示(資料重新整理)
//
// Created by apple on 14-5-29.
// Copyright (c) 2014年 itcase. All rights reserved.
//
#import "YYViewController.h"
#import "YYheros.h"
@interface YYViewController ()<UITableViewDataSource,UIAlertViewDelegate,UITableViewDelegate>
@property (strong, nonatomic) IBOutlet UITableView *tableview;
@property(nonatomic,strong)NSArray *heros;
@end
複製代碼 代碼如下:
@implementation YYViewController
- (void)viewDidLoad
{
[super viewDidLoad];
//設定資料來源
self.tableview.dataSource=self;
self.tableview.delegate=self;
self.tableview.rowHeight=60.f;
NSLog(@"%d",self.heros.count);
}
#pragma mark -懶載入
-(NSArray *)heros
{
if (_heros==nil) {
NSString *fullpath=[[NSBundle mainBundle]pathForResource:@"heros.plist" ofType:nil];
NSArray *temparray=[NSArray arrayWithContentsOfFile:fullpath];
NSMutableArray *arrayM=[NSMutableArray array];
for (NSDictionary *dict in temparray) {
YYheros *hero=[YYheros herosWithDict:dict];
[arrayM addObject:hero];
}
_heros=[arrayM mutableCopy];
}
return _heros;
}
#pragma mark- tableview的處理
//多少組
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
//多少行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.heros.count;
}
//每組每行的資料,設定cell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//NSLog(@"cellForRowAtIndexPath 修改的了 %d", indexPath.row);
//1.去緩衝中取
static NSString *identifier=@"hero";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];
//2.如果沒有,那麼就自己建立
if (cell==nil) {
NSLog(@"chuangjiancell");
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
//3.設定資料
//3.1拿到該行的模型
YYheros *hero=self.heros[indexPath.row];
cell.textLabel.text=hero.name;
cell.imageView.image=[UIImage imageNamed:hero.icon];
cell.detailTextLabel.text=hero.intro;
//4.返回cell
return cell;
}
#pragma mark-資料重新整理
//1.快顯視窗,拿到資料
//當某一行被選中的時候調用該方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//拿到改行的資料模型
YYheros *hero=self.heros[indexPath.row];
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"修改資料" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"確定", nil];
//密碼框形式的
//alert.alertViewStyle=UIAlertViewStyleSecureTextInput;
alert.alertViewStyle=UIAlertViewStylePlainTextInput;
UITextField *text=[alert textFieldAtIndex:0];
//把當前行的英雄資料顯示到文字框中
text.text=hero.name;
alert.tag=indexPath.row;
[alert show];
}
//2.修改資料,完成重新整理操作
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
//1.修改模型
//如果選中的是取消,那麼就返回,不做任何操作
if (0==buttonIndex) return;
//否則就修改模型,重新整理資料
YYheros *hero=self.heros[alertView.tag];
//拿到當前彈窗中的文本資料(已經修改後的資料)
UITextField *text=[alertView textFieldAtIndex:0];
//用修改後的資料去修改模型
hero.name=text.text;
//2.重新整理資料
// 只要調用tableview的該方法就會自動重新調用資料來源的所有方法
// 會自動調用numberOfSectionsInTableView
// 會自動調用numberOfRowsInSection
// 會自動調用cellForRowAtIndexPath
// [self.tableview reloadData];
// 重新整理指定行
NSIndexPath *path = [NSIndexPath indexPathForRow:alertView.tag inSection:0];
[self.tableview reloadRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationRight];
//如果不進行重新整理會怎麼樣?(修改之後不會即時重新整理,要等到重新對cell進行資料填充的時候才會重新整理)
}
//隱藏狀態列
-(BOOL)prefersStatusBarHidden
{
return YES;
}
@end
四、把常用的代碼封裝成一個帶參數的宏
封裝方法和代碼:
複製代碼 代碼如下:
//
// Global.h
// 10-英雄展示(資料重新整理)
//
// Created by apple on 14-5-29.
// Copyright (c) 2014年 itcase. All rights reserved.
//
#ifndef _0____________Global_h
#define _0____________Global_h
/**
* 自訂帶參數的宏
*/
#define YYinitH(name) -(instancetype)initWithDict:(NSDictionary *)dict;\
+(instancetype)herosWithDict:(NSDictionary *)dict;
#define YYinitM(name) -(instancetype)initWithDict:(NSDictionary *)dict\
{\
if (self=[super init]) {\
[self setValuesForKeysWithDictionary:dict];\
}\
return self;\
}\
\
+(instancetype)herosWithDict:(NSDictionary *)dict\
{\
return [[self alloc]initWithDict:dict];\
}\
#endif
以後在需要使用的時候,只需要使用宏即可。
如在YYheros.m檔案中使用YYinitM(hero)這一句代碼可以代替下面的程式碼片段:
複製代碼 代碼如下:
-(instancetype)initWithDict:(NSDictionary *)dict
{
if (self=[super init]) {
// self.name=dict[@"name"];
// self.icon=dict[@"icon"];
// self.intro=dict[@"intro"];
//使用KVC
[self setValuesForKeysWithDictionary:dict];
}
return self;
}
+(instancetype)herosWithDict:(NSDictionary *)dict
{
return [[self alloc]initWithDict:dict];
}
五、注意點
1.重新整理資料的兩個步驟:
1)修改模型
2)重新整理表格式資料(可以全部重新整理,也可以重新整理指定的行)
2.在主控制器檔案中,遵守了三個協議
分別是:
UITableViewDataSource,
UIAlertViewDelegate,
UITableViewDelegate
UITableview控制項使用小結
一、UITableview的使用步驟
UITableview的使用就只有簡單的三個步驟:
1.告訴一共有多少組資料
方法:
複製代碼 代碼如下:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
2.告訴每組一共有多少行
方法:
複製代碼 代碼如下:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
3.設定每組每行(cell)
方法:
複製代碼 代碼如下:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
二、使用說明
1.多少組資料和顯示多少行通常是和資料息息相關的,在開發中資料通常儲存在plist檔案中,需要以一定的方式載入到項目中(模型)。
2.設定每組每行,說簡單點就是設定tableview中得每個cell.
設定cell的步驟有三步:
(1)建立一個cell(需要考慮效能,對cell進行迴圈利用,注意緩衝處理方式)
(2)為cell設定資料
(3)返回一個cell
設定cell有三種方式:
(1)使用系統提供的tableviewcell進行設定
(2)通過xib自訂tableviewcell,適用於長相一致的,如團購展示介面
(3)通過純程式碼自訂tableviewcell,適用於有差距的,如表現為高度不一樣,有的cell擁有某個屬性,而有的cell中沒有,如微博展示介面
三、自訂tableviewcell
1.通過xib檔案自訂一個view的步驟
(1)建立一個xib檔案,描述一個view的內部
(2)建立一個自訂的類,自訂的類需要繼承自系統內建的view,繼承自哪個類,取決於xib跟對象的class
(3)建立類的類型最好跟xib的檔案名稱保持一致
(4)將xib的控制項和自訂類的.m檔案進行連線
(5)提供一個類的方法返回一個建立好的自定iview(屏蔽從xib載入的過程)
(6)提供一個模型屬性讓外界傳遞模型資料
(7)重寫模型屬性的setter方法,在這裡講模型資料展示到對應的子控制項上面
2.通過代碼方式自訂cell
(1)建立⼀一個繼承自UITableViewCell的類
(2)重寫initWithStyle:reuseIdentifier:方法
添加所有需要顯示的子控制項(不需要設定子控制項的資料和frame, 子控制項要添加 到contentView中)
對子控制項進行一次性的屬性設定(有些屬性只需要設定一次, 比如字型\固定的圖片)
(3)提供2個模型
資料模型: 存放文字資料\圖片資料
frame模型: 存放資料模型\所有子控制項的frame\cell的高度
(4)cell擁有一個frame模型(不要直接擁有資料模型)
(5)重寫frame模型屬性的setter方法: 在這個方法中設定子控制項的顯示資料和frame
(6)frame模型資料的初始化已經採取懶載入的方式(每一個cell對應的frame模型資料只載入一 次)
四、使用代理的步驟
(1)先搞清楚誰是誰的代理(delegate)
(2)定義代理協議,協議名稱的命名規範:控制項類名 + Delegate
(3)定義代理方法
代理方法一般都定義為@optional
代理方法名都以控制項名開頭
代理方法至少有1個參數,將控制項本身傳遞出去
(4)設定代理(delegate)對象 (⽐比如myView.delegate = xxxx;)
代理對象遵守協議
代理對象實現協議裡面該實現的方法
(5)在恰當的時刻調⽤代理對象(delegate)的代理方法,通知代理髮生了什麼事情
(在調⽤之前判斷代理是否實現了該代理⽅方法)