iOS開發(fā)小技巧:tableview

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];
}

更新行高

  1. 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`

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末见擦,一起剝皮案震驚了整個濱河市钉汗,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌鲤屡,老刑警劉巖损痰,帶你破解...
    沈念sama閱讀 223,207評論 6 521
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異酒来,居然都是意外死亡卢未,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,455評論 3 400
  • 文/潘曉璐 我一進(jìn)店門役首,熙熙樓的掌柜王于貴愁眉苦臉地迎上來尝丐,“玉大人显拜,你說我怎么就攤上這事〉” “怎么了远荠?”我有些...
    開封第一講書人閱讀 170,031評論 0 366
  • 文/不壞的土叔 我叫張陵,是天一觀的道長失息。 經(jīng)常有香客問我譬淳,道長,這世上最難降的妖魔是什么盹兢? 我笑而不...
    開封第一講書人閱讀 60,334評論 1 300
  • 正文 為了忘掉前任邻梆,我火速辦了婚禮,結(jié)果婚禮上绎秒,老公的妹妹穿的比我還像新娘浦妄。我一直安慰自己,他們只是感情好见芹,可當(dāng)我...
    茶點故事閱讀 69,322評論 6 398
  • 文/花漫 我一把揭開白布剂娄。 她就那樣靜靜地躺著,像睡著了一般玄呛。 火紅的嫁衣襯著肌膚如雪阅懦。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,895評論 1 314
  • 那天徘铝,我揣著相機(jī)與錄音耳胎,去河邊找鬼。 笑死惕它,一個胖子當(dāng)著我的面吹牛怕午,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播淹魄,決...
    沈念sama閱讀 41,300評論 3 424
  • 文/蒼蘭香墨 我猛地睜開眼诗轻,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了揭北?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 40,264評論 0 277
  • 序言:老撾萬榮一對情侶失蹤吏颖,失蹤者是張志新(化名)和其女友劉穎搔体,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體半醉,經(jīng)...
    沈念sama閱讀 46,784評論 1 321
  • 正文 獨居荒郊野嶺守林人離奇死亡疚俱,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,870評論 3 343
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了缩多。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片呆奕。...
    茶點故事閱讀 40,989評論 1 354
  • 序言:一個原本活蹦亂跳的男人離奇死亡养晋,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出梁钾,到底是詐尸還是另有隱情绳泉,我是刑警寧澤,帶...
    沈念sama閱讀 36,649評論 5 351
  • 正文 年R本政府宣布姆泻,位于F島的核電站零酪,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏拇勃。R本人自食惡果不足惜四苇,卻給世界環(huán)境...
    茶點故事閱讀 42,331評論 3 336
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望方咆。 院中可真熱鬧月腋,春花似錦、人聲如沸瓣赂。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,814評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽钩述。三九已至寨躁,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間牙勘,已是汗流浹背职恳。 一陣腳步聲響...
    開封第一講書人閱讀 33,940評論 1 275
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留方面,地道東北人放钦。 一個月前我還...
    沈念sama閱讀 49,452評論 3 379
  • 正文 我出身青樓,卻偏偏與公主長得像恭金,于是被迫代替她去往敵國和親操禀。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,995評論 2 361

推薦閱讀更多精彩內(nèi)容