UITableView使用详解及技巧大全


一、使用详解

在开发iphone的应用时基本上都要用到UITableView,这里讲解一下UITableView的使用方法及代理的调用情况

- (void)viewDidLoad

{

   [super viewDidLoad];

   //初始化数据

  NSArray*array1_=@[@"张铁林",@"张国立",@"张国荣",@"张艺谋",@"张惠妹"];

  NSArray *array2_=@[@"李小龙",@"李小路"];

  NSArray *array3_=@[@"王刚"];

  self.myDic=@{@"老张家":array1_,@"老李家":array2_,@"老王家":array3_};


   

  UITableView*myTableView_=[[UITableView alloc] initWithFrame:CGRectMake(0,0,320,460) style:UITableViewStylePlain];

   myTableView_.delegate=self;

   myTableView_.dataSource=self;

   //改变换行线颜色lyttzx.com

   myTableView_.separatorColor = [UIColorblueColor];

   //设定Header的高度,

   myTableView_.sectionHeaderHeight=50;

   //设定footer的高度,

   myTableView_.sectionFooterHeight=100;

   //设定行高

   myTableView_.rowHeight=100;

   //设定cell分行线的样式,默认为UITableViewCellSeparatorStyleSingleLine

   [myTableView_setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];

   //设定cell分行线颜色

   [myTableView_setSeparatorColor:[UIColor redColor]];

   //编辑tableView

   myTableView_.editing=NO;

   [self.viewaddSubview:myTableView_];

   

   //跳到指的rowor section

   [myTableView_scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:2 inSection:2]

                atScrollPosition:UITableViewScrollPositionBottomanimated:NO];

}


//指定有多少个分区(Section),默认为1

- (NSInteger)numberOfSectionsInTableView:(UITableView*)tableView {

  return [[self.myDicallKeys]count];

}



//每个section底部标题高度(实现这个代理方法后前面sectionHeaderHeight设定的高度无效)

-(CGFloat)tableView:(UITableView *)tableViewheightForHeaderInSection:(NSInteger)section{

  return20;

}


//每个section头部标题高度(实现这个代理方法后前面sectionFooterHeight设定的高度无效)

-(CGFloat)tableView:(UITableView *)tableViewheightForFooterInSection:(NSInteger)section{

  return20;

}


//每个section头部的标题-Header

- (NSString *)tableView:(UITableView *)tableViewtitleForHeaderInSection:(NSInteger)section{

  return [[self.myDicallKeys]objectAtIndex:section];

}


//每个section底部的标题-Footer

- (NSString *)tableView:(UITableView *)tableViewtitleForFooterInSection:(NSInteger)section{

   return nil;

}


//用以定制自定义的section头部视图-Header

-(UIView *)tableView:(UITableView *)tableViewviewForHeaderInSection:(NSInteger)section{

   return nil;

}


//用以定制自定义的section底部视图-Footer

-(UIView *)tableView:(UITableView *)tableViewviewForFooterInSection:(NSInteger)section{

  UIImageView *imageView_=[[UIImageViewalloc]initWithFrame:CGRectMake(0,0,320,20)];

   imageView_.image=[UIImageimageNamed:@"1000.png"];

  return [imageView_autorelease];

}



//指定每个分区中有多少行,默认为1

- (NSInteger)tableView:(UITableView *)tableViewnumberOfRowsInSection:(NSInteger)section{

  return[[self.myDicobjectForKey:[[self.myDicallKeys]objectAtIndex:section]] count];

}


//改变行的高度(实现主个代理方法后rowHeight设定的高度无效)

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

  return100;

}



//绘制Cell

-(UITableViewCell *)tableView:(UITableView *)tableViewcellForRowAtIndexPath:(NSIndexPath *)indexPath {

  staticNSString *SimpleTableIdentifier =@"SimpleTableIdentifier";

  UITableViewCell*cell = [tableViewdequeueReusableCellWithIdentifier:

                      SimpleTableIdentifier];

  if(cell ==nil) {

      cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault

                             reuseIdentifier: SimpleTableIdentifier]autorelease];

    //设定附加视图

      [cell setAccessoryType:UITableViewCellAccessoryDetailDisclosureButton];

//      UITableViewCellAccessoryNone,              //没有标示

//     UITableViewCellAccessoryDisclosureIndicator,   //下一层标示

//     UITableViewCellAccessoryDetailDisclosureButton, //详情button

//      UITableViewCellAccessoryCheckmark           //勾选标记

      

   //设定选中cell时的cell的背影颜色

      cell.selectionStyle=UITableViewCellSelectionStyleBlue  //选中时蓝色效果

//     cell.selectionStyle=UITableViewCellSelectionStyleNone;//选中时没有颜色效果

//     cell.selectionStyle=UITableViewCellSelectionStyleGray; //选中时灰色效果

//      

//      //自定义选中cell时的背景颜色

//      UIView *selectedView = [[UIView alloc]initWithFrame:cell.contentView.frame];

//      selectedView.backgroundColor = [UIColororangeColor];

//      cell.selectedBackgroundView =selectedView;

//     cell.selectionStyle=UITableViewCellAccessoryNone;//行不能被选中


   }

   

  //这是设置没选中之前的背景颜色

   cell.contentView.backgroundColor= [UIColorclearColor];

   cell.imageView.image=[UIImageimageNamed:@"1001.jpg"];//未选cell时的图片

   cell.imageView.highlightedImage=[UIImageimageNamed:@"1002.jpg"];//选中cell后的图片

   cell.textLabel.text=[[self.myDicobjectForKey:[[self.myDicallKeys]objectAtIndex:indexPath.section]]objectAtIndex:indexPath.row];

  return cell;

}


//行缩进

-(NSInteger)tableView:(UITableView *)tableViewindentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath{

  NSUInteger row = [indexPathrow];

  return row;

}

//选中Cell响应事件

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

   [tableViewdeselectRowAtIndexPath:indexPath animated:YES];//选中后的反显颜色即刻消失

   //得到当前选中的cell

  UITableViewCell *cell=[tableViewcellForRowAtIndexPath:indexPath];

  NSLog(@"cell=%@",cell);

}


//行将显示的时候调用,预加载行

-(void)tableView:(UITableView *)tableViewwillDisplayCell:(UITableViewCell *)cellforRowAtIndexPath:(NSIndexPath*)indexPath

{

  NSLog(@"将要显示的行是\ncell=%@  \n indexpath=%@",cell,indexPath);

}


//选中之前执行,判断选中的行(阻止选中第一行)

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

{

  NSUInteger row = [indexPathrow];

  if(row ==0)

     returnnil;

  return indexPath;

}




//编辑状态,点击删除时调用

- (void)tableView:(UITableView *)tableViewcommitEditingStyle:(UITableViewCellEditingStyle)editingStyle

forRowAtIndexPath:(NSIndexPath *)indexPath

{

   

}


//cell右边按钮格式为UITableViewCellAccessoryDetailDisclosureButton时,点击按扭时调用的方法

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

  NSLog(@"当前点击的详情button\n indexpath=%@",indexPath);

}


//右侧添加一个索引表

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

  return [self.myDicallKeys];

}


//划动cell是否出现del按钮

- (BOOL)tableView:(UITableView *)tableViewcanEditRowAtIndexPath:(NSIndexPath *)indexPath {

   return YES;

}


//设定横向滑动时是否出现删除按扭,(阻止第一行出现)

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

{

  if(indexPath.row==0) {

     returnUITableViewCellEditingStyleNone;

   }

  else{

     returnUITableViewCellEditingStyleDelete;

   }

  returnUITableViewCellEditingStyleDelete;

}


//自定义划动时delete按钮内容

- (NSString *)tableView:(UITableView *)tableView

titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath*)indexPath{

  return @"删除这行";

      

}


//开始移动row时执行

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

{

  NSLog(@"sourceIndexPath=%@",sourceIndexPath);

  NSLog(@"sourceIndexPath=%@",destinationIndexPath);

}


//滑动可以编辑时执行

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

{

  NSLog(@"willBeginEditingRowAtIndexPath");

}


//将取消选中时执行,也就是上次先中的行

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

{

  NSLog(@"上次选中的行是 \n indexpath=%@",indexPath);

  return indexPath;

}



//让行可以移动

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

{

   return NO;

}


//移动row时执行

-(NSIndexPath *)tableView:(UITableView *)tableViewtargetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPathtoProposedIndexPath:(NSIndexPath*)proposedDestinationIndexPath

