UITableView的使用

来源:互联网 发布:中彩票 知乎 编辑:程序博客网 时间:2024/05/19 09:51

UITableView的使用

如何使用UITabelView

要使用UITableView,就要我们自定义的类继承于UITableViewController(IOS中的MVC设计模式),
然后我们就可以继承处理UITableView的各种方法了。Apple对方法定义名很好理解,看到方法名也就
知道起什么作用了。

一些主要的方法

UITableView主要用于相同数据的展示,是UITabelViewCell的集合,每个UITableViewCell用来展示数据。

为了定位每个UITableViewCell,又有了section,row2个变量,具体的如下图所示

主要的方法:

  • 返回section的个数
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  • 设置每个section的标题
    - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
  • 返回每个section中row的个数,其实就是UITableViewCell的个数。
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  • 处理每个UITableViewCell
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

    每个UITabelViewCell如何展示,indexPath包含了UITableView的section,row信息,可以通过indexPath.section,indexPath.row
    获得。
  • 返回每个UITableViewCell的高度
    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  • UITableViewCell的选中事件
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  • 滑动删除的事件
    - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
  • 滑动删除 的 删除按钮
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_0){    return @"删除";}
  • 取消选择效果
- (void)deselectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated;

每个UITableViewCell点击以后的事件,push一个新的view,刷新数据?在这里添加下处理就OK。

例子展示

根据数据来确定section,row的值,用来展示数据,只要确定好section,及row的内容。

处理每个cell

我们就可以根据数据在每个cell中添加我们想要的处理。看一下在药品指南中的代码

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    static NSString *CellIdentifier = @"Cell";    UITableViewCell    *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];    // 设置cell无选中效果    cell.selectionStyle = UITableViewCellSelectionStyleNone;    // 初始化tableview的内容    if (indexPath.section!=0) {        CGSize size = [self getLabeSize:[_theDic objectForKey:[_theArray objectAtIndex:indexPath.section - 1]] Width:300];        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, size.width, size.height+10)];        [self initLabel:label];        [label setText:[_theDic objectForKey:[_theArray objectAtIndex:indexPath.section -1]]];        [cell.contentView addSubview:label ];    }    else    {        。。。。。。。。。。。        。。。。。。。。。。。    }    return cell;}

根据特定的条件,来初始化UITableViewCell。可以自定义UILabel,UIButton,UIImageView等,添加到UITableViewCell上。
一切取决于你的数据特点,以及你想要的效果。
比如说实现UITableView隔行变色,只要判断indexPath.row的值,然后设置cell的背景色就行,如果你动态添加了UILabel等之类的,也要
做相应的颜色变换。

cell.selectionStyle = UITableViewCellSelectionStyleNone;

用来设置cell没有选中的效果。

滑动删除的实现

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{    if (editingStyle == UITableViewCellEditingStyleDelete) {        // Delete the row from the data source        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];        //处理数据,然后刷新tableview。    }       else if (editingStyle == UITableViewCellEditingStyleInsert) {        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view    }   }

导航栏右上角添加编辑按钮实现删除

添加按钮,并绑定函数

 UIBarButtonItem *editButton = [[UIBarButtonItem alloc]                                   initWithTitle:@"编辑"                                    style:UIBarButtonItemStyleDone                                   target:self                                   action:@selector(edit)];    self.navigationItem.rightBarButtonItem = editButton;    [editButton release];    _tag = YES;

函数实现

-(void) edit{if (_tag == YES) {[self.tableView setEditing:YES animated:YES];//设置导航栏上右边的编辑按钮UIBarButtonItem *editButton = [[UIBarButtonItem alloc]  initWithTitle:@"完成"   style:UIBarButtonItemStyleBordered  target:self  action:@selector(edit)];self.navigationItem.rightBarButtonItem = editButton;_tag = NO;}else {[self.tableView setEditing:NO animated:YES];//设置导航栏上右边的编辑按钮UIBarButtonItem *editButton = [[UIBarButtonItem alloc]  initWithTitle:@"编辑"   style:UIBarButtonItemStyleBordered  target:self  action:@selector(edit)];self.navigationItem.rightBarButtonItem = editButton;_tag = YES;}}

快速定位的实现

快速定位的帖子

UITableView自带了快速索引的办法,我们要做的是处理好数据,获得索引序列,以及对应内容。

3个重要的方法

返回的是索引的序列。

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

初始化每个section的title,

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

返回的是section title集合中与索引序列字母相同的section的位置。

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

处理好数据,然后就可以实现快速定位。

TableView与其他控件共存

经常在一个页面中,需要在上部显示一些控件,如:搜索框等,下部显示TableView。
对于这种情况,即使用ViewController即可,最底层是个普通的View,在View上分别添加搜索框与TableView。
在xib上,代码中都可以做到。需要注意,对于TableView的delegate与dataSource需要设置为self,在xib上直接拉线即可,在代码中,直接

_evaTable = [[UITableView alloc] initWithFrame:CGRectMake(0,70,320,346)];//UITableViewStyleGrouped        _evaTable.delegate = self;        _evaTable.dataSource = self;        [self.view addSubview:_evaTable];

添加自定义的背景图片

UIImageView *imageview = [[UIImageView alloc] initWithFrame:self.myTable.backgroundView.frame];    [imageview setImage:[UIImage imageNamed:@"bb2.png"]];    self.myTable.backgroundView = imageview;

还要设置uitableviewcell的背景色为透明色。
这样tableview背景图使我们自定义的图片

原创粉丝点击