UITableView常见用法以及注意细节

UITableView用途广泛,而且多样化,其中有一些不太常用,用到的话需要去查资料,今天边用边记录下来

*第一发:UITableViewCell 分割线

1. 分割线样式

  [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];

2. 分割线长度

  self.tableView.separatorInset = UIEdgeInsetsMake(0, 20, 0, 20);

3. 分割线颜色

  self.tableView.separatorColor = [UIColor blueColor];

3. 自定义分割线

  1.设置隐藏分割线

  [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];

  2.在Cell中重写- (void)drawRect:(CGRect)rect方法

  - (void)drawRect:(CGRect)rect

  {

      CGContextRef context = UIGraphicsGetCurrentContext();

      CGContextSetFillColorWithColor(context, [UIColor clearColor].CGColor);

      CGContextFillRect(context, rect);

      //上分割线,

      CGContextSetStrokeColorWithColor(context, [UIColor grayColor].CGColor);

      CGContextStrokeRect(context, CGRectMake(5, -1, rect.size.width - 10, 1));

          //下分割线

      CGContextSetStrokeColorWithColor(context, [UIColor grayColor].CGColor);

      CGContextStrokeRect(context, CGRectMake(24, rect.size.height, rect.size.width - 48, 1));

  }

*第二发:UITableViewCell重要的属性

 1.是否可以点击

  self.tableView.allowsSelection = NO;

 2.点击cell显示样式

  cell.selectionStyle = UITableViewCellSelectionStyleBlue;

 3.点击cell不变色

  cell.selectionStyle = UITableViewCellSelectionStyleNone;

 4.cell右边图标

     cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

  另:cell.accessoryType =  UITableViewCellAccessoryDetailDisclosureButton  cell的右侧是个button,通常会自定义button

  同时附上点击触发相应代理方法

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

     {

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

      }

 4.//滚动  弹簧效果

    self.tableView.scrollEnabled = NO;

    self.tableView.bounces = NO;

 5.//不显示没内容的cell

    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];

*第三发:UITableView常用方法

 1. //cell展示数据,向后缩进

  - (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath

     {

           if (indexPath.row == 0) {

            return 10;

      }

        return 0;

       }

  2.//用来指定cell的触发事件

    - (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath

    {

      //点击第一个cell 没有反应

        if (indexPath.row == 0) {

           return nil;

      }

       return indexPath;

    }

 

 

 

 

 

注意点:

  1.当有多个section时,用UITableViewStyleGrouped,因为经常用UITableViewStylePlain,所以上次遇到问题百思不得其解

  2.代理注意不要忘记了,经常出现代理方法不走,记得回头看看哦

  3.UITableView风格两种样式的不同 UITableViewStyleGrouped头视图不会悬浮,会跟着view一起滚动,特别是两个section的间距,第一系统有默认间距,第二需要同时调用设置header和footer的代理间距,且不能设置为0,可以0.1;而UITableViewStylePlain悬浮最上面,多用于通讯录。

 

 

//时间有限,用到再写,持续更新中.......

 

posted @ 2015-10-17 11:29  fengkuangIT  阅读(254)  评论(0编辑  收藏  举报