UITableView的强大更多程度上来自于能够任意自定义UITableViewCell单元格。app
一般,UITableView中的Cell是 动态的,在使用过程当中,会建立一个Cell池,根据每一个cell的高度(即tableView:heightForRowAtIndexPath:返回 值),以及屏幕高度计算屏幕中可显示几个cell。而进行自定义TableViewCell无非是采用代码实现或采用IB编辑nib文件来实现两种方式, 本文主要收集代码的方式实现各类cell自定义。
如何动态调整Cell高度框架
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { ide
staticNSString*CellIdentifier=@"Cell"; code
UITableViewCell*cell =[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; orm
if(cell ==nil){ 对象
cell =[[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; blog
UILabel*label =[[UILabel alloc] initWithFrame:CGRectZero]; 事件
label.tag =1; 图片
label.lineBreakMode =UILineBreakModeWordWrap; get
label.highlightedTextColor =[UIColor whiteColor];
label.numberOfLines =0;
label.opaque = NO;// 选中Opaque表示视图后面的任何内容都不该该绘制
label.backgroundColor =[UIColor clearColor];
[cell.contentView addSubview:label];
[label release];
}
UILabel*label =(UILabel*)[cell viewWithTag:1];
NSString*text;
text =[textArray objectAtIndex:indexPath.row];
CGRect cellFrame =[cell frame];
cellFrame.origin =CGPointMake(0,0);
label.text = text;
CGRect rect =CGRectInset(cellFrame,2,2);
label.frame = rect;
[label sizeToFit];
if(label.frame.size.height >46){
cellFrame.size.height =50+ label.frame.size.height -46;
}
else{
cellFrame.size.height =50;
}
[cell setFrame:cellFrame];
return cell;
}
-(CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath
{
UITableViewCell*cell =[self tableView:tableView cellForRowAtIndexPath:indexPath];
return cell.frame.size.height;
}
如何用图片自定义Table Separeator分割线
通常地,利用相似[tableView setSeparatorColor:[UIColor redColor]];语句便可修改cell中间分割线的颜色。那又如何用一个图片做为分割线背景呢?能够尝试以下:
方法一:
先设置cell separatorColor为clear,而后把图片作的分割线添加到自定义的custom cell上。
方法二:
在cell里添加一个像素的imageView后将图片载入进,以后设置tableView.separatorStyle = UITableViewCellSeparatorStyleNone
自定义首行Cell与其上面导航栏间距
自定义UITableViewCell的accessory样式
默认的accessoryType属性有四种取值:UITableViewCellAccessoryNone、 UITableViewCellAccessoryDisclosureIndicator、 UITableViewCellAccessoryDetailDisclosureButton、 UITableViewCellAccessoryCheckmark。
若是想使用自定义附件按钮的其余样式,则需使用UITableView的accessoryView属性来指定
UIButton*button;
if(isEditableOrNot){
UIImage*image =[UIImage imageNamed:@"delete.png"];
button =[UIButton buttonWithType:UIButtonTypeCustom];
CGRect frame =CGRectMake(0.0,0.0,image.size.width,image.size.height);
button.frame = frame;
[button setBackgroundImage:image forState:UIControlStateNormal];
button.backgroundColor =[UIColor clearColor];
cell.accessoryView = button;
}else{
button =[UIButton buttonWithType:UIButtonTypeCustom];
button.backgroundColor =[UIColor clearColor];
cell.accessoryView = button;
}
以上代码仅仅是定义了附件按钮两种状态下的样式,问题是如今这个自定义附件按钮的事件仍不可用。
即事件还没法传递到 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对象,正好也能够得到每一个触摸在视图中的位置。
// 检查用户点击按钮时的位置,并转发事件到对应的accessory tapped事件
-(void)btnClicked:(id)sender event:(id)event
{
NSSet*touches =[event allTouches];
UITouch*touch =[touches anyObject];
CGPoint currentTouchPosition =[touch locationInView:self.tableView];
NSIndexPath*indexPath =[self.tableView indexPathForRowAtPoint:currentTouchPosition];
if(indexPath !=nil)
{
[self tableView:self.tableView accessoryButtonTappedForRowWithIndexPath:indexPath];
}
}
这样,UITableView的accessoryButtonTappedForRowWithIndexPath方法会被触发,而且得到一个indexPath参数。经过这个indexPath参数,咱们便可区分到底哪一行的附件按钮发生了触摸事件。
-(void)tableView:(UITableView*)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath*)indexPath
{
int*idx = indexPath.row;
//这里加入本身的逻辑
}