TableView


表视图

属性:

frame

数据源代理 .dataSource

表视图代理.delegate

加表视图的时候要加两个协议<UITableViewDelegate,UITableViewDataSource>

然后设置上面两个代理 = self

storyBoard里面右键点住Table View拖到界面小黄球

表头视图 .tableHeaderView

style Grouped Plain

这个只能在创建的时候设置

UITableView *tv = [[UITableViewalloc]initWithFrame:self.view.boundsstyle:UITableViewStyleGrouped];

重新加载

[self.showResultVC.tableViewreloadData];

方法:

这两个经常要写

不足一页时去掉多余的行

_tableView.tableFooterView = [UIViewnew];

点中时取消高亮

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

    [tableView deselectRowAtIndexPath:indexPathanimated:YES];

}

设置行高

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

{

    return70;

}

ios8之后出现,根据内容自动调整cell高度

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath{

    returnUITableViewAutomaticDimension;

}


表视图单元 TableViewCell

主要内容 .textLabel.text

详细内容.detailTextLabel.text

图片.imageView.image

写在cell .m文件里的一个方法

监听tableView的变化

- (void)layoutSubviews{

    [superlayoutSubviews];

    self.redLb.frame =CGRectMake(self.contentView.frame.size.width - 60 - 20, (self.contentView.frame.size.height - 30)/2,60, 30);

}

辅助视图 

系统版.accessoryType =UITableViewCellAccessoryDetailDisclosureButton;

自定义版 .accessoryView=view;

当然要自己在view里面加东西。。

辅助视图方法

//点击圆圈i以外的部分的响应

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

{

    NSLog(@"didSelect");

}


//只有点击圆圈i时才响应

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

{

    NSLog(@"点击了圆圈i");

}


两种重用单元格方式

第一种

// 1.先尝试着去队列中取单元格

    UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:@"Cell"];

    // 2.如果没有取到,再新建

    if (cell ==nil) {

        cell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:@"Cell"];

    }

第二种

// 为了让tableView知道自动创建的单元格什么样

    // 提前将单元格类注册一下

    // [xxx class]返回该类型的描述信息

    [self.tableViewregisterClass:[UITableViewCellclass] forCellReuseIdentifier:@"Cell1"];

在第三问中

UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:@"Cell2"forIndexPath:indexPath];

设置分区头

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

{

    return@"分区头:城市名";

}

设置TableViewCell中内容tag值

用来检测控件是否存在

UILabel *label = (UILabel *)[cell.contentViewviewWithTag:1];

    if (label ==nil) {//没找到再新建

        label = [[UILabelalloc]init];

        //label设置tag

        label.tag =1;

        [cell.contentViewaddSubview:label];

    }


表视图的编辑

先设置导航右上角的按钮变成编辑

self.navigationItem.rightBarButtonItem = [[UIBarButtonItemalloc]initWithTitle:@"编辑"style:UIBarButtonItemStylePlaintarget:selfaction:@selector(clickEditButton:)];

//点击编辑按钮,切换表格的编辑模式

-(void)clickEditButton:(UIBarButtonItem *)item

{

    [self.tableViewsetEditing:!self.tableView.editinganimated:YES];

    

    item.title =self.tableView.editing?@"完成":@"编辑";


}


#pragma mark - 表格的编辑功能


//1:当前行是否可编辑

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

{

    if (indexPath.row ==0) {

        returnNO;

    }else{

        returnYES;

    }

}


//2:当前行的编辑样式是什么

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

{

    if (indexPath.row ==self.allCitys.count-1)

    {

        returnUITableViewCellEditingStyleInsert;

    }else{

        returnUITableViewCellEditingStyleDelete;

    }

    //return UITableViewCellEditingStyleDelete|UITableViewCellEditingStyleInsert;

}


//1:点击编辑按钮后做什么

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

{

    if (editingStyle ==UITableViewCellEditingStyleDelete){//点击的是删除按钮

        // 1.先删除行所在的数据模型中的数据

        [self.allCitysremoveObjectAtIndex:indexPath.row];

        

        // 2.更新tableView的显示

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

        

    }else{//点击的是增加按钮

        

        // 1.先将数据增加到数据模型中

        City *city = [[Cityalloc]init];

        city.name =@"test";

        city.population =1000;

        [self.allCitysaddObject:city];

        

        // 2.刷新表格

        NSIndexPath *newIndexPath = [NSIndexPathindexPathForRow:self.allCitys.count-1inSection:0];

        [tableView insertRowsAtIndexPaths:@[newIndexPath]withRowAnimation:UITableViewRowAnimationAutomatic];

        

    }

}


