tableView的高度宽度动态布局(默认状态是UIScreen.main.bounds.size.height-180-69)
一://根据滑动慢慢改变frame
func scrollViewDidScroll(_ scrollView: UIScrollView){
guard isCommPush == true else {
return
}
//如果内容小于默认frame就直接返回
// print(myTableView?.contentSize.height)//这里能正确获取到
guard (myTableView?.contentSize.height)! > UIScreen.main.bounds.size.height-180-69 else {
return
}
//如果myTableView已经是最大高度了,就不再执行下面的方法,否则的话滑动的时候页面会一直上下抖动
guard maxHeight <= 110 else {
return
}
let offsetY = scrollView.contentOffset.y // 向下滑动时<0,向上滑动时>0
if offsetY > maxHeight {//这个比较方法就只会执行往上滑动更多的操作
self.selectCommView?.bigView.frame = CGRect.init(x: 0.0, y: 180-offsetY, width: UIScreen.main.bounds.size.width, height: UIScreen.main.bounds.size.height-180+offsetY)
// self.selectCommView?.bigViewTop.constant = 180-offsetY//不要用这种非常卡
maxHeight = offsetY
}
}
二:contenSize等于所有cell(内容)的size
1、在viewDidLoad中添加观察者
- [self.tableView addObserver:self forKeyPath:@"contentSize" options:0 context:NULL];
2、重写observeValueForKeyPath方法,一旦UITableView的contentSize发生改变,就会调用这个方法
- - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
- CGRect frame = self.tableView.frame;
- frame.size = self.tableView.contentSize;
- self.tableView.frame = frame;
- }
_____________________________________________________________________
tableView有一个bounces属性。默认YES,所以tableView上下用力拉都会有弹性滑动,如下设置可以禁止,但是这样的话上下弹性都没了
有时的需求是上方不要弹性,下方要弹性,可以用监听,只要是小于0就是弹性发生的情况,手动设置0禁止即可,如果有x偏转需要先去除x再放上去,同理只取消下方弹性理论上这种思路应该也可以
- (void)scrollViewDidScroll:(UIScrollView *)scrollView { if (scrollView == self.tableView) { CGFloat offY = scrollView.contentOffset.y; if (offY < 0) { scrollView.contentOffset = CGPointZero; } } }
但是一般情况下我们只要设置tableView的Clip to Bounds为YES就可以了,这样跑到frame外面的内容就看不到了,弹性也有了
___________________________________________________________________________________
xib搭建cell
拉入一个UITableViewCell,注意这个cell有一个尺寸,一定要设置否则不能自动布局,
Cell中还有一个View,这个View就是superView,一定要设置,跟cell一个尺寸。另外切记要给这个View设置到他的superViewde边框为(0,0,0,0)
[_tvDevicesList registerNib:[UINib nibWithNibName:@“AllDeviceListTableView" bundle:nil] forCellReuseIdentifier:allDeviceListTableViewCellIdentifier];
通过cell注册TableView,可以把自定义cell与TableView连接起来。NibName:xib文件名
CellReuseIdentifier:给cell取的Identifier
cell的xib文件只要把class改为 cell的.h/或者.swift的类名就可以联通
________________________________________________________________________
cell的四种创建方法:
1、系统默认cell(不需要注册)
cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier")
cell?.selectionStyle = .none
//切记!!!
// cell = tableView.dequeueReusableCell(withIdentifier: "cellID", for: indexPath)//unable to dequeue a cell with identifier reuseIdentifier - must register a nib or a class for the identifier or connect a prototype cell in a storyboard.
2、xib中tableView中的 cell(不需要注册)
let cell = tableView.dequeueReusableCell(withIdentifier:"familyListCell", for: indexPath)
3、cell类(需要注册)
3.1
mytableView.register(UINib.init(nibName: "FaceIDCell", bundle: nil), forCellReuseIdentifier: "FaceIDCell")//cell由xib创建
//[_myCollectView registerClass:[DeviceListCollectionViewCell class] forCellWithReuseIdentifier:allDeviceListCellIdentifier];//cell由代码创建
let cell = tableView.dequeueReusableCell(withIdentifier: "FaceIDCell")as! FaceIDCell
————————————————————————————————————————————
创建不复用的cell
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
NSString *CellIdentifier = [NSString stringWithFormat:@"cell%ld%ld",(long)indexPath.section,(long)indexPath.row];
[self.myCollectView registerNib:[UINib nibWithNibName:allDeviceListCellIdentifier bundle:nil] forCellWithReuseIdentifier:CellIdentifier];
// 通过不同标识创建cell实例
DeviceListCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
........
}