你真的會用UITableView嘛

UITableView是工程開發(fā)中最經(jīng)常使用到的UI控件,但是你真的了解它嘛,這里記錄幾點(diǎn)有用的但你可能并不知道的。

  • 當(dāng)我們的數(shù)據(jù)未能顯示滿一屏幕的時(shí)候,UITableView會顯示多余的橫線,這個(gè)時(shí)候你如果希望去掉這些橫線,你可以加上這句話。
    self.tableView.tableFooterView = [[UIView alloc]init];
  • UITableView的分割線默認(rèn)是開頭空15像素點(diǎn)的(好像是15來著~~),產(chǎn)品經(jīng)理有時(shí)候希望能夠定格顯示,那么你可能會這么做。
        self.tableView.separatorInset = UIEdgeInsetsZero;

但是你很快就會發(fā)現(xiàn)這么做并沒有效果,這是因?yàn)?lt;code>separatorInset</code>這個(gè)屬性在iOS7以后就已經(jīng)失效了,但是我們還是能夠達(dá)到同樣的效果,你可以在你的tablevView的代理協(xié)議實(shí)現(xiàn)界面加上下面這段代碼:

/**
 *  分割線頂頭
 */
-(void)viewDidLayoutSubviews
{
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.tableView setSeparatorInset:UIEdgeInsetsMake(0,0,0,0)];
    }
    
    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [self.tableView setLayoutMargins:UIEdgeInsetsMake(0,0,0,0)];
    }
}
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}

再次運(yùn)行,好了我們的UITableView終于頂頭顯示分割線了。

  • 很多情況下我們的UITableViewCell的高度是動態(tài)不確定的,比如說很多聊天的界面都需要我們?nèi)討B(tài)的計(jì)算cell的高度,你可能會在<code>heightForRowAtIndexPath</code>代理協(xié)議方法中返回你計(jì)算好的cell高度,然后在蘋果推出約束以后,我們其實(shí)有更加方便的方法去實(shí)現(xiàn)相同的效果。你可以嘗試在你的代碼中加入以下兩行代碼:
 self.tableView.estimatedRowHeight = 68.0;
 self.tableView.rowHeight = UITableViewAutomaticDimension;

再次運(yùn)行你的程序,其實(shí)你發(fā)現(xiàn)了好像你的cell并沒有動態(tài)的返回高度,這是因?yàn)樯厦嬲f了,這兩行代碼必須配合約束來使用。
我們拖出一個(gè)SB,然后在cell上放上一個(gè)label,講label的<code>numberOfLines</code>屬性設(shè)置為0,然后設(shè)置好label的上下左右約束,然后再對label的內(nèi)容進(jìn)行賦值,再次運(yùn)行你的程序,這個(gè)時(shí)候你的cell就會動態(tài)的顯示高度了,label的高度取決于你的內(nèi)容的多少,同時(shí)按照你的約束進(jìn)行顯示。
-你可能寫過這樣下面這樣的代碼

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:true];
    [tableView beginUpdates];
    ROW--;//此操作表示減少數(shù)據(jù)源的個(gè)數(shù)。
    [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];
    [tableView endUpdates];
}

用一個(gè)動畫來刪除某一個(gè)cell,其中有兩行代碼特別有意思:

    [tableView beginUpdates];
    [tableView endUpdates];

這倆吧其實(shí)和<code>[tableView reloadData]</code>作用類似,但是這倆貨卻能非常輕松的創(chuàng)造出不錯(cuò)的效果,比如說和我們上一點(diǎn)說的用約束來控制label的行高相結(jié)合的是的時(shí)候,我們先來看一下效果:

一個(gè)tableView點(diǎn)擊縮放的效果

其實(shí)我的代碼很少,核心代碼只有以下幾行:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:true];
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    UILabel *label = [cell.contentView viewWithTag:1000];
    [tableView beginUpdates];
    if (label.numberOfLines == 0) {
        label.numberOfLines = 1;
    }else{
        label.numberOfLines = 0;
    }
    [tableView endUpdates];
}

我用SB創(chuàng)建了一個(gè)UITableView,然后在cell上放置了一個(gè)label,初始化label 的<code>numberOfLines</code>然后在界面上設(shè)置tableView

self.tableView.estimatedRowHeight = 68.0;
self.tableView.rowHeight = UITableViewAutomaticDimension;

然后在他的點(diǎn)擊動作中改變label的<code>numberOfLines</code>,同時(shí)結(jié)合使用:

[tableView beginUpdates];
 [tableView endUpdates];

像上面po出來的代碼那樣,這個(gè)時(shí)候你如果使用[tableView reloadData]也能夠達(dá)到改變cell高度的效果,但是界面上就不會有使用[tableView beginUpdates]那么流暢,以此類推,其實(shí)在很多地方都可以用<code>[tableView beginUpdates]</code>來代替<code>[tableView reloadData]</code>來達(dá)到更好的效果.

  • 你可能會經(jīng)常忽略UITableView的一些屬性和回調(diào),必須下面這個(gè)方法:
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    CGFloat offSet = tableView.contentOffset.y;
    if (offSet<=0) {
        return;
    }
    CGRect oldRect = cell.frame;
    CGRect newRect = cell.frame;
    newRect.origin.x += 50;
    cell.frame = newRect;
    [UIView animateWithDuration:0.5 animations:^{
        cell.frame = oldRect;
    }];
}

如果你這么寫會簡單的有一個(gè)展示的動畫,這個(gè)回調(diào)就是在cell展示到屏幕的時(shí)候發(fā)起的動作。
還有這個(gè)屬性:<code>tableView.visibleCells</code>,你的產(chǎn)品經(jīng)理可能會要求你的cell在滾動的時(shí)候進(jìn)行一些展示類的動畫----滾動的時(shí)候進(jìn)行展開收起之類的,這樣的話你可以這么做:

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    for (UITableViewCell *cell in _tableView.visibleCells) {
        /**
         *  你可以在這里對當(dāng)前的cell進(jìn)行一些操作
         *
         */
    }
}

這個(gè)屬性會返回即將展示到屏幕上的cell,而放在這個(gè)滾動的回掉中你就可以對你的cell進(jìn)行不停的調(diào)整了,具體能做出什么動畫,就靠你的想象能力了。

  • tableView可能會造成你的Controller過于龐大,或許你可以使用MVVM類似的構(gòu)架來瘦身你的Controller。。。。。。
    更多好文:http://www.strongx.cn
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容