標籤:
一、UITableView的簡單使用
顯示要素:
1、顯示多少給區組
2、顯示多少行資料
3、每行顯示什麼內容
代理不會提醒你有什麼方法沒調用,但是UITableViewDataSource會
1)用代碼建立一個UITableView
UITableView *tableview =[[UITableView alloc]initWithFrame:CGRectMakr(0,0,[UIScreen mainScreen].bounds.size.width,[UIScreen mainScreen].bounds.size.height) style:UITableViewStylePlain];
/**
注意 style:
UITableViewStylePlain
UITableViewStyleGrouped
區別:plain 的footer 和 header 具有浮動效果,並且中間沒有間隔
grouped:footer和header之間具有一定的間距
*/
2)設定tableview datasource的代理方法
tableview.datasource =self;
3)調用三個一定要實現的代理方法
3.1) numberOfSectionsInTableView 返回(NSInteger)
3.2) numberOfRowsInSection 返回(NSInteger)
3.3)cellForRowsAtIndex 返回(UITableViewCell*)
4)注意 三個代理方法的調用方式(當你設定3個section 分別顯示1,2,2行row 的時候)
可以觀測到:
調用section組數次numberOfSectionsInTableView方法
每次都會確認全部組對應的相應行數 調用numberOfRowsInSection
sections*rows
調用sections*rows次cellForRowsAtIndex方法
把對應每組每行都取過去
5)總結調用代理方法的過程和次數:
每次先調用擷取總共的組數,然後傳入組數,擷取第一組的行數,再傳入組數,擷取第二組的行數,以此類推,最後,根據每組對應的每行 indexPath 屬性進行內容傳遞
6)設定tableview section的header和footer
titleForFooterInSection/titleForHeaderInsection 返回(NSString*)
注意也可以用viewForFooterInSection/viewForHeaderInSection 不同的是,需要定義(UITableViewDelegate)這個代理,返回(UIView*)
直接返回就可顯示
二、汽車展示:UITableView的資料繫結
1)建立model類
@property(nonatomic,copy)NSString *name;
@property(nonatomic,copy)NSString *icon;
-(instancetype)initWithDictionary:(NSDictionary*)dict;
+(instancetype) CarinitWithDictioary:(NSDictionary*)dict;
2)定義執行model類方法
-(instancetype) initWithDictionary:(NSDictionary*)dict
{//注意,之前錯寫成 Carmodel .init 報錯不能初始化一個類NSObject
if(self =[super init])
{ [self setValuesForKeyWithDictionary:dict];}}
+(instancetype)CarinitWithDictionary:(NSDictionary*)dict{[return [self alloc]initWithDictionary:dict];}
3)進行懶載入,懶載入實際上就是重定義它的getter方法,而且可以在使用的時候分配記憶體,不使用的時候自動回收記憶體,節省記憶體消耗,並且可以避免重複執行個體化,各執行個體之間獨立,同時也能夠減少直接在viewDidLoad中定義參數的代碼量
@property (nonatomic,strong) NSArray *dataArray;(如果將來會修改dataArray的值時,需要用NSMutableArray 才可以調用相應的賦值方法 addObjects..)
-(NSArray *) dataArray
{
NSString *path=[NSBundel mainBundel] pathForResourct :@"car.plist",ofType:nil];
NSArray *tempArray =[NSArray arrayWithContentsOfFile :path];
NSMutableArray *muArray =[NSMutableArray array];
for(NSDictionary *dict in tempArray)
{
CarModel *model =[CarModel CarinitWithDictionary:dict];
[muArray addObject:model];
}
_dataArray =muArray;
return _dataArray;
}
注意,
如果tableview中沒有顯示的時候,可以考慮:
1、是否添加代理 tableview.delegate=self;
2、考慮是否取出資料:path路徑是否正確,懶載入是否成功?
三、一些其他的tableview 和tableviewcell 的屬性
1)/*
cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator; //箭頭
cell.accessoryType=UITableViewCellAccessoryNone; //沒有箭頭
cell.accessoryType =UITableViewCellAccessoryCheckmark; //打勾
cell.accessoryType=UITableViewCellAccessoryDetailButton;//圓圈+i
*/
/*
cell.selectionStyle=UITableViewCellSelectionStyleBlue;//ios7以上預設為灰色
cell.selectionStyle=UITableViewCellSelectionStyleGray;
cell.selectionStyle=UITableViewCellSelectionStyleNone;//什麼都不顯示
cell.selectionStyle=UITableViewCellSelectionStyleDefault;//預設為灰色
cell.selectedBackgroundView設定背景view 但是不能改變寬高
*/
/**
tableview 的常見屬性
1、rowHeight 行高
2、separatorColor 分割線顏色
3、separatorStyle 分割線樣式
4、allowMultipleSelection 是否允許多行選擇
*/
/**
cell的常見屬性
1、selectionStyle
2、selectedBackgroundView 注意 view寬高是可以改變,但是相對位置無法改變
3、background 注意其backgroundview不設定frame 預設就是完整的一行寬高,即便改變,寬高的設定仍然是無效的
*/
/**
cell的內建控制項
1、textLabel
2、detailLabel
3、imageview
4、accessoryType
5、accessoryView
*/
/**
UITableViewCellStyle
UITableViewCellStyleDefault
UITableViewCellStyleValue1
UITableViewCellStyleValue2
UITableViewCellStyleSubtitle
*/
/**
accessoryType
UITableViewCellAccessoryDetailDisclosureIndicator
UITableViewCellAccessorycheckwork
*/
/**
tableview.separatorStyle
UITableViewCellSeparatorStyleNone
UITableViewCellSepatratorStyleSingleLine
UITableViewCellSeparatorStyleSingleLineEtched
*/
/**
設定cell的選擇樣式
UITableViewCellSelectionsStyleNone
UITableViewCellSelectionsStyleGray
UITableViewCellSelectionsStyleDefault
UITableViewCellSelectionsStyleBlue
*/
2)注意為什麼在tableview的section中加入了footer/header但是沒有顯示
如果直接控制項拖拽UITableView 的話會產生一個預設的section高度
但是如果是代碼添加的話,就需要手動設定:
tableview.sectionFooterHeight=20;
tableview.SectionHeaderHeight =20;
自訂accessoryview的時候,frame中的座標值不能改變,只能改變其寬高
類似switch 的寬textfield的高 是不能修改的
3)設定tableview的行高有兩種方式
a、tableview.rowHeight =5;
b heightForRowAtIndexPath(UITableViewDelegate) 返回NSFloat
唯一的區別是,用執行方法,可以擷取indexpath 可以針對不同的行設定會變動的行高
四、cell的重用
1)瞭解為什麼要重用cell
cell的瀏覽機制是,當使用者滑動時,划出視窗的cell就會被廢棄,然後進入的是一個重新建立好的cell,這樣的話,一旦已耗用時間過長,不斷的廢棄重創, 會造成程式記憶體吃緊,效率非常低 隱患很大。每個cell小時之後,都會重新建立cell
cell重用機制:把移出的cell 放入緩衝池,重複使用,而只是更換上面的資料
那麼,當有多種不同類型的cell在tableview上顯示的時候,怎麼在緩衝池中找到
重用標識符!即需要重用的cell起名 identifier
2)cell重用的操作過程
1、定義重用標識符
2、在緩衝池中尋找
3、判斷緩衝池中是否有相應的cell 如果沒有的話,就重新建立
4、賦值新資料
a \
NSString *identifier [email protected]"cell";
b\
UITableViewCell *cell =[tableview dequeueReusableCellWithIdentifier:identifier];
c\
if(cell ==nil){
cell =[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
d\
cell.textLabel.text =model.name;
}
五、模型的嵌套
1)在carmodel中匯入 innermodel
if(self =[super init])
{
[self setValuesForKeyWithDictionary:dict];
ning 一直反應interface中沒定義InnerCarinitWithDictionary方法 匯入innercarmodel
// InnerCarModel *innerModel =[[InnerCarModel alloc] InnerCarinitWithDictionary寫法是錯誤的導致,不需要alloc
NSMutableArray *muArray =[NSMutableArray array];
for(NSDictionary *dict in self.cars)
{
InnerModel *model =[InnerModel InnerinitWithDictionary];
[muArray addObject:model];
}
self.cars =muArray;
return self;
}
2)在tableview中顯示
組數:self.dataArray.count
行數:CarModel *model =self.dataArray[section];
return model.cars.count;
內容:
CarModel *model =self.dataArray[indexPath.section];
InnerModel *innermodel =model.cars[indexPath.row];
六、cell的編輯,增刪
Objective-c——UI基礎開發第六天(UITableView)