#pragma mark - 行的移动


//1:改行是否可以移动

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

{

    returnYES;

}


//1:移动后做什么

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

{

    //1.按照旧的位置,找到原始的数据

    City *city =self.allCitys[sourceIndexPath.row];

    

    //2.将数据从原位置上移除

    [self.allCitysremoveObjectAtIndex:sourceIndexPath.row];

    

    //3.再将数据按照新的位置插入回数组

    [self.allCitysinsertObject:city atIndex:destinationIndexPath.row];

}


刷新功能

UIRefreshControl *rc = [[UIRefreshControlalloc]init];

表视图控制器一般自带刷新控件

self.refreshControl = rc;

添加功能

[rc addTarget:selfaction:@selector(refresh:)forControlEvents:UIControlEventValueChanged];

- (void)refresh:(UIRefreshControl *)rc{

    if ([rcisRefreshing]) {//正在转

        // 发网络请求读取新数据

        // 当数据接收完毕后,通知系统刷新界面

        // 3秒钟后。将

        [selfperformSelector:@selector(refreshOver:)withObject:@"abc"afterDelay:3];

    }

}


//result参数就是 withObject中传来的“abc”

-(void)refreshOver:(NSString *)result

{

    //先改数据模型

    [self.allStrinsertObject:result atIndex:0];

    //abc显示到表格中

    NSIndexPath *t1 = [NSIndexPathindexPathForRow:0inSection:0];

    NSIndexPath *t2 = [NSIndexPathindexPathForRow:0inSection:1];

    NSIndexPath *t3 = [NSIndexPathindexPathForRow:0inSection:2];

    NSIndexPath *t4 = [NSIndexPathindexPathForRow:0inSection:3];

    [self.tableViewinsertRowsAtIndexPaths:@[t1,t2,t3,t4]withRowAnimation:UITableViewRowAnimationAutomatic];

    //修改refreshControl为结束刷新状态

    [self.refreshControlendRefreshing];

}

设置索引

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView

{

    return@[@"A",@"B",@"C",@"D"];

}

搜索

需要新建一个TableViewController显示搜索结果

searchController

//创建搜索控制器的实例

    self.searchController = [[UISearchControlleralloc]initWithSearchResultsController:self.showResultVC];

    

    //设置搜索条的尺寸为自适应

    [self.searchController.searchBarsizeToFit];

    

    //设置搜索条中的分段类别

    self.searchController.searchBar.scopeButtonTitles = @[@"设备",@"软件",@"其他"];

    

    //为当前表头视图添加searchBar

    self.tableView.tableHeaderView =self.searchController.searchBar;

    

    //设置搜索控制器的结果更新代理对象

    self.searchController.searchResultsUpdater =self;


//允许当前界面上切换展示数据的上下文对象

    //此属性设置为yes,才能在当前界面上切换

    //由另一个表视图来显示搜索结果

    self.definesPresentationContext =YES;

    

    //为了点击seacherBar上的类别按钮时,也能立即响应

    //所以需要设置searchBar的代理

    self.searchController.searchBar.delegate = self;


#pragma mark - UISearchBarDelegate协议

-(void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope

{

    [selfupdateSearchResultsForSearchController:self.searchController];

}


#pragma mark - UISearchrResultUpdating协议


//只要用户在文本框中输入的搜索文本发生变化

//则自动执行此方法

-(void)updateSearchResultsForSearchController:(UISearchController *)searchController

{

    //根据用户输入的文字,逐一比对商品信息

    //将商品信息显示到展示结果的vc

    

    //searchBar中获取用户在文本框中输入的文字

    NSString *searchText = searchController.searchBar.text;

    

    //获取用户选择的类别按钮

    NSInteger selectedButtonIndex = searchController.searchBar.selectedScopeButtonIndex;

    

    //self.allProducts中逐一比对数据

    NSMutableArray *resultArray = [NSMutableArrayarray];

    for (Product *pin self.allProducts) {

        //判断商品名种包含输入的搜索文本的范围

        //@"abcdefg"    @"def"

        //length:3 location:3

        NSRange range = [p.namerangeOfString:searchText];

        //如果名称匹配,且类别相同

        //则将此商品记录到结果数组中

        if (range.length>0 && p.type==selectedButtonIndex) {

            [resultArray addObject:p];

        }

        

    }

    

    //将要展示的数据结果showResultVC传过去

    self.showResultVC.resultArray = resultArray;

    

    //更新视图显示数据

    [self.showResultVC.tableViewreloadData];

}









评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值