码迷,mamicode.com
首页 > 其他好文 > 详细

UITableView笔记

时间:2015-10-08 14:53:28      阅读:266      评论:0      收藏:0      [点我收藏+]

标签:

UITableView


 /*

     UITableView组成:

        1.tableView 表格 组成

            Section段组成

                1.段头HeaderView

                2.段尾FooterView

                3.NUITableViewCell

        2.tableHeaderView 表头

        3.tableFooterView 表尾

     */

    

    /*

     模型: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];


}


UITableView笔记

标签:

原文地址:http://my.oschina.net/u/2448151/blog/514128

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!