`
wenzongliang
  • 浏览: 448698 次
  • 性别: Icon_minigender_1
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

UITableView自定义个性化

 
阅读更多

 

UITableView的强大更多程度上来自于可以任意自定义UITableViewCell单元格。

通常,UITableView中的Cell是 动态的,在使用过程中,会创建一个Cell池,根据每个cell的高度(即tableView:heightForRowAtIndexPath:返回 值),以及屏幕高度计算屏幕中可显示几个cell。而进行自定义TableViewCell无非是采用代码实现采用IB编辑nib文件来实现两种方式, 本文主要收集代码的方式实现各种cell自定义。

如何动态调整Cell高度

  1. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {  
  2.    
  3.     static NSString *CellIdentifier = @"Cell";  
  4.    
  5.     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];  
  6.     if (cell == nil) {  
  7.         cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];  
  8.         UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];  
  9.         label.tag = 1;  
  10.         label.lineBreakMode = UILineBreakModeWordWrap;  
  11.         label.highlightedTextColor = [UIColor whiteColor];  
  12.         label.numberOfLines = 0;  
  13.         label.opaque = NO; // 选中Opaque表示视图后面的任何内容都不应该绘制  
  14.         label.backgroundColor = [UIColor clearColor];  
  15.         [cell.contentView addSubview:label];  
  16.         [label release];  
  17.     }  
  18.    
  19.     UILabel *label = (UILabel *)[cell viewWithTag:1];  
  20.     NSString *text;  
  21.     text = [textArray objectAtIndex:indexPath.row];  
  22.     CGRect cellFrame = [cell frame];  
  23.     cellFrame.origin = CGPointMake(0, 0);  
  24.    
  25.     label.text = text;  
  26.     CGRect rect = CGRectInset(cellFrame, 2, 2);  
  27.     label.frame = rect;  
  28.     [label sizeToFit];  
  29.     if (label.frame.size.height > 46) {  
  30.         cellFrame.size.height = 50 + label.frame.size.height - 46;  
  31.     }  
  32.     else {  
  33.         cellFrame.size.height = 50;  
  34.     }  
  35.     [cell setFrame:cellFrame];  
  36.    
  37.     return cell;  
  38. }  
  39.   
  40. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath  
  41. {  
  42.     UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];  
  43.     return cell.frame.size.height;  
  44. }  

如何用图片自定义Table Separeator分割线
一般地,利用类似[tableView setSeparatorColor:[UIColor redColor]];语句即可修改cell中间分割线的颜色。那又如何用一个图片作为分割线背景呢?可以尝试如下:
方法一:
先设置cell separatorColor为clear,然后把图片做的分割线添加到自定义的custom cell上。

方法二:
在cell里添加一个像素的imageView后将图片载入进,之后设置tableView.separatorStyle = UITableViewCellSeparatorStyleNone

自定义首行Cell与其上面导航栏间距

  1. tableView.tableHeaderView = [[[UIView alloc] initWithFrame:CGRectMake(0,0,5,20)] autorelease];  

自定义UITableViewCell的accessory样式
      默认的accessoryType属性有四种取值:UITableViewCellAccessoryNone、 UITableViewCellAccessoryDisclosureIndicator、 UITableViewCellAccessoryDetailDisclosureButton、 UITableViewCellAccessoryCheckmark。

如果想使用自定义附件按钮的其他样式,则需使用UITableView的accessoryView属性来指定。

  1. UIButton *button;  
  2. if(isEditableOrNot) {  
  3.     UIImage *image = [UIImage imageNamed:@"delete.png"];  
  4.     button = [UIButton buttonWithType:UIButtonTypeCustom];  
  5.     CGRect frame = CGRectMake(0.0,0.0,image.size.width,image.size.height);  
  6.     button.frame = frame;  
  7.     [button setBackgroundImage:image forState:UIControlStateNormal];  
  8.     button.backgroundColor = [UIColor clearColor];  
  9.     cell.accessoryView = button;  
  10. }else{  
  11.     button = [UIButton buttonWithType:UIButtonTypeCustom];  
  12.     button.backgroundColor = [UIColor clearColor];  
  13.     cell.accessoryView = button;  
  14. }  

以上代码仅仅是定义了附件按钮两种状态下的样式,问题是现在这个自定义附件按钮的事件仍不可用。

即事件还无法传递到 UITableViewDelegate的accessoryButtonTappedForRowWithIndexPath方法上。

当我们在上述代码 中在加入以下语句:
       [button addTarget:self action:@selector(btnClicked:event:) forControlEvents:UIControlEventTouchUpInside];
后, 虽然可以捕捉到每个附件按钮的点击事件,但我们还无法进行区别到底是哪一行的附件按钮发生了点击动作!因为addTarget:方法最多允许传递两个参 数:target和event,这两个参数都有各自的用途了(target指向事件委托对象,event指向所发生的事件)。看来只依靠Cocoa框架已 经无法做到了。

      但我们还是可以利用event参数,在自定义的btnClicked方法中判断出事件发生在UITableView的哪一个cell上。因为UITableView有一个很关键的方法indexPathForRowAtPoint,可以根据触摸发生的位置,返回触摸发生在哪一个cell的indexPath。而且通过event对象,正好也可以获得每个触摸在视图中的位置。

 

  1. // 检查用户点击按钮时的位置,并转发事件到对应的accessory tapped事件  
  2. - (void)btnClicked:(id)sender event:(id)event  
  3. {  
  4.      NSSet *touches = [event allTouches];  
  5.      UITouch *touch = [touches anyObject];  
  6.      CGPoint currentTouchPosition = [touch locationInView:self.tableView];  
  7.      NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:currentTouchPosition];  
  8.      if(indexPath != nil)  
  9.      {  
  10.          [self tableView:self.tableView accessoryButtonTappedForRowWithIndexPath:indexPath];  
  11.      }  
  12. }  

 

这样,UITableView的accessoryButtonTappedForRowWithIndexPath方法会被触发,并且获得一个indexPath参数。通过这个indexPath参数,我们即可区分到底哪一行的附件按钮发生了触摸事件。

  1. - (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath  
  2. {  
  3.     int  *idx = indexPath.row;  
  4.    //这里加入自己的逻辑  
  5. }  

 

cell设置背景图片。

 

UIImageView *bgImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"home.png"]];
    cell.backgroundView = bgImageView;    
    [bgImageView release];

cell设置选中图片

UIImageView *ivSelected = [[UIImageView alloc] initWithImage:kSelectedTableCellImage];  
  cell.selectedBackgroundView = ivSelected;   
  [ivSelected release];

 

 

-、建立 UITableView

 DataTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 420)];
 [DataTable setDelegate:self];
 [DataTable setDataSource:self];
 [self.view addSubview:DataTable];
 [DataTable release];
 
二、UITableView各Method说明
 
//Section总数
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
 return TitleData;
}
 
// Section Titles
//每个section显示的标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
 return @"";
}
 
//指定有多少个分区(Section),默认为1
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
 return 4;
}
 
//指定每个分区中有多少行,默认为1
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
}
 
//绘制Cell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
  
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
                             SimpleTableIdentifier];
    if (cell == nil) {  
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                       reuseIdentifier: SimpleTableIdentifier] autorelease];
 }
 cell.imageView.image=image;//未选cell时的图片
 cell.imageView.highlightedImage=highlightImage;//选中cell后的图片
 cell.text=//.....
 return cell;
}
 
//行缩进
-(NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath{
 NSUInteger row = [indexPath row];
 return row;
}
 
//改变行的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 40;
}
 
//定位
[TopicsTable setContentOffset:CGPointMake(0, promiseNum * 44 + Chapter * 20)];
 
//返回当前所选cell
NSIndexPath *ip = [NSIndexPath indexPathForRow:row inSection:section];
[TopicsTable selectRowAtIndexPath:ip animated:YES scrollPosition:UITableViewScrollPositionNone];
 
[tableView setSeparatorStyle:UITableViewCellSelectionStyleNone];
 
//选中Cell响应事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
 [tableView deselectRowAtIndexPath:indexPath animated:YES];//选中后的反显颜色即刻消失
}
 
//判断选中的行(阻止选中第一行)
-(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSUInteger row = [indexPath row];
    if (row == 0)
        return nil;
   
    return indexPath;
}
 
//划动cell是否出现del按钮
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
}
 
//编辑状态
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
{
[topicsTable setContentSize:CGSizeMake(0,controller.promiseNum * 44)];
//右侧添加一个索引表
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
}
//返回Section标题内容
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
}
//自定义划动时del按钮内容
- (NSString *)tableView:(UITableView *)tableView
titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
//跳到指的row or section
[tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:NO];
三、在UITableViewCell上建立UILable多行显示
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";   
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
  UILabel *Datalabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 320, 44)];
  [Datalabel setTag:100];
  Datalabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  [cell.contentView addSubview:Datalabel];
  [Datalabel release];
 } 
 UILabel *Datalabel = (UILabel *)[cell.contentView viewWithTag:100];
 [Datalabel setFont:[UIFont boldSystemFontOfSize:18]];
 Datalabel.text = [data.DataArray objectAtIndex:indexPath.row];
 cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}
//选中cell时的颜色
typedef enum {
    UITableViewCellSelectionStyleNone,
    UITableViewCellSelectionStyleBlue,
    UITableViewCellSelectionStyleGray
} UITableViewCellSelectionStyle 
//cell右边按钮格式
typedef enum {
    UITableViewCellAccessoryNone,                   // don't show any accessory view
    UITableViewCellAccessoryDisclosureIndicator,    // regular chevron. doesn't track
    UITableViewCellAccessoryDetailDisclosureButton, // blue button w/ chevron. tracks
    UITableViewCellAccessoryCheckmark               // checkmark. doesn't track
} UITableViewCellAccessoryType
//是否加换行线
typedef enum {
    UITableViewCellSeparatorStyleNone,
    UITableViewCellSeparatorStyleSingleLine
} UITableViewCellSeparatorStyle//改变换行线颜色
tableView.separatorColor = [UIColor blueColor];

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics