iOS tableView全面詳解


1.tableView有兩種展現形式,UITableViewStylePlain和UITableViewStyleGrouped

創建tableView

self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH,SCREEN_HEIGHT-44) style:UITableViewStylePlain];

2.設置tableView的代理,必須實現的兩個方法設置cell的內容和cell的高度,否則會出現崩潰的現象。

    self.table.dataSource = self;

    self.table.delegate = self;

3.如果有尾部視圖或者頭部視圖,還要設置self.tableView.tableHeaderView和self.tableView.tableFooterView

 //顯示有多少組

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 

//顯示每組的頭部

-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

//顯示每組的尾部

-(UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section

//顯示每組的尾部標題有多高

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{

      return CGFLOAT_MIN;

}

//顯示每組的頭部標題有多高

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section

{

      return CGFLOAT_MIN;

}

//顯示每組頭標題名稱

-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section

//顯示每組尾標題名稱

-(NSString*)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section

//顯示每組有多少行

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 

//顯示每組的行高

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

//顯示內容

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

 //打開或關閉編輯功能

    if ([self.table isEditing]) {

        [self.table setEditing:NO animated:YES];

    }else{

        [self.table setEditing:YES animated:YES];

    }

//對應的行是否可以被編輯

-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath 

//設置編輯風格

-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{

    UITableViewCellEditingStyleNone,

    UITableViewCellEditingStyleDelete,刪除

    UITableViewCellEditingStyleInsert添加

}

 

//提交編輯(左滑刪除和添加)

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

    

    if (editingStyle == UITableViewCellEditingStyleDelete) {//刪除

        [self.serviceArry removeObjectAtIndex:indexPath.row];

        [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationBottom];//刪除刷新

    }

    else if (editingStyle == UITableViewCellEditingStyleInsert) {//添加

       [datas insertObject:@"ab" atIndex:indexPath.row];

    [self.table insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationBottom];//添加刷新

 

    }

}

//設置是否可以移動

-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath

//提交移動結果

-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{

    //先把要移動的數據保存

    id moveObj = [datas objectAtIndex:sourceIndexPath.row];

    //從數組中刪除要移動的數據

    [datas removeObjectAtIndex:sourceIndexPath.row];

    //把要移動的數據插入到目標位置

    [datas insertObject:moveObj atIndex:destinationIndexPath.row];

}

     

//每行的點擊事件

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM