YYKit源碼探究(二十一) —— UITableView分類(lèi)(一)

版本記錄

版本號(hào) 時(shí)間
V1.0 2018.03.25

前言

iOS圈內(nèi)有幾個(gè)人大家基本都知道,比如說(shuō)王巍、唐巧,還有YYKit框架的作者現(xiàn)任職于滴滴的郭曜源 - ibireme等。這里有一篇唐巧對(duì)他的專(zhuān)訪(fǎng),還有他的 GitHub - Yaoyuan博客,這里貼出來(lái)框架YYKit 框架。接下來(lái)幾篇我們就一起來(lái)看一下這個(gè)框架。感興趣的可以看上面寫(xiě)的幾篇。
1. YYKit源碼探究(一) —— 基本概覽
2. YYKit源碼探究(二) —— NSString分類(lèi)之Hash(一)
3. YYKit源碼探究(三) —— NSString分類(lèi)之Encode and decode(二)
4. YYKit源碼探究(四) —— NSString分類(lèi)之Drawing(三)
5. YYKit源碼探究(五) —— NSString分類(lèi)之Regular Expression(四)
6. YYKit源碼探究(六) —— NSString分類(lèi)之NSNumber Compatible(五)
7. YYKit源碼探究(七) —— NSString分類(lèi)之Utilities(六)
8. YYKit源碼探究(八) —— NSNumber分類(lèi)(一)
9. YYKit源碼探究(九) —— UIFont分類(lèi)之架構(gòu)分析和Font Traits(一)
10. YYKit源碼探究(十) —— UIFont分類(lèi)之Create font(二)
11. YYKit源碼探究(十一) —— UIFont分類(lèi)之Load and unload font(三)
12. YYKit源碼探究(十二) —— UIFont分類(lèi)之Dump font data(四)
13. YYKit源碼探究(十三) —— UIImage分類(lèi)之框架結(jié)構(gòu)和Create image部分(一)
14. YYKit源碼探究(十四) —— UIImage分類(lèi)之Image Info(二)
15. YYKit源碼探究(十五) —— UIImage分類(lèi)之Modify Image(三)
16. YYKit源碼探究(十六) —— UIImage分類(lèi)之Image Effect(四)
17. YYKit源碼探究(十七) —— UIImageView分類(lèi)之架構(gòu)和image部分(一)
18. YYKit源碼探究(十八) —— UIImageView分類(lèi)之highlight image部分(二)
19. YYKit源碼探究(十九) —— UIScreen分類(lèi)(一)
20. YYKit源碼探究(二十) —— UIScrollView分類(lèi)(一)

回顧

上一篇主要介紹了UIScrollView分類(lèi),這一篇主要看一下UITableView部分。


API

下面我們看一下API。

/**
 Perform a series of method calls that insert, delete, or select rows and
 sections of the receiver.
 
 @discussion Perform a series of method calls that insert, delete, or select
             rows and sections of the table. Call this method if you want
             subsequent insertions, deletion, and selection operations (for
             example, cellForRowAtIndexPath: and indexPathsForVisibleRows)
             to be animated simultaneously.
 
 @discussion If you do not make the insertion, deletion, and selection calls
             inside this block, table attributes such as row count might become
             invalid. You should not call reloadData within the block; if you
             call this method within the group, you will need to perform any
             animations yourself.
 
 @param block  A block combine a series of method calls.
 */
- (void)updateWithBlock:(void (^)(UITableView *tableView))block;

/**
 Scrolls the receiver until a row or section location on the screen.
 
 @discussion            Invoking this method does not cause the delegate to 
                        receive a scrollViewDidScroll: message, as is normal for 
                        programmatically-invoked user interface operations.
 
 @param row             Row index in section. NSNotFound is a valid value for
                        scrolling to a section with zero rows.
 
 @param section         Section index in table.
 
 @param scrollPosition  A constant that identifies a relative position in the 
                        receiving table view (top, middle, bottom) for row when 
                        scrolling concludes.
 
 @param animated        YES if you want to animate the change in position,
                        NO if it should be immediate.
 */
- (void)scrollToRow:(NSUInteger)row inSection:(NSUInteger)section atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated;

/**
 Inserts a row in the receiver with an option to animate the insertion.
 
 @param row        Row index in section.
 
 @param section    Section index in table.
 
 @param animation  A constant that either specifies the kind of animation to
                   perform when inserting the cell or requests no animation. 
 */
- (void)insertRow:(NSUInteger)row inSection:(NSUInteger)section withRowAnimation:(UITableViewRowAnimation)animation;

/**
 Reloads the specified row using a certain animation effect.
 
 @param row        Row index in section.
 
 @param section    Section index in table.
 
 @param animation  A constant that indicates how the reloading is to be animated,
                   for example, fade out or slide out from the bottom. The animation 
                   constant affects the direction in which both the old and the 
                   new rows slide. For example, if the animation constant is 
                   UITableViewRowAnimationRight, the old rows slide out to the 
                   right and the new cells slide in from the right.
 */
- (void)reloadRow:(NSUInteger)row inSection:(NSUInteger)section withRowAnimation:(UITableViewRowAnimation)animation;

/**
 Deletes the row with an option to animate the deletion.
 
 @param row        Row index in section.
 
 @param section    Section index in table.
 
 @param animation  A constant that indicates how the deletion is to be animated, 
                   for example, fade out or slide out from the bottom.
 */
- (void)deleteRow:(NSUInteger)row inSection:(NSUInteger)section withRowAnimation:(UITableViewRowAnimation)animation;

/**
 Inserts the row in the receiver at the locations identified by the indexPath, 
 with an option to animate the insertion.
 
 @param indexPath  An NSIndexPath object representing a row index and section 
                   index that together identify a row in the table view.
 
 @param animation  A constant that either specifies the kind of animation to
                   perform when inserting the cell or requests no animation.
 */
- (void)insertRowAtIndexPath:(NSIndexPath *)indexPath withRowAnimation:(UITableViewRowAnimation)animation;

/**
 Reloads the specified row using a certain animation effect.
 
 @param indexPath  An NSIndexPath object representing a row index and section
                   index that together identify a row in the table view.
 
 @param animation A constant that indicates how the reloading is to be animated,
                  for example, fade out or slide out from the bottom. The animation
                  constant affects the direction in which both the old and the
                  new rows slide. For example, if the animation constant is
                  UITableViewRowAnimationRight, the old rows slide out to the
                  right and the new cells slide in from the right.
 */
- (void)reloadRowAtIndexPath:(NSIndexPath *)indexPath withRowAnimation:(UITableViewRowAnimation)animation;

/**
 Deletes the row specified by an array of index paths, 
 with an option to animate the deletion.
 
 @param indexPath  An NSIndexPath object representing a row index and section
                   index that together identify a row in the table view.
 
 @param animation  A constant that indicates how the deletion is to be animated,
                   for example, fade out or slide out from the bottom.
 */
- (void)deleteRowAtIndexPath:(NSIndexPath *)indexPath withRowAnimation:(UITableViewRowAnimation)animation;

/**
 Inserts a section in the receiver, with an option to animate the insertion.
 
 @param section    An index specifies the section to insert in the receiving
                   table view. If a section already exists at the specified 
                   index location, it is moved down one index location.
 
 @param animation  A constant that indicates how the insertion is to be animated, 
                   for example, fade in or slide in from the left.
 */
- (void)insertSection:(NSUInteger)section withRowAnimation:(UITableViewRowAnimation)animation;

/**
 Deletes a section in the receiver, with an option to animate the deletion.
 
 @param section    An index that specifies the sections to delete from the 
                   receiving table view. If a section exists after the specified
                   index location, it is moved up one index location.
 
 @param animation  A constant that either specifies the kind of animation to 
                   perform when deleting the section or requests no animation.
 */
- (void)deleteSection:(NSUInteger)section withRowAnimation:(UITableViewRowAnimation)animation;

/**
 Reloads the specified section using a given animation effect.
 
 @param section    An index identifying the section to reload.
 
 @param animation  A constant that indicates how the reloading is to be animated, 
                   for example, fade out or slide out from the bottom. The 
                   animation constant affects the direction in which both the 
                   old and the new section rows slide. For example, if the 
                   animation constant is UITableViewRowAnimationRight, the old 
                   rows slide out to the right and the new cells slide in from the right.
 */
- (void)reloadSection:(NSUInteger)section withRowAnimation:(UITableViewRowAnimation)animation;

/**
 Unselect all rows in tableView.
 
 @param animated YES to animate the transition, NO to make the transition immediate.
 */
- (void)clearSelectedRowsAnimated:(BOOL)animated;

下面我們就一起看一下這些方法。

1. - (void)updateWithBlock:(void (^)(UITableView *tableView))block;

執(zhí)行一系列方法調(diào)用,以插入,刪除或選擇行和部分。

執(zhí)行一系列方法調(diào)用,插入,刪除或選擇表格的行和部分。 如果希望后續(xù)插入,刪除和選擇操作(例如cellForRowAtIndexPath:indexPathsForVisibleRows)同時(shí)進(jìn)行動(dòng)畫(huà),請(qǐng)調(diào)用此方法。

如果您不在該塊內(nèi)進(jìn)行插入,刪除和選擇調(diào)用,則table屬性(如行數(shù))可能會(huì)失效。 您不應(yīng)該在塊中調(diào)用reloadData; 如果您在組中調(diào)用此方法,則需要自己執(zhí)行任何動(dòng)畫(huà)。

方法實(shí)現(xiàn)

下面我們就看一下方法實(shí)現(xiàn)。

- (void)updateWithBlock:(void (^)(UITableView *tableView))block {
    [self beginUpdates];
    block(self);
    [self endUpdates];
}

這里beginUpdatesendUpdates都是系統(tǒng)的API。

2. - (void)scrollToRow:(NSUInteger)row inSection:(NSUInteger)section atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated;

該方法的作用就是滾動(dòng)到屏幕指定的行或者section。

調(diào)用此方法不會(huì)導(dǎo)致代理接收scrollViewDidScroll:消息,正如程序調(diào)用的用戶(hù)界面操作一樣。

方法實(shí)現(xiàn)

下面看一下方法實(shí)現(xiàn)。

- (void)scrollToRow:(NSUInteger)row inSection:(NSUInteger)section atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated {
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:section];
    [self scrollToRowAtIndexPath:indexPath atScrollPosition:scrollPosition animated:animated];
}

3. - (void)insertRow:(NSUInteger)row inSection:(NSUInteger)section withRowAnimation:(UITableViewRowAnimation)animation;

該方法的作用就是插入一行到指定的位置,可動(dòng)畫(huà)。

方法實(shí)現(xiàn)

下面我們看一下方法的實(shí)現(xiàn)。

- (void)insertRow:(NSUInteger)row inSection:(NSUInteger)section withRowAnimation:(UITableViewRowAnimation)animation {
    NSIndexPath *toInsert = [NSIndexPath indexPathForRow:row inSection:section];
    [self insertRowAtIndexPath:toInsert withRowAnimation:animation];
}

- (void)insertRowAtIndexPath:(NSIndexPath *)indexPath withRowAnimation:(UITableViewRowAnimation)animation {
    [self insertRowsAtIndexPaths:@[indexPath] withRowAnimation:animation];
}

4. - (void)reloadRow:(NSUInteger)row inSection:(NSUInteger)section withRowAnimation:(UITableViewRowAnimation)animation;

該方法的作用是使用指定的動(dòng)畫(huà)效果刷新指定的行。

  • animation:例如,指示如何重新加載動(dòng)畫(huà)的常數(shù)會(huì)從底部淡出或滑出。 動(dòng)畫(huà)常量會(huì)影響舊行和新行的滑動(dòng)方向。 例如,如果動(dòng)畫(huà)常量為UITableViewRowAnimationRight,則舊行向右滑動(dòng),新單元格從右側(cè)滑入。

方法實(shí)現(xiàn)

下面看一下方法的實(shí)現(xiàn)。

- (void)reloadRow:(NSUInteger)row inSection:(NSUInteger)section withRowAnimation:(UITableViewRowAnimation)animation {
    NSIndexPath *toReload = [NSIndexPath indexPathForRow:row inSection:section];
    [self reloadRowAtIndexPath:toReload withRowAnimation:animation];
}

- (void)reloadRowAtIndexPath:(NSIndexPath *)indexPath withRowAnimation:(UITableViewRowAnimation)animation {
    [self reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:animation];
}

5. - (void)deleteRow:(NSUInteger)row inSection:(NSUInteger)section withRowAnimation:(UITableViewRowAnimation)animation;

該方法的作用就是刪除指定的行,可動(dòng)畫(huà)。

方法實(shí)現(xiàn)

下面我們就看一下方法的實(shí)現(xiàn)。

- (void)deleteRow:(NSUInteger)row inSection:(NSUInteger)section withRowAnimation:(UITableViewRowAnimation)animation {
    NSIndexPath *toDelete = [NSIndexPath indexPathForRow:row inSection:section];
    [self deleteRowAtIndexPath:toDelete withRowAnimation:animation];
}

- (void)deleteRowAtIndexPath:(NSIndexPath *)indexPath withRowAnimation:(UITableViewRowAnimation)animation {
    [self deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:animation];
}

6. - (void)insertRowAtIndexPath:(NSIndexPath *)indexPath withRowAnimation:(UITableViewRowAnimation)animation;

在指定的NSIndexPath處插入指定的行,可動(dòng)畫(huà)。

方法實(shí)現(xiàn)

- (void)insertRowAtIndexPath:(NSIndexPath *)indexPath withRowAnimation:(UITableViewRowAnimation)animation {
    [self insertRowsAtIndexPaths:@[indexPath] withRowAnimation:animation];
}

7. - (void)reloadRowAtIndexPath:(NSIndexPath *)indexPath withRowAnimation:(UITableViewRowAnimation)animation;

在指定的NSIndexPath處刷新指定的行,可動(dòng)畫(huà)。

方法實(shí)現(xiàn)

- (void)reloadRowAtIndexPath:(NSIndexPath *)indexPath withRowAnimation:(UITableViewRowAnimation)animation {
    [self reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:animation];
}

8. - (void)deleteRowAtIndexPath:(NSIndexPath *)indexPath withRowAnimation:(UITableViewRowAnimation)animation;

在指定的NSIndexPath處刪除指定的行,可動(dòng)畫(huà)。

方法實(shí)現(xiàn)

- (void)deleteRowAtIndexPath:(NSIndexPath *)indexPath withRowAnimation:(UITableViewRowAnimation)animation {
    [self deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:animation];
}

9. - (void)insertSection:(NSUInteger)section withRowAnimation:(UITableViewRowAnimation)animation;

在指定的section處插入指定的section,可動(dòng)畫(huà)。

方法實(shí)現(xiàn)

- (void)insertSection:(NSUInteger)section withRowAnimation:(UITableViewRowAnimation)animation {
    NSIndexSet *sections = [NSIndexSet indexSetWithIndex:section];
    [self insertSections:sections withRowAnimation:animation];
}

10. - (void)deleteSection:(NSUInteger)section withRowAnimation:(UITableViewRowAnimation)animation;

刪除指定的section,可動(dòng)畫(huà)。

方法實(shí)現(xiàn)

- (void)deleteSection:(NSUInteger)section withRowAnimation:(UITableViewRowAnimation)animation {
    NSIndexSet *sections = [NSIndexSet indexSetWithIndex:section];
    [self deleteSections:sections withRowAnimation:animation];
}

11. - (void)reloadSection:(NSUInteger)section withRowAnimation:(UITableViewRowAnimation)animation;

刷新指定的section,可動(dòng)畫(huà)。

方法實(shí)現(xiàn)

- (void)reloadSection:(NSUInteger)section withRowAnimation:(UITableViewRowAnimation)animation {
    NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:section];
    [self reloadSections:indexSet withRowAnimation:animation];
}

12. - (void)clearSelectedRowsAnimated:(BOOL)animated;

解除tableview中所有行的選中,可動(dòng)畫(huà)。

方法實(shí)現(xiàn)

- (void)clearSelectedRowsAnimated:(BOOL)animated {
    NSArray *indexs = [self indexPathsForSelectedRows];
    [indexs enumerateObjectsUsingBlock:^(NSIndexPath* path, NSUInteger idx, BOOL *stop) {
        [self deselectRowAtIndexPath:path animated:animated];
    }];
}

后記

本篇主要內(nèi)容就是UITableView分類(lèi),感興趣的可以關(guān)注和給個(gè)贊~~~~

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

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