IOS——TableView以及TableViewCell方法总结

1. TableView 样式

TableView有两种样式,可以在初始化的时候选择Plain或者Grouped

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


2. 委托方法(tableview delegate)

sectionArrays是所有数据的集合 NSMutableArray


2.1  分组(必须实现的方法)

2.1.1. TableView分为多少个section(默认返回1)

    cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;


  1. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {  
  2.   
  3.     return [titleArray count];//返回标题数组中元素的个数来确定分区的个数  
  4.   


2.1.2 每个section有多少个cell

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


2.1.3 每个Cell的样式

  1. -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {  
  2.   
  3.    
  4.   
  5.     static NSString *CellIdentifier = @"Cell";  
  6.   
  7.  //初始化cell并指定其类型,也可自定义cell  
  8.   
  9. UITableViewCell *cell = (UITableViewCell*)[tableView  dequeueReusableCellWithIdentifier:CellIdentifier];  
  10.   
  11.   if(cell == nil)   
  12.   
  13.   {  
  14.   
  15.   cell = [[[UITableViewCellalloc]   
  16.   
  17.   initWithFrame:CGRectZero   
  18.   
  19.   reuseIdentifier:CellIdentifier] autorelease];  
  20.   
  21. }  
  22.   
  23.    switch (indexPath.section) {  
  24.   
  25.   case 0://对应各自的分区  
  26.   
  27.     [[cell textLabel]  setText:[dataArray1 objectAtIndex:indexPath.row]];//给cell添加数据  
  28.   
  29.     break;  
  30.   
  31.   case 1:  
  32.   
  33.     [[cell textLabel]  setText:[dataArray2 objectAtIndex:indexPath.row]];  
  34.     break;
  35.   default:
  36.     [[cell textLabel]  setText:@"Unknown"];  
  37. }
  38.   return cell;//返回cell


2.2  Section Header     

2.2.1 单独设置某个Section Header内容

  1. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {  
  2.     NSString *provincName = [[provinces objectAtIndex:section] objectForKey:@"p_Name"];  
  3.     return provincName;   

2.2.2 以Array形式设置所有Section Header内容

  1. - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {  
  2.     //返回省份的数组  
  3.     NSMutableArray *array = [NSMutableArray arrayWithCapacity:35];  
  4.     for (NSDictionary *dict in provinces) {  
  5.         [array addObject:[dict objectForKey:@"p_Name"]];  
  6.     }  
  7.     return array;  

2.2.3 自定义Section Header样式

  1. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
  2.   {
  3.   // create the parent view that will hold header Label
  4.   UIView* customView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320, 20.0)];
  5.   UIImageView *bg = [[UIImageView alloc]initWithFrame:customView.frame];
  6.   bg.image = [UIImage imageNamed:@"carTypeCellTitleBg1.png"];
  7.   [customView addSubview:bg];
  8.   // create the button object
  9.   UILabel * headerLabel = [[UILabel alloc] initWithFrame:CGRectZero];
  10.   headerLabel.backgroundColor = [UIColor clearColor];
  11.   headerLabel.opaque = NO;
  12.   headerLabel.textColor = [UIColor colorWithRed:242.0/255.0f green:161.0/255.0f blue:4.0/255.0 alpha:1.0];
  13.   // headerLabel.highlightedTextColor = [UIColor whiteColor];
  14.   headerLabel.font = [UIFont italicSystemFontOfSize:15];
  15.   headerLabel.frame = customView.frame;
  16.   // If you want to align the header text as centered
  17.   // headerLabel.frame = CGRectMake(150.0, 0.0, 300.0, 44.0);
  18.   // headerLabel.text = <:Put display to want you whatever here>// i.e. array element
  19.   headerLabel.text = @"title";
  20.   [customView addSubview:headerLabel];
  21.   return customView;
  22.   }

2.2.4  设置Section Header高度

  1.   - (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
  2.   {
  3.   return 21.0;
  4.   }

2.3 Section Footer

2.3.1   设置Footer内容

-(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{ 
    NSString *result = nil; 
    if ([tableView isEqual:myTableView]&§ion==0) { 
        result = @"Section 0 Header"; 
    } 
    return result; 
}

2.3.2   自定义Footer样式

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

{

    if (section == 0)

    {

       

        UIButton *button = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];

        [button setTitle:@"我是button 1"forState:UIControlStateNormal ];

        return button;

    }

    else

    {

        UIButton *button = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];

        [button setTitle:@"我是button 2"forState:UIControlStateNormal ];

        return button;

    }

}

2.3.3 自定义Footer高度

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

{

    return 20.0;

 }


2.4 Cell

这篇文章中的例子没有尝试过,但是很有用,可以看看  http://blog.csdn.net/totogo2010/article/details/7698767

2.4.1 Cell点击事件的监听

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

{

//       取消cell被选中效果

[tableView deselectRowAtIndexPath:indexPath animated:YES];

    NSString *titileString = [arrayobjectAtIndex:[indexPathrow]];  //这个表示选中的那个cell上的数据

        UIAlertView *alert = [[UIAlertViewalloc]initWithTitle:@"提示"message:titileStringdelegate:selfcancelButtonTitle:@"OK"otherButtonTitles:nil];

       [alert show];

}


2.4.2 改变Cell高度

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 40;
}


2.4.3 点击某个Cell无效

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

{

//第一个cell 点击的时候没有反应

    if (indexPath.row ==0) {

        returnnil;

    }

    return indexPath;

}



给 TableView  设置背景

1. 定义UIImageView

2. tableView.setBackgroundView即可

要记住,在初始化Cell时,要设置[cell setbeckgroundColor:[UIColor clearColor]],否则会默认Cell背景为白色


取消 TableView   Cell的分隔线

[self.myTableView       setSeparatorStyle:UITableViewCellSeparatorStyleNone];


解决滑动时Cell内容重叠问题
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
    }
reuseIdentifier千万不能复用原来的identifier,一定设为nil


取消 TableView   Cell的选中高亮效果
cell.selectedBackgroundView=[[UIView alloc] init];
cell.selectedBackgroundView.backgroundColor=[UIColor clearColor];


选中 TableView   Cell时改变字体颜色

直接在label中设置:label.highlightedTextColor=[xxxxxx];

但是要注意,撤销[tableView deselectRowAtIndexPath:indexPath animated:YES];这句代码


默认选中 TableView   Cell的第一行

NSIndexPath *path=[NSIndexPath indexPathForItem:0 inSection:0];

[tableview selecRowAtIndexPath:path animated:YES scrollPosition:UITableViewScrollPositionButtom];


TableViewCell 样式

UITableViewCellAccessoryNone,                                          空

UITableViewCellAccessoryDisclosureIndicator,                    >

UITableViewCellAccessoryDetailDisclosureButton,              ">"按钮

UITableViewCellAccessoryCheckmark                                  √


    cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值