iOS開發小技巧:tableview

cell的收起、打開

www.jianshu.com/p/202b5cfcc…git

自定義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];

NSIndexSet *indexSet=[[NSIndexSet alloc]initWithIndex:2];    
[tableview reloadSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic];    

複製代碼

判斷某行cell是否已經顯示

CGRect cellRect = [tableView rectForRowAtIndexPath:indexPath];
BOOL completelyVisible = CGRectContainsRect(tableView.bounds, cellRect);
複製代碼

判斷cell在屏幕上

1.-(NSArray*)visibleCells; UITableView的方法,這個最直接,返回一個UITableviewcell的數組。 對於自定製的cell,以後的處理可能稍微繁瑣些。github

2.- (NSArray*)indexPathsForVisibleRows; UITableview的又一個方法,這個比較好用,返回一個NSIndexPath的數組,能夠直接用indexpath.row去調你的table_related_Array裏的數據了。比較方便,用於自定製的cell。數組

3.這個方法能夠用在代理回調較多的設計中緩存

- (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的可視區域了。

} else {
    //myCell在可視區域,業務處理

}
複製代碼

加載網絡圖片優化

思想:中止滾動時才加載 來自www.jianshu.com/p/328e50390… 我的認爲需求不適合markdown

某個cell

UITableViewCell *cell = [weakSelf.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:3 inSection:0]];

或

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{

   if (scrollView == _rightTableView && _isSelected == NO) {
       //系統方法返回處於tableView某座標處的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的子控件,獲取對應的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];
複製代碼

默認選中某行cell

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    PDNetCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PDNetCell" forIndexPath:indexPath];
    cell.model = [self.gridArr objectAtIndex:indexPath.row];
    
    // 默認選中行( 關鍵代碼 )
    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;    //  標誌,只能默認點擊一次
    }
    
    return cell;
}


// 在cell.m文件中方法,顯示選中樣式
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

//.......
}
複製代碼

選中cell時的樣式

//無色  
cell.selectionStyle = UITableViewCellSelectionStyleNone;  
//藍色  
cell.selectionStyle = UITableViewCellSelectionStyleBlue;  
//灰色  
cell.selectionStyle = UITableViewCellSelectionStyleGray;
複製代碼

設置cell之間的間距

//自定義cell,重寫setFrame:方法
- (void)setFrame:(CGRect)frame
{
    frame.size.height -= 20;
    [super setFrame:frame];
}
複製代碼

插入數據

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];
複製代碼

數據未顯示滿一屏幕,隱藏多餘的Cell

self.tableView.tableFooterView = [[UIView alloc]init];

複製代碼

分割線設置爲頂格(默認開頭空15像素點)

www.titanjun.top/2016/11/20/…網絡

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];
}
複製代碼

更新行高

  1. cell中添加一個屬性

@property(nonatomic,assign)float cellH;oop

2.設置block回調,用於刷新行高 @property(nonatomic,strong)void(^heightReback_Block)(float cellH); 佈局

3.調用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高度自適應1 *

// 實現代理方法 便可
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}
//  注:cell子控件的佈局 !
複製代碼

cell高度自適應2

用frame佈局時,這種一般在你的模型中添加一個輔助屬性cellHeight,在模型中重寫這個屬性的get方法,根據你的佈局和模型中的其餘屬性值計算出總高度。最後在tableView:heightForRow方法中,根據indexPath找出對應的模型,返回這個高度便可。動畫

cell高度自適應3 *

// 預估行高
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.estimatedRowHeight = 150;
...

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0 || indexPath.section == 1) {
            return 81;
    }
    // 解決固定行高和系統自動計算行高  其餘組走系統自動計算行高
    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;
}
複製代碼

編輯狀態下可多選

self.myTableView.allowsMultipleSelectionDuringEditing = YES;
複製代碼
- (IBAction)edit:(id)sender {    
    [self.myTableView setEditing:!self.myTableView.isEditing animated:YES];    //進入批量編輯狀態
    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];   //刷新數據
    //動畫刷新數據
    [self.myTableView deleteRowsAtIndexPaths:self.myTableView.indexPathsForSelectedRows withRowAnimation:UITableViewRowAnimationAutomatic];
}
複製代碼

滾動到某一行cell

[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:NSNotFound inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];
複製代碼

下拉放大header圖片

第三方:github.com/iThinkerYZ/… 1.建立tableview

。。。
//注意設置四周間距(上左下右)
self.tableView.contentInset = UIEdgeInsetsMake(0.2*screenH, 0, 0, 0);
複製代碼

2.建立圖片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;     //關鍵
    
    [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中一樣適用。

抽象基類

設計同model、同邏輯的多種cell www.jianshu.com/p/f308c43fb…

相關文章
相關標籤/搜索