UITableView筆記

UITableView數組


 /*函數

     UITableView組成:spa

        1.tableView 表格 組成3d

            Section段組成代理

                1.段頭HeaderVieworm

                2.段尾FooterView繼承

                3.NUITableViewCell索引

        2.tableHeaderView 表頭事件

        3.tableFooterView 表尾rem

     */

    

    /*

     模型:ImageName  Name  FriendModel

     段數組:3個模型 一個模型對應一行 sectionArr

     大數組:3個段數組  dataArr

     使用dataArr 將全部數據對接到表格視圖的代理中

     */

//    return _dataArr.count; 返回多少段

//    return [_dataArr[section] count] 每一段有多少行

//    每一行的數據如何經過_dataArr對接 indexPath.section indexPath.row




//建立UITableView

    //繼承與UIScrollview

    UITableView *tabview = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];

設置代理:

 //設置代理(處理tableView一些功能,選擇,編輯,行高,section Header/footer..)

    tabview.delegate = self;

    //數據代理, 數據的來源(cell行數, 顯示的內容)

    tabview.dataSource = self;

//設置分割線

    tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;

    tableView.separatorColor = [UIColor blackColor];

    tableView.separatorInset = UIEdgeInsetsMake(0, 50, 0, 10);


必須遵照協議<UITableViewDelegate,UITableViewDataSource>

 必須實現這兩個協議方法:

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


}


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


//indexPath.section    當前的組

    //indexPath.row        當前的行


//第一種方式<非註冊方式能夠選擇cell的style>

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:TabCellreuseId];

    if (cell==nil) {

        cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleValue1) reuseIdentifier:TabCellreuseId];

        

    }

    //第二種方式

#if 0

//從複用池獲取cell, 這個前面必需要註冊

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:TabCellreuseId forIndexPath:indexPath];

#endif

//讓箭頭出現

 cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;


}

//返回UIView做爲header/footer

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

//先註冊

//從複用池取出headerView,前面必需要註冊

    UITableViewHeaderFooterView *headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:TabHeaderId];

//在次可設置header的背景色,文字headerView.contentView.backgroundColorheaderView.textLabel.text

    

     //headerView添加手勢

    

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapOne:)];

    [headerView addGestureRecognizer:tap];

    

    return headerView;

}

//選中某一個cell的時候, 會調用這個函數//去下一個界面

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

    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    

    DetailViewController *detailVC = [[DetailViewController alloc] init];

    

    [self.navigationController pushViewController:detailVC animated:YES];


}


———————索引———————

//返回數組

-(Nsarray *)sectionIndex…..

//指定索引的位置

-(NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index{

    if (index == 0) {

        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:9 inSection:0];

        

        [tableView scrollToRowAtIndexPath:indexPath atScrollPosition:(UITableViewScrollPositionMiddle) animated:YES];

        return -1;

    }

    return index-1;

}

———————編輯———————

// 可否編輯(Bool)

//如何編輯(void)....comitEdit

//首先獲取對應的secttion數據

    NSMutableArray *mmuarr = _dataArr[indexPath.section];

    if (editingStyle == UITableViewCellEditingStyleDelete) {

        //刪除數組元素

        [mmuarr removeObjectAtIndex:indexPath.row];

        //經過tableView

        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimationAutomatic)];

    }else if (editingStyle == UITableViewCellEditingStyleInsert){

    

        [mmuarr insertObject:@"I'm new" atIndex:0];

        

        [tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimationAutomatic)];

    }



//多行刪除(Item左右按鈕Edit/trach)

//刪除響應事件

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

   //多項編輯模式

        return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert


}


-(void)deleteBBIClick:(UIBarButtonItem *)bbtn{

//獲取所有選中的cell的位置

  NSArray *indexPaths = [self.tableView indexPathsForSelectedRows];

//從數據源裏面刪

    //將數據保存

    NSMutableArray *muarr = [[NSMutableArray alloc] init];

    for (NSIndexPath *obj in indexPaths) {

        [muarr addObject:_dataSource[obj.row]];

    }

    [_dataSource removeObjectsInArray:muarr];

    

    //tableView

    

    [self.tableView  deleteRowsAtIndexPaths:indexPaths withRowAnimation:(UITableViewRowAnimationAutomatic)];


}


———移動——

 —(bool)可否移動

//返回目標的位置

- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath

{

    //若是返回  sourceIndexPath 不能移動

    //若是返回  proposedDestinationIndexPath 能夠移動

    

    //在同一組裏面

    if (sourceIndexPath.section == proposedDestinationIndexPath.section) {

        return proposedDestinationIndexPath;

    }

    //不然, 不能跨組移動

    return sourceIndexPath;

}

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

{

    //原數組

    NSMutableArray *sourceArray = _dataSource[sourceIndexPath.section];

    //目標數組

    NSMutableArray *destArray = _dataSource[destinationIndexPath.section];

    

    //先保存原來的數據

    NSString *string = sourceArray[sourceIndexPath.row];

    

    //刪除原來數組裏面的數據

    [sourceArray removeObjectAtIndex:sourceIndexPath.row];

    

    //將數據插入到新的數組裏面

    [destArray insertObject:string atIndex:destinationIndexPath.row];


}

相關文章
相關標籤/搜索