cell的收起屈雄、打開
http://www.reibang.com/p/202b5cfcc6f4
自定義cell選中時的背景色
cell.selectedBackgroundView = [UIView new];
cell.selectedBackgroundView.backgroundColor = [UIColor xxxxxx];
刷新某個cell或section
NSIndexPath *indexPath=[NSIndexPath indexPathForRow:3 inSection:0];
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:UITableViewRowAnimationNone];
NSIndexPath *indexPath=[NSIndexPath indexPathForRow:3 inSection:0];
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:UITableViewRowAnimationNone];
判斷某行cell是否已經(jīng)顯示
CGRect cellRect = [tableView rectForRowAtIndexPath:indexPath];
BOOL completelyVisible = CGRectContainsRect(tableView.bounds, cellRect);
判斷cell在屏幕上
1.-(NSArray*)visibleCells;
UITableView
的方法踢涌,這個最直接踏兜,返回一個UITableviewcell
的數(shù)組。
對于自定制的cell伤为,之后的處理可能稍微繁瑣些蜓萄。
2.- (NSArray*)indexPathsForVisibleRows;
UITableview
的又一個方法码泛,這個比較好用,返回一個NSIndexPath
的數(shù)組,可以直接用indexpath.row
去調(diào)你的table_related_Array里的數(shù)據(jù)了嫉沽。比較方便,用于自定制的cell昔逗。
3.這個方法可以用在代理回調(diào)較多的設(shè)計中
- (CGRect)rectForRowAtIndexPath:(NSIndexPath*)indexPath;
CGRect cellR = [myTV rectForRowAtIndexPath:indx];
if (myTV.contentOffset.y - cellR.origin.y < myCell.frame.size.height || cellR.origin.y - myTV.contentOffset.y >myTV.size.height) {
//這時myCell不在myTV的可視區(qū)域了。
} else {
//myCell在可視區(qū)域至非,業(yè)務(wù)處理
}
加載網(wǎng)絡(luò)圖片優(yōu)化
思想:停止?jié)L動時才加載
來自http://www.reibang.com/p/328e503900d0
個人認(rèn)為需求不適合
某個cell
UITableViewCell *cell = [weakSelf.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:3 inSection:0]];
或
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
if (scrollView == _rightTableView && _isSelected == NO) {
//系統(tǒng)方法返回處于tableView某坐標(biāo)處的cell的indexPath
NSIndexPath * indexPath = [_rightTableView indexPathForRowAtPoint:scrollView.contentOffset];
NSLog(@"滑到了第 %ld 組 %ld個",indexPath.section, indexPath.row);
_currentIndexPath = [NSIndexPath indexPathForRow:0 inSection:indexPath.section];
[_leftTableView reloadData];
[_leftTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:indexPath.section] atScrollPosition:UITableViewScrollPositionMiddle animated:NO];
}
}
自定義cell的右icon
self.accessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"對號"]];
// 好處:無需再次布局
點擊cell的子控件钠署,獲取對應(yīng)的cell
CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:buttonPosition];
UITableViewCell *cell = (UITableViewCell*)[self.tableView cellForRowAtIndexPath:indexPath];
選中滾動到某行cell
[self.myTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:UITableViewScrollPositionNone];
默認(rèn)選中某行cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
PDNetCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PDNetCell" forIndexPath:indexPath];
cell.model = [self.gridArr objectAtIndex:indexPath.row];
// 默認(rèn)選中行( 關(guān)鍵代碼 )
if (!isInit) {
NSIndexPath *firstPath = [NSIndexPath indexPathForRow:0 inSection:0];
[tableView selectRowAtIndexPath:firstPath animated:YES scrollPosition:UITableViewScrollPositionNone];
if ([tableView.delegate respondsToSelector:@selector(tableView:didSelectRowAtIndexPath:)]) {
[tableView.delegate tableView:tableView didSelectRowAtIndexPath:firstPath];
}
isInit = YES; // 標(biāo)志,只能默認(rèn)點擊一次
}
return cell;
}
// 在cell.m文件中方法睡蟋,顯示選中樣式
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
//.......
}
選中cell時的樣式
//無色
cell.selectionStyle = UITableViewCellSelectionStyleNone;
//藍(lán)色
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
//灰色
cell.selectionStyle = UITableViewCellSelectionStyleGray;
設(shè)置cell之間的間距
//自定義cell踏幻,重寫setFrame:方法
- (void)setFrame:(CGRect)frame
{
frame.size.height -= 20;
[super setFrame:frame];
}
插入數(shù)據(jù)
NSMutableArray *insertion = [NSMutableArray arrayWithCapacity:0];
for (int i = 0; i < tmpGoodsList.goods.count; i++) {
[insertion addObject:[NSIndexPath indexPathForRow:tmpcount + i inSection:3]];
}
[self.rushTableView insertRowsAtIndexPaths:insertion withRowAnimation:UITableViewRowAnimationMiddle];
數(shù)據(jù)未顯示滿一屏幕,隱藏多余的Cell
self.tableView.tableFooterView = [[UIView alloc]init];
分割線設(shè)置為頂格(默認(rèn)開頭空15像素點)
http://www.titanjun.top/2016/11/20/iOS之UITableView設(shè)置全屏分隔線/
cell.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
滾動到某行
[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:NSNotFound inSection:2] atScrollPosition:UITableViewScrollPositionTop animated:YES];
點擊cell自動滾到下一行
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//第一種方法
[tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
//第二種方法
[self.tableVieW selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionTop];
}
更新行高
- cell中添加一個屬性
@property(nonatomic,assign)float cellH;
2.設(shè)置block回調(diào)戳杀,用于刷新行高
@property(nonatomic,strong)void(^heightReback_Block)(float cellH);
3.調(diào)用block该面,開始刷新
cell.heightReback_Block = ^(float cellH) {
_cellH = cellH; // 更新行高
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:UITableViewRowAnimationBottom];
};
·······
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return _cellH;
}
cell高度自適應(yīng)1 *
// 實現(xiàn)代理方法 即可
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 100;
}
// 注:cell子控件的布局 !
cell高度自適應(yīng)2
用frame布局時信卡,這種通常在你的模型中添加一個輔助屬性cellHeight隔缀,在模型中重寫這個屬性的get方法,根據(jù)你的布局和模型中的其他屬性值計算出總高度傍菇。最后在tableView:heightForRow方法中猾瘸,根據(jù)indexPath找出對應(yīng)的模型,返回這個高度即可丢习。
cell高度自適應(yīng)3 *
// 預(yù)估行高
self.tableView.rowHeight = UITableViewAutomaticDimension;
self.tableView.estimatedRowHeight = 150;
...
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 0 || indexPath.section == 1) {
return 81;
}
// 解決固定行高和系統(tǒng)自動計算行高 其他組走系統(tǒng)自動計算行高
return UITableViewAutomaticDimension;
}
自己緩存cell高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
BSQuestionsModel * model = _dataArray[indexPath.section];
return model.cell_height?:UITableViewAutomaticDimension;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
BSQuestionsModel * model = _dataArray[indexPath.section];
BSQuestionsTableViewCell * cell = [BSQuestionsTableViewCell cellForTableView:tableView model:model];
//高度緩存
CGFloat height = [cell systemLayoutSizeFittingSize:CGSizeMake(tableView.frame.size.width, 0) withHorizontalFittingPriority:UILayoutPriorityRequired verticalFittingPriority:UILayoutPriorityFittingSizeLevel].height;
model.cell_height = height;
return cell;
}
編輯狀態(tài)下可多選
self.myTableView.allowsMultipleSelectionDuringEditing = YES;
- (IBAction)edit:(id)sender {
[self.myTableView setEditing:!self.myTableView.isEditing animated:YES]; //進(jìn)入批量編輯狀態(tài)
self.deleteBtn.hidden = !self.myTableView.isEditing;
}
- (IBAction)delete:(id)sender {
NSMutableArray *deleArr = [NSMutableArray array];
for(NSIndexPath *indx in self.myTableView.indexPathsForSelectedRows){
[deleArr addObject:self.girlArray[indx.row]]; //拿到選中的行
}
[self.girlArray removeObjectsInArray:deleArr]; //從模型中把它們刪除
// [self.myTableView reloadData]; //刷新數(shù)據(jù)
//動畫刷新數(shù)據(jù)
[self.myTableView deleteRowsAtIndexPaths:self.myTableView.indexPathsForSelectedRows withRowAnimation:UITableViewRowAnimationAutomatic];
}
滾動到某一行cell
[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:NSNotFound inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];
下拉放大header圖片
第三方:https://github.com/iThinkerYZ/YZHeaderScaleImage
1.創(chuàng)建tableview
牵触。。咐低。
//注意設(shè)置四周間距(上左下右)
self.tableView.contentInset = UIEdgeInsetsMake(0.2*screenH, 0, 0, 0);
2.創(chuàng)建圖片img view
UIImageView *iimgv = [[UIImageView alloc] initWithFrame:
CGRectMake(0, -0.2*screenH , screenW, 0.2*screenH)]; // 0.2*screenH為圖片原始高度
iimgv.image = [UIImage imageNamed:@"myHeader"];
iimgv.contentMode = UIViewContentModeScaleAspectFill; //關(guān)鍵
[self.tableView addSubview:iimgv]; //添加
self.iimgv = iimgv;
3.下拉放大處理
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
CGFloat y = self.tableView.contentOffset.y;
if (y < -0.234*screenH) {
CGRect frame = self.iimgv.frame;
frame.origin.y = y;
frame.size.height = - y;
self.iimgv.frame = frame;
}
return;
}
4.scrollview中同樣適用揽思。
抽象基類
設(shè)計同model、同邏輯的多種cell
http://www.reibang.com/p/f308c43fb459`