IOS TableView&ScrollView

来源:互联网 发布:淘宝网平板电脑的架子 编辑:程序博客网 时间:2024/06/06 01:00

TableView


  • tableView删除默认选中行的背景色
[self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow]animated:YES];
  • tableView的背景色
 setTableView.backgroundView = [[UIView alloc]init];    setTableView.backgroundColor = [UIColor redColor];[UIColor colorWithRed:241.0/255.0 green:241.0/255.0 blue:241.0/255.0 alpha:1];
  • tableViewCell的分割线
setTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  • tableViewCell的分割线颜色
[theTableView setSeparatorColor:[UIColor xxxx ]];
  • tableViewCell选中时背景色
cell.selectedBackgroundView = [[[UIView alloc] initWithFrame:cell.frame] autorelease];cell.selectedBackgroundView.backgroundColor = [UIColor xxxxxx]; 
  • tableViewCell选中时背景
cell.selectedBackgroundView = [[[UIImageView alloc] initWithImage:[UIIma`ge imageNamed:@"cellart.png"]] autorelease];  还有字体颜色   cell.textLabel.highlightedTextColor = [UIColor xxxcolor];  [cell.textLabel setTextColor:co

-设置tableViewCell的背景颜色

- (void)tableView: (UITableView*)tableView willDisplayCell: (UITableViewCell*)cell forRowAtIndexPath: (NSIndexPath*)indexPath{    cell.backgroundColor = indexPath.row % 2?[UIColor colorWithRed: 240.0/255 green: 240.0/255 blue: 240.0/255 alpha: 1.0]: [UIColor whiteColor];    cell.textLabel.backgroundColor = [UIColor clearColor];    cell.detailTextLabel.backgroundColor = [UIColor clearColor];}
  • tableView与tableVieCelll的圆角效果
yourTableView.layer.cornerRadius = 20;if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7){        UIView *cellBackgroundView = [[UIView alloc] init];        //cellBackgroundView.layer.cornerRadius = 8.0;//是否加圆角        cellBackgroundView.layer.borderWidth = 0.57;//边框宽度        cellBackgroundView.layer.borderColor = [UIColor colorWithRed:0.92 green:0.92 blue:0.92 alpha:1.0].CGColor;        cell.backgroundView = cellBackgroundView;    }
  • tableViewGroup没有数据是不显示cell的

  • 在cell里面定义一个view把view的背景设为tableView的背景色,这样就出现间隔了

UIView *view = [[UIView alloc initWithFrame:CGRectMake(0, 0, 320, 10)]; view.backgroundColor = [UIColor colorWithRed:74.0/255 green:56.0/255 blue:58.0/255 alpha:1.0]; [cell.contentView addSubview:view]; [view release];
  • tableView内容重复
 NSArray*subviews = [[NSArray alloc]initWithArray:cell.contentView.subviews];    for (UIView *subview in subviews) {        [subview removeFromSuperview];    }
  • 可以用group类型,height Header返回0.0001可以没有header

    • 界面设置
      这里写图片描述

    setTableView= [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, self.view.frame.size.height) style:UITableViewStyleGrouped];
    setTableView.separatorStyle = UITableViewCellSeparatorStyleNone;

  • (CGFloat)tableView:(UITableView )tableView heightForRowAtIndexPath:(NSIndexPath )indexPath{
    return 44;
    }

  • (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0||section == 3) {
    return 2;
    }
    return 1;
    }

  • ios7风格是顶到左右,加下面的方法和一避免CGRect bounds 调可以让cell的线超左右

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{    if ([cell respondsToSelector:@selector(tintColor)]) {        if (tableView == self.tableView) {            CGFloat cornerRadius = 5.f;            cell.backgroundColor = UIColor.clearColor;            CAShapeLayer *layer = [[CAShapeLayer alloc] init];            CGMutablePathRef pathRef = CGPathCreateMutable();            CGRect bounds = CGRectInset(cell.bounds, 10, 0);            BOOL addLine = NO;            if (indexPath.row == 0 && indexPath.row == [tableView numberOfRowsInSection:indexPath.section]-1) {                CGPathAddRoundedRect(pathRef, nil, bounds, cornerRadius, cornerRadius);            } else if (indexPath.row == 0) {                CGPathMoveToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMaxY(bounds));                CGPathAddArcToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMinY(bounds), CGRectGetMidX(bounds), CGRectGetMinY(bounds), cornerRadius);                CGPathAddArcToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMinY(bounds), CGRectGetMaxX(bounds), CGRectGetMidY(bounds), cornerRadius);                CGPathAddLineToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMaxY(bounds));                addLine = YES;            } else if (indexPath.row == [tableView numberOfRowsInSection:indexPath.section]-1) {                CGPathMoveToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMinY(bounds));                CGPathAddArcToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMaxY(bounds), CGRectGetMidX(bounds), CGRectGetMaxY(bounds), cornerRadius);                CGPathAddArcToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMaxY(bounds), CGRectGetMaxX(bounds), CGRectGetMidY(bounds), cornerRadius);                CGPathAddLineToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMinY(bounds));            } else {                CGPathAddRect(pathRef, nil, bounds);                addLine = YES;            }            layer.path = pathRef;            CFRelease(pathRef);            layer.fillColor = [UIColor colorWithWhite:1.f alpha:0.8f].CGColor;//可以调颜色            if (addLine == YES) {                CALayer *lineLayer = [[CALayer alloc] init];                CGFloat lineHeight = (1.f / [UIScreen mainScreen].scale);                lineLayer.frame = CGRectMake(CGRectGetMinX(bounds)+10, bounds.size.height-lineHeight, bounds.size.width-10, lineHeight);                lineLayer.backgroundColor = tableView.separatorColor.CGColor;                [layer addSublayer:lineLayer];            }            UIView *testView = [[UIView alloc] initWithFrame:bounds];            [testView.layer insertSublayer:layer atIndex:0];            testView.backgroundColor = UIColor.clearColor;            cell.backgroundView = testView;        }    }}
  • 调节tableView上cell上下左右间距,只要创建一个UITableViewCell的子类,重写一下setfrme方法即可以调整。
-(void)setFrame:(CGRect)frame{    frame.origin.x -= 30;    frame.size.width += 55;   [super setFrame:frame];}
tableviewcell懒加载位置问题查了一资料,可能是历史遗留问题,所以tableViewCell在初始化的时候宽高默认是320*44.只有在布局的时候才会调整到设置的高度。所以可以重写layoutSubviews方法。在layoutSubviews里面加载label即可。

QQ分组效果
这里写图片描述

下载地址:http://download.csdn.net/download/u010742414/8650257

  • tableViewCell单选,多选
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{  //单选//    if ([indexPath isEqual:self.currentIndexPath])//    {//        cell.accessoryType = UITableViewCellAccessoryCheckmark;////    }//    else//    {//        cell.accessoryType  = UITableViewCellAccessoryNone;//    }    //单选    if ([indexPath isEqual:self.currentIndexPath])    {        cell.accessoryView.hidden = NO;        UIImageView* accView =[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"icon_address_selected"]];        accView.frame = CGRectMake(2, 200, 35, 35);        cell.accessoryView=accView;    }    else    {        cell.accessoryView.hidden = YES;    }//多选    for (NSIndexPath* index in self.mArraysesected)    {          if (![indexPath isEqual:index])          {                cell.accessoryType = UITableViewCellAccessoryNone;          }        if ([indexPath isEqual:index])        {            cell.accessoryType = UITableViewCellAccessoryCheckmark;            return cell;        }    }    return cell;}}-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{   //单选//    self.lastIndexPath = self.currentIndexPath;//    self.currentIndexPath = indexPath;//    if (self.lastIndexPath)//    {//        [tableView reloadRowsAtIndexPaths:@[self.lastIndexPath] withRowAnimation:UITableViewRowAnimationAutomatic];//    }//    //        [tableView reloadRowsAtIndexPaths:@[self.currentIndexPath] withRowAnimation:UITableViewRowAnimationAutomatic];    //多选    if ([self.mArraysesected containsObject:indexPath])    {        [self.mArraysesected removeObject:indexPath];    }else{        [self.mArraysesected addObject:indexPath];    }    [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];}
  • 用tableViewPaln做这样的效果,N个section,一个行,header设置透明色,plan的heade和footer会跟随移动
    这里写图片描述

TbleviewController ios8 删除时添加多个按钮

ios 新的属性typedef NS_ENUM(NSInteger, UITableViewRowActionStyle) {    UITableViewRowActionStyleDefault = 0,    UITableViewRowActionStyleDestructive = UITableViewRowActionStyleDefault,    UITableViewRowActionStyleNormal} NS_ENUM_AVAILABLE_IOS(8_0);NS_CLASS_AVAILABLE_IOS(8_0) @interface UITableViewRowAction : NSObject <NSCopying>+ (instancetype)rowActionWithStyle:(UITableViewRowActionStyle)style title:(NSString *)title handler:(void (^)(UITableViewRowAction *action, NSIndexPath *indexPath))handler;/////////////////下面实现相关代码////////////////////////////(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{    return YES;}- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{    return  UITableViewCellEditingStyleDelete;}-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{    if (editingStyle == UITableViewCellEditingStyleDelete) {        [self.dataSource removeObjectAtIndex:indexPath.row];        [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];    }}- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{    //设置删除按钮      UITableViewRowAction *deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除"handler:^(UITableViewRowAction *action,NSIndexPath *indexPath) {          [self.dataSource removeObjectAtIndex:indexPath.row];          [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];      }];    //设置收藏按钮    UITableViewRowAction *collectRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"收藏"handler:^(UITableViewRowAction *action,NSIndexPath *indexPath) {        collectRowAction.backgroundColor = [UIColor greenColor];        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"收藏" message:@"收藏成功" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];        [alertView show];    }];    //设置置顶按钮    UITableViewRowAction *topRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"置顶" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {        [self.dataSource exchangeObjectAtIndex:indexPath.row withObjectAtIndex:0];        NSIndexPath *firstIndexPath = [NSIndexPath indexPathForRow:0 inSection:indexPath.section];        [tableView moveRowAtIndexPath:indexPath toIndexPath:firstIndexPath];    }];    collectRowAction.backgroundEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];    topRowAction.backgroundColor = [UIColor blueColor];    collectRowAction.backgroundColor = [UIColor grayColor];    return  @[deleteRowAction,collectRowAction,topRowAction];}

这里写图片描述

TbleviewController 设置滑动范围

TbleviewController 当在一些情境中我们需要我们的tableview 滑动时增大它们的滑动范围时候我们可以调用    self.tableView.contentSize 函数即可 当tableview 滑动时设置它们的contentSize-(void)scrollViewDidScroll:(UIScrollView *)scrollView{        self.tableView.contentSize = CGSizeMake(0,1000);}

tableviewController subclassView 不随着一起滚动

添加一个button 等宽与视图的宽度    tryButton = [UIButtonbuttonWithType:UIButtonTypeCustom];    tryButton.frame =CGRectMake(0,self.view.frame.size.height-44,self.view.frame.size.width,44);    // [tryButton setBackgroundImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];    [tryButtonsetBackgroundColor:[UIColor redColor]];    [tryButtonaddTarget:selfaction:@selector(tryButtonAction)forControlEvents:UIControlEventTouchUpInside];    [self.tableViewaddSubview:tryButton];添加在scrollView方法里设置坐标即可-(void)scrollViewDidScroll:(UIScrollView *)scrollView{    tryButton.frame =CGRectMake(tryButton.frame.origin.x, (self.tableView.frame.size.height-44)+self.tableView.contentOffset.y , tryButton.frame.size.width,tryButton.frame.size.height);}

去掉最后一条 cell分割线

 if(IOS7){                cell.separatorInset = UIEdgeInsetsMake(0, 0, 0, cell.bounds.size.width);            }

获取 tableview 每个cell 的坐标点

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{  CGRect  popoverRect = [tableView convertRect:[tableView rectForRowAtIndexPath:indexPath] toView:[tableView superview]];}

给tableview设置缩进级别

-(NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath{    if (indexPath.row ==0) {        return10;    }     return 0;}

TableView重用方法

方法一,子视图少的情况

 for (UIView *view in cell.contentView.subviews) {    [view removeFromSuperview];}或者
  else//当页面拉动的时候 当cell存在并且最后一个存在 把它进行删除就出来一个独特的cell我们在进行数据配置即可避免    {        while ([cell.contentView.subviews lastObject] != nil) {            [(UIView *)[cell.contentView.subviews lastObject] removeFromSuperview];        }    }

方法二,缺点,多个不同的cell需要有不同的标识符

UIImageView *imageView = [[UIImageView alloc] initWithFra  }  UIImageView *imgView = (UIImageView*)[cell.contentView viewWithTag:1000];

方法三,自定义cell,但一般只对固定不变的类进行封装


多个TableView切换的方法

一定要在加载完数据后,reloadData之前确定需要加载的tableViewCell,而且,使用上拉或者加载更多刷新的时候,一样的,否则出现tableCell为nil的情况

ScrollView


  • UIScrollView在手指离开后立即停止滑动
-(void)scrollViewWillBeginDecelerating: (UIScrollView *)scrollView{    [scrollView setContentOffset:scrollView.contentOffset animated:NO];}
0 0
原创粉丝点击