UITableView是app開(kāi)發(fā)中常用到的控件夕冲,功能很強(qiáng)大氮兵,多用于數(shù)據(jù)的顯示。
初始化tableview后歹鱼,要想顯示數(shù)據(jù)泣栈,就必須遵守tableview的數(shù)據(jù)源代理,而且實(shí)現(xiàn)以下方法否則程序會(huì)崩潰:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
該方法決定了tableview的每一行顯示什么內(nèi)容,返回值為UITableViewCell南片,可以通過(guò)設(shè)置UITableViewCell的各種屬性(image掺涛,label..)從而達(dá)到想要的效果,或者自定義Cell疼进,關(guān)于自定義cell在后面會(huì)做相關(guān)介紹
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
該方法決定了每一組(section)中有幾個(gè)cell
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
該方法決定了tabview顯示幾組薪缆,這個(gè)方法不一定要實(shí)現(xiàn),如果不實(shí)現(xiàn)默認(rèn)顯示一組
如果想要實(shí)現(xiàn)對(duì)tableview 的操作(如:點(diǎn)擊每一個(gè)cell能獲得相應(yīng))則必須遵守UITableViewDelegate協(xié)議伞广,成為代理
以下是幾個(gè)比較常用的方法
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
該方法可以設(shè)置每個(gè)cell 的高度
- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section; ?
該方法可以自定義每個(gè)組的頭部(圖片拣帽,按鈕等等)
- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section;
該方法可以自定義每個(gè)組的尾部(圖片,按鈕等等)
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
該方法獲取當(dāng)前點(diǎn)擊的cell的indexPath(結(jié)構(gòu)體嚼锄,包括了section和row)
如何復(fù)制粘貼剪切一個(gè)cell
遵守UITableViewDelegate協(xié)議减拭,并實(shí)現(xiàn)以下方法
該方法決定在長(zhǎng)按cell后 是否顯示 復(fù)制粘貼 菜單
-(BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath{
return YES;
}
該方法決定 菜單上顯示什么按鈕
-(BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender{
//? ? 不顯示剪切的按鈕
if (action==@selector(copy:)||action==@selector(paste:)) {
return YES;
}else{
return NO;
}
}
該方法決定 點(diǎn)擊菜單上的復(fù)制粘貼按鈕 后干啥
-(void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender{
if (action==@selector(copy:)) {
? ? ? ?NSLog(@"copy!");
}else if(action==@selector(paste:)){
? ? ? ?NSLog(@"paste!");
}
cell 的編輯
設(shè)置cell能否被編輯
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
return YES;
}
設(shè)置刪除按鈕的文字
-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
return @"刪除";
}
實(shí)現(xiàn)cell 的編輯,通過(guò)editingStyle做不同操作
editingStyle:是編輯模式是枚舉類型灾票,有以下三種
UITableViewCellEditingStyleNone,
UITableViewCellEditingStyleDelete,
UITableViewCellEditingStyleInsert
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
? }
UITableViewCell的使用
創(chuàng)建TableViewCell
UITableViewCell *cell=[UITableViewCell alloc]initWithStyle:<#(UITableViewCellStyle)#> reuseIdentifier:<#(nullable NSString *)#>]
reuseIdentifier:重用標(biāo)識(shí)符峡谊,定義重用標(biāo)識(shí)符可以實(shí)現(xiàn)cell的復(fù)用、
TableViewCell的重用:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"newscell"];
//復(fù)用重用標(biāo)識(shí)符為newscell的cell
if (cell == nil) {
//沒(méi)有重用標(biāo)識(shí)符為newsreel的cell 創(chuàng)建一個(gè)cell并且設(shè)置重用標(biāo)識(shí)符
cell=[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"newsreel"];
}
//注冊(cè)tableviewcell刊苍,這樣就不需要對(duì)cell進(jìn)行為空判斷
[tableView registerClass:<#(nullable Class)#> forCellReuseIdentifier:<#(nonnull NSString *)#>]
//使用xib創(chuàng)建cell既们,使用下面的方法進(jìn)行tableviewcell的注冊(cè)
[tableView registerNib:<#(nullable UINib *)#> forCellReuseIdentifier:<#(nonnull NSString *)#>]