{

  NSLog(@"targetIndexPathForMoveFromRowAtIndexPath");

  //用于限制只在当前section下面才可以移动

  if(sourceIndexPath.section !=proposedDestinationIndexPath.section){

     return sourceIndexPath;

   }

  returnproposedDestinationIndexPath;

}





二、技巧

//不显示多余的单元格
self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];

// 这句话不显示单元格之间的分割线
_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

// 这句话在单元格的最后显示一个箭头
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

//--- 点击cell的时候 cell不会产生高亮状态 --
1.cell.selectionStyle = UITableViewCellSelectionStyleNone;该方法缺点是虽然cell可以被用户选中后,但不会被突出显示。
2.第二种方法允许单元格高亮显示,但是交互完成之后移除高亮显示。这需要通知表格取消单元格选中状态。 代码如下:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}


//didSelectRowAtIndexPath方法中,获取某个cell的实例
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    或者某个自定义cell
    MyCustomCell *cell = (MyCustomCell *)[tableView cellForRowAtIndexPath:indexPath];
}

// --- 写在viewdidload里面cell自适应高度 ----
tableView.rowHeight = UITableViewAutomaticDimension; // 自适应单元格高度
tableView.estimatedRowHeight = 50; //先估计一个高度

// 去掉UItableview headerview黏性(sticky)
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat sectionHeaderHeight = 40;
    if (scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=0) {
        scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
    }
    else if (scrollView.contentOffset.y>=sectionHeaderHeight) {
        scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
    }
}

// 获取手势点击的是哪一个cell的坐标
NSIndexPath *indexPath = [self.tableView indexPathForCell:((UITableViewCell *)longPress.view)];

// 局部刷新
//一个section刷新

NSIndexSet *indexSet=[[NSIndexSet alloc]initWithIndex:2];
[tableview reloadSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic];

//一个cell刷新
NSIndexPath *indexPath=[NSIndexPath indexPathForRow:3 inSection:0];
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:UITableViewRowAnimationNone];

// 关于UITableView如何跳转到最后一行或者任意指定行。
其实现如下:
[self.chatTableView scrollToRowAtIndexPath:
 [NSIndexPath indexPathForRow:[self.chatArray count]-1 inSection:0]
                          atScrollPosition: UITableViewScrollPositionBottom
                                  animated:NO];

//任意设置Cell选中状态的背景色:

方法1:
UIView *bgView = [[UIView alloc] init];
bgView.backgroundColor = [UIColor orangeColor];
self.selectedBackgroundView = bgView;
该方法设置的是纯色, 也可以使用任何图片,把selectedBackgroundView设成UIImageView。

方法2:重写UITableViewCell 的 setSelected:animated:方法
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];
    if (selected) {
        self.backgroundColor = RGB(224, 152, 40);
    }
    else {
        self.backgroundColor = [UIColor clearColor];
    }
}


// 点击button时获取button所在的cell的indexpath
UIButton *button = sender;
HeroDermaTableViewCell *cell = (HeroDermaTableViewCell *)[[button superview] superview];
NSIndexPath *indexPath = [_tableView indexPathForCell:cell];


//点击Cell中的按钮时,如何取所在的Cell:

-(void)OnTouchBtnInCell:(UIButton *)btn 
{ 
 

CGPoint point = btn.center; 
 

point = [table convertPoint:point fromView:btn.superview]; 
 

NSIndexPath* indexpath = [table indexPathForRowAtPoint:point]; 
 

UITableViewCell *cell = [table cellForRowAtIndexPath:indexpath]; 
  ...

  // 也可以通过一路取btn的父窗口取到cell,但如果cell下通过好几层subview才到btn,就要取好几次 superview

  // 所以我用上面的方法,比较通用。这种  方法也适用于其它控件。 
}


//让UITableView的section header view不悬停的方法

当UITableView的style属性设置为Plain时,这个tableview的section header在滚动时会默认悬停在界面顶端。取消这一特性的方法有两种:
将 style设置为 Grouped。这时所有的section header都会随着scrollview滚动了。不过grouped和 plain的样式有轻微区别,切换样式后也许需要重新调整UI
重载scrollview的delegate方法:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat sectionHeaderHeight = 40;
    if (scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=0) {
        scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
    } else if (scrollView.contentOffset.y>=sectionHeaderHeight) {
        scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
    }
}



 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值