自定义UITableViewCell:Cell高度、分割线、间距等
来源:互联网 发布:电视盒软件市场 编辑:程序博客网 时间:2024/06/09 23:22
UITableView的强大更多程度上来自于可以任意自定义UITableViewCell单元格。
通常,UITableView中的Cell是 动态的,在使用过程中,会创建一个Cell池,根据每个cell的高度(即tableView:heightForRowAtIndexPath:返回 值),以及屏幕高度计算屏幕中可显示几个cell。而进行自定义TableViewCell无非是采用代码实现或采用IB编辑nib文件来实现两种方式, 本文主要收集代码的方式实现各种cell自定义。
如何动态调整Cell高度
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
staticNSString*CellIdentifier=@"Cell";
UITableViewCell*cell =[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell ==nil){
cell =[[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
UILabel*label =[[UILabel alloc] initWithFrame:CGRectZero];
label.tag =1;
label.lineBreakMode =UILineBreakModeWordWrap;
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与其上面导航栏间距
- tableView.tableHeaderView = [[[UIView alloc] initWithFrame:CGRectMake(0,0,5,20)] autorelease];
自定义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;
//这里加入自己的逻辑
}
- IOS 自定义UITableViewCell:Cell高度、分割线、间距等
- 自定义UITableViewCell:Cell高度、分割线、间距等
- UITableViewCell自定义分割线
- UITableViewCell自定义分割线
- 自定义cell分割线
- iOS UITableViewCell左边分割线0间距
- 【学习笔记】tableview 文字设置 缩进,头尾标题 间距,cell高度 点击 分割线
- UITableViewCell中cell分割线不显示
- 自定义带有cell间距的UITableViewCell
- 设置cell分割线的高度
- UITableViewCell分割线(样式,宽度·高度调整,颜色等的设置)
- iOS-OC-UITableViewCell分割线自定义
- iOS 自定义UITableViewCell的分割线
- iOS中 自定义cell分割线/分割线偏移
- iOS编程------自定义UITableViewCell / cell自适应高度
- UITableViewCell中去除没有内容的cell的分割线
- 使UITableViewCell之间的cell不显示分割线
- UITableViewCell 分割线的属性 cell两端置顶 缩进 颜色
- linux终端terminal个性化配置
- java客户端模拟表单上传文件
- this的含义,static的用法
- POJ 1852 Ants
- Oracle日期查询:季度、月份、星期等时间信息
- 自定义UITableViewCell:Cell高度、分割线、间距等
- 京东在线网银---测试岗位面试
- js实现浏览器title闪烁
- 百度--测试开发面试
- UVA - 10954 Add All(贪心+优先队列)
- Django 学习之安装配置(win7)
- UvaLive 6667 Longest Chain (分治求三维LIS)
- vs2013+matlab2013
- Oracle数据库操作大全(七)-——事务处理