title: UITableView 相关知识
date: 2015-12-23 11:50
categories: IOS
tags: UITableView
小小程序猿
我的博客:http://daycoding.com
使用开源组件FDTemplateLayoutCell 动态计算Cell高度相关问题
- 自定义Cell
需要注意tableview注册自定义的cell,否侧程序崩溃报错
[self.tbl_marker registerClass:[DataListOverLayerTableViewCell class] forCellReuseIdentifier:@"LINE"];
- 自定义Cell 约束条件
如果自定义Cell约束条件错误会导致测量的高度为0,需逐步修改约束条件直至能计算出高度
注意:约束条件最好包括 top\bottom\left\right 和 width、height这样能最大程度保证能测量出来高度
[self.btn_title mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.contentView.mas_left).offset(10);
make.top.equalTo(self.contentView.mas_top).offset(10);
make.bottom.equalTo(hline.mas_top).offset(-10);
make.right.equalTo(self.contentView.mas_right).offset(-10);
make.width.mas_equalTo(self.contentView.frame.size.width-20);
make.height.greaterThanOrEqualTo(@60);
}];
设置cell点击选中状态去掉背景
由于使用FDTemplateLayoutCell 作为预加载cell高度计算,这时会先调用**heightForRowAtIndexPath**
方法计算高度,在tableView fd_heightForCellWithIdentifier:@"MARKER" configuration:^(DataListTableViewCell* cell)
中会生成cell,所以在执行方法cellForRowAtIndexPath
的时候就会取得缓存cell[tableView dequeueReusableCellWithIdentifier:@"MARKER"]
不会执行cell的初始化方法,所以在初始化里执行cell.selectionStyle = UITableViewCellSelectionStyleNone;
不会有任何效果,需要移除到初始化方法外
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//取消选中效果
[self.tbl_marker deselectRowAtIndexPath:[self.tbl_marker indexPathForSelectedRow] animated:YES];
}
就尼玛0.5进位问题导致cell高度不对,uitextview不显示文字
//这里返回的height 是float 不能随意改成int
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section==1) {
return 108;
}else
{
//这里返回的height 是float 不能随意改成int
//如果返回int 会导致舍去0.5个单位 导致uitextview 高度被舍去1px 导致不显示文字
float height = [tableView fd_heightForCellWithIdentifier:@"cell" configuration:^(EditMarkerExpandableTextCellTableViewCell* cell) {
MarkerAttr* attr = [self.attrArray objectAtIndex:indexPath.row];
cell.textView.text = [self.dataDic objectForKey:attr.key];
}];
return height;
}
return 100;
}
获取被点击view所在的UITableViewCell
原因:由于IOS系统不同,不能使用LayerCellTableViewCell* cell = (LayerCellTableViewCell*)[[view superview ] superview]
来获取cell,因为contentview和cell之间在不同版本里相隔了一个其他view
使用如下代码解决问题:
CGPoint buttonPosition = [view convertPoint:CGPointZero toView:self.tbv_layers];
NSIndexPath *indexPath = [self.tbv_layers indexPathForRowAtPoint:buttonPosition];
LayerCellTableViewCell* cell = (LayerCellTableViewCell*)[self.tbv_layers cellForRowAtIndexPath:indexPath];
利用UITable的group特性来设置cell之间的间距并取消header的悬浮效果
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
CGFloat sectionHeaderHeight = 10; //这里是我的headerView和footerView的高度
if (_tableView.contentOffset.y<=sectionHeaderHeight&&_tableView.contentOffset.y>=0) {
_tableView.contentInset = UIEdgeInsetsMake(-_tableView.contentOffset.y, 0, 0, 0);
} else if (_tableView.contentOffset.y>=sectionHeaderHeight) {
_tableView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
}
}
刷新单个cell或者section
//一个section刷新
NSIndexSet *indexSet=[[NSIndexSet alloc]initWithIndex:2];
[tableview reloadSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic];
//一个cell刷新
NSIndexPath *indexPath=[NSIndexPath indexPathForRow:3 inSection:0];
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:UITableViewRowAnimationNone];