UITableView 详解

来源:互联网 发布:知满天通过率怎么样 编辑:程序博客网 时间:2024/06/05 07:57

系统默认的cell选中颜色设置 


//无色
cell.selectionStyle = UITableViewCellSelectionStyleNone;
//蓝色
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
//灰色
cell.selectionStyle = UITableViewCellSelectionStyleGray;




自定义UITableViewCell选中时背景或颜色
cell.selectedBackgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"cellart.png"]]; //还有字体颜色 
cell.textLabel.highlightedTextColor = [UIColor xxxcolor];  [cell.textLabel setTextColor:color];//设置cell的字体的颜色




pop返回table时,cell自动取消选中状态
首先我有一个UITableViewController,其中每个UITableViewCell点击后都会push另一个ViewController,每次点击Cell的时候,Cell都会被选中,当从push的ViewController返回的时候选中的Cell便会自动取消选中。后来由于某些原因我把这个UITableViewController改成了UIViewController,之后就产生了一个问题:每次返回到TableView的时候,之前选中的Cell不能自动取消选中,经过查找得知:
UITableViewController有一个clearsSelectionOnViewWillAppear的property,
而当把UITableViewController修改成UIViewController后,这个属性自然就不存在了,因此我们必须手动添加取消选中的功能,方法很简单,在viewWillAppear方法中加入:
[self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow] animated:YES];


点击后,过段时间cell自动取消选中
 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
     …………
    //消除cell选择痕迹
    [self performSelector:@selector(deselect) withObject:nil afterDelay:0.5f];
}
- (void)deselect
{
    [self.tableview deselectRowAtIndexPath:[self.tableview indexPathForSelectedRow] animated:YES];
}














//设置tableView不能滚动


[self.tableView setScrollEnabled:NO];








1.去除UITableViewCell的点击效果,在cellForRowAtIndexPath:方法中写上
cell.selectionStyle = UITableViewCellSelectionStyleNone;


2.点击UITableCell时,Cell背景颜色不变,但是上面自定义的控件如:UILabel、UIImageView会变颜色
设置UILabel或UIImageView的Highlighted 然后 在cellForRowAtIndexPath:方法中写上
            UIView *view_bg = [[[UIView alloc]initWithFrame:cell.frame]autorelease];
            view_bg.backgroundColor = [UIColor clearColor];
            cell.selectedBackgroundView = view_bg;


3.点击cell行时,背景颜色一闪而过,在didSelectRowAtIndexPath:方法中写上
 
[tableView deselectRowAtIndexPath:indexPath animated:NO];




下滑手势取消键盘事件


os 7 简单方法
self.tableView.keyboardDismissMode  = UIScrollViewKeyboardDismissModeInteractive;






在我们自定义UITableViewCell的子视图上
可以通过一些drawing and layouting 在子视图上可以容易的使他们滚动更加平滑


cell.layer.shouldRasterize = YES;
cell.layer.rasterizationScale = [UIScreen mainScreen].scale;


这两行代码使得 Moped  这个应用的滚动速度 在老的 iPod Touch 是 30fps 到 45 fps 之间 提升到现在的 60fps
有个小问题 ,这个方法 使所有的views 插入able cell  进行渲染,当cell 滚动时候将会快速进行drawn,如果你的cell  上有个类似滚动条的动画,它们不会有任何改善,也许会更糟,因为当进度条滚动更新数据的时候都要进行渲染


http://www.kurutepe.com/2013/03/make-your-custom-uitableviewcells-scroll-smoothly/






UItableViewcell 的分界线


要去除多余的分割线,在UITableView初始化时加上以下代码即可:    
self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];


设置UITableViewCell之间分隔线的颜色
[self.tableview setSeparatorColor:[UIColor redColor]];


隐藏UITableViewCell的分隔线


[self.myTableView setSeparatorStyle:UITableViewCellSeparatorStyleNone]; 


 UITableViewCellSeparatorStyle有如下几种 
typedef NS_ENUM(NSInteger, UITableViewCellSeparatorStyle) {
    UITableViewCellSeparatorStyleNone,
    UITableViewCellSeparatorStyleSingleLine,
    UITableViewCellSeparatorStyleSingleLineEtched  // This separator style is only supported for grouped style table views currently
};


在iOS7之后uitableviewcell 的分界线原来短了15像素


为什么我说是少了15像素呢?
首先我们拖拽一个默认的tableview 控件! 看下xcode5 面板的inspector(检查器)
我们可以找到一个 Separator Insetss 标签 默认是 Default
我们选择一下 发现有个Custom  这时候我们惊奇的发现Left  15  ,这时候我们只要把这个 15  改成 0 , 然后保存, 你就会发现tableview 的分割线跟以前一样了。


如果是代码写的tableview 
下面我们接着分析这个问题,让我们查询下 tableview delegate  
我们会发现 ios7 增加了一些新属性,
@property (nonatomic)         UIEdgeInsets                separatorInset       NS_AVAILABLE_IOS(7_0)UI_APPEARANCE_SELECTOR;// allows customization of the frame of cell separators


这个时候你应改发现separatorInset 这个单词是否有点眼熟,  苹果公司已经给了注释,可以自定义视cell 的分割线,
UIEdgeInsets 是个结构体类型,这时候我们发现了我们要的属性 left
typedef struct UIEdgeInsets {
    CGFloat top, left, bottom, right; // specify amount to inset (positive) for each of the edges. values can be negative to 'outset'
} UIEdgeInsets;


在ios7中,UITableViewCell左侧会有默认15像素的空白。我们可以设置setSeparatorInset:UIEdgeInsetsZero 能将空白去掉。
    [myTableView setSeparatorInset:UIEdgeInsetsMake(0,0,0,0)];
这时候你会发现你的tableview 的分割线是不是跟以前一样了呢!




但是在ios8中,设置setSeparatorInset:UIEdgeInsetsZero 已经不起作用了。
下面是解决办法


首先在viewDidLoad方法加入以下代码:
 if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {


[self.tableView setSeparatorInset:UIEdgeInsetsZero];


}


if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {


[self.tableView setLayoutMargins:UIEdgeInsetsZero];
}


然后在UITableView的代理方法中加入以下代码
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
[cell setSeparatorInset:UIEdgeInsetsZero];
}
if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
[cell setLayoutMargins:UIEdgeInsetsZero];
}
}




获取 tableview 每个cell 的坐标点
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
  CGRect  popoverRect = [tableView convertRect:[tableView rectForRowAtIndexPath:indexPath] toView:[tableView superview]];

}

触摸tableView时收起键盘


在聊天应用当中,可能会需要在触摸tableView时收起键盘(如QQ)可以给tableView添加手势来解决这个问题

UITapGestureRecognizer *tableViewGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(commentTableViewTouchInSide)];tableViewGesture.numberOfTapsRequired = 1;tableViewGesture.cancelsTouchesInView = NO;[commentTableView addGestureRecognizer:tableViewGesture];- (void)commentTableViewTouchInSide{[messageTextField resignFirstResponder];}


UITableView 优化的几个要点:


1、使用不透明的视图
2、不要重复创建不必要的cell (复用)
3、减少视图的数量
4、不要做多余的绘制工作
5、预渲染图像
6、不要阻塞主线程


0 0
原创粉丝点击