关于UITableView使用

来源:互联网 发布:mac cad怎么样卸载软件 编辑:程序博客网 时间:2024/06/05 17:47

七。UITableView使用


1. UITableView的创建

//UITableView 继承于 UIScrollView

//UITableViewStylePlain默认样式

//UITableViewStyleGrouped 自动分段

//新建一个UITableView   

UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];


//设置代理

//外观

tableView.delegate = self;

//数据

tableView.dataSource = self;


#pragma mark – UITableViewDelegate

/*实现代理的方法

  1. 1 设置段(可选,默认一行)
  2. 2 设置行
  3. 3 设置每一行Cell的样式

其他.设置行高

设置段头段尾

*/

//设置段 默认是返回是1

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

    return 3;

}


//设置一段有多少行

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

{

    if (section == 0) {

        return 2;

    }

    return 10;

}


//设置cell

//cell返回是根据段和行来确定

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

{

    //indexPath 有一个类别专门为UITableView服务的

    //indexPath 类别中有两个属性 section 代表tabbleView的段 row代表tabbleView的行

    

//    NSLog(@"%ld",indexPath.row);

    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];

    //设置副标题

    cell.detailTextLabel.text = @"副标题";

    //设置图片

    cell.imageView.image = [UIImage imageNamed:@"001@2x"];

    //设置主标题

    cell.textLabel.text = [NSString stringWithFormat:@"%ld,%ld",indexPath.section,indexPath.row];

    

    //cell上面还有一层视图 contentViewcell是一样大小

    cell.contentView.backgroundColor = [UIColor redColor];

    

return cell;

--------复用创建cell,节省资源------------

static NSString *signCell = @"cell";

    

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:signCell];

    

    if (cell == nil) {

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:signCell];

    }

------------------------------------------


}


//设置cell的行高系统默认是44

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

{

    return 100;

}


//选中指定行(cell

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

{

    //indexPath.row和数组的下标是一致的

    NSLog(@"index == %ld",indexPath.row);

//    NSLog(@"title == %@",_dataArr[indexPath.row]);

    SecondViewController *second = [[SecondViewController alloc] init];

    second.name = _dataArr[indexPath.row];

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


}


#pragma mark --- option可选择函数

//处理信息按钮的事件

- (void)tableView:(UITableView *)tableView

accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath

{

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"您的余额已不足请及时充值" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];

    [alert show];

}


//定制删除按钮

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath

{

    return @"删除";

}


//指定行编辑

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

{

    if (indexPath.row == 0) {

        return NO;

    }

    return YES;

}


//删除/插入

- (void)tableView:(UITableView *)tableView

commitEditingStyle:(UITableViewCellEditingStyle)editingStyle

forRowAtIndexPath:(NSIndexPath *)indexPath

{

    if (editingStyle == UITableViewCellEditingStyleDelete) {

        

        //1.先删除数据源中的元素

        [_dataArr removeObjectAtIndex:indexPath.row];

        

        //2.处理UI界面

        [_tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];

    }else

    {

        //1.先插入到数据源中

        [_dataArr insertObject:@"南极" atIndex:indexPath.row];

        

        //2.处理UI界面

        [_tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];

    }

}


//设置编辑模式(删除或者增加)

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

{

//想进行删除或者插入,必须先选择模式

//插入模式

//    return UITableViewCellEditingStyleInsert;


//删除模式

    return UITableViewCellEditingStyleDelete;


}


//移动cell

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

{

    //对数据源中的元素交换位置

    //sourceIndexPath 要交换的元素

    //destinationIndexPath 目标元素

    

    [_dataArr exchangeObjectAtIndex:sourceIndexPath.row withObjectAtIndex:destinationIndexPath.row];

    

}



2.表头和段头


表头和段头(还有cell)都是一个UIView,可以自定义,也可以用系统的。


GLScrollView类中定义了表头的样式

//设置表头

    GLScrollView *scrollView = [[GLScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 250)];

    tableView.tableHeaderView = scrollView;

    

    //设置表头

    UIActivityIndicatorView *act = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];

    act.frame = CGRectMake(0, 0, 200, 50);

    act.color = [UIColor redColor];

    

    [act startAnimating];

    

    tableView.tableFooterView = act;

    

}


//设置段头段尾

//设置段头高度

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

{

    return 50;

}


//设置段尾高度

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

//{

//    return 10;

//}


//自定义段头

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

{

    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];

    button.tintColor = [UIColor blackColor];

    button.backgroundColor = [UIColor orangeColor];

    [button setTitle:[NSString stringWithFormat:@"%ld段段头",section] forState:UIControlStateNormal];

    return button;

}



3. UITableView搜索功能

原理,对数据源进行搜索,取得一个搜索结果到一个数组当中。

 1)搜索满足几点,

1,有一个数据源。2,创建搜索控制器。3,有一个接收搜索结果的数组。4,用一个Bool类型储存是否在搜索状态。

NSMutableArray *_dataArrM; //数据源

    UISearchController *_searchVC; //搜索控制器

    NSMutableArray *_searchArrM; //搜索结果的数组

    BOOL _isSearch; //是否在搜索状态


   2)创建搜索控制器

实现代理<UISearchControllerDelegate,UISearchResultsUpdating>

//参数代表搜索结果哪个控制器显示

    //nil 当前视图控制器显示

    _searchVC = [[UISearchController alloc] initWithSearchResultsController:nil];

    

//    是否在搜索的时候使底色变暗

    _searchVC.dimsBackgroundDuringPresentation = NO;

    

    //适配搜索栏 自适应

    [_searchVC.searchBar sizeToFit];

    

    //设置提示文字

    _searchVC.searchBar.placeholder = @"请输入关键词";

    

    //设置代理

    //搜索结束时候调用

    _searchVC.delegate = self;

    

    //点击搜索时调用

    _searchVC.searchResultsUpdater  = self;

    

    //设置颜色

    _searchVC.searchBar.barTintColor = [UIColor orangeColor];

    

    //初始化搜索数组

    _searchArrM = [NSMutableArray array];


实现代理

#pragma mark --- UISearchControllerDelegate

//点击搜索输入都会触发

- (void)updateSearchResultsForSearchController:(UISearchController *)searchController

{

    //1.进入搜索状态

    _isSearch = YES;

    

    //2.搜索关键词查找数据源中是否存在

    //遍历数据源

    for(Person *p1 in _dataArrM)

    {

        //搜索的关键词只要是人名字的字串

        if ([p1.name rangeOfString:_searchVC.searchBar.text].location != NSNotFound)

        {

            [_searchArrM addObject:p1];

        }

    }

    

    //3.刷新tableview

    [_tableView reloadData];

}


//结束搜索时触发

- (void)didDismissSearchController:(UISearchController *)searchController

{

    //1.搜索状态置为N0

    _isSearch = NO;

    

    //2.清除搜索结果数组

    [_searchArrM removeAllObjects];

    

    //3.刷新tabbleView

    [_tableView reloadData];

    

}


4.自定义的cell,表头,段头

要注意的是,自行创建类,然后这个类要继承和其相对应的。

如:

  1. 1. cell要继承UITableViewCell

init方法可以用

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier

2.表头,表尾,段头,段尾 要继承UIView


0 0
原创粉丝点击