iOS 關於UITableView的黑科技


  UITableView是我們最常用的控件了,今天我就來介紹一些關於UITableView的黑科技和一些注意的地方。

1.修改左滑刪除按鈕的高度

  左滑刪除這是iOS最先發明的,之后安卓開始模仿。有時候我們需要對他進行自定義,比如添加圖片啊,修改字體和大小啊,其實這個可以很簡單。

- (void)layoutSubviews {
    
    [super layoutSubviews];
    
    for (UIView *subview in self.subviews) {
        if ([subview isKindOfClass:NSClassFromString(@"UITableViewCellDeleteConfirmationView")]) {
            // 修改左滑刪除按鈕的高度
            CGRect frame = subview.frame;
            frame.size.height = self.frame.size.height - 10;
            subview.frame = frame;
            
            for (UIButton*deleteBtn in subview.subviews) {
                
                if ([deleteBtn isKindOfClass:[UIButton class]]) {
                    
                    // deleteBtn 就是那個刪除按鈕 在這里自定義按鈕的字體、背景色、添加圖片等
                    [deleteBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
                }
            }
            
            break;
        }
    }
}

  UITableViewCellDeleteConfirmationView就是刪除按鈕所在的View。我這次的需求如下圖:

調整deleteBtn高度.png
由於刪除按鈕的高度和cell的高度是一致的,所以會出現左面的效果。不過用戶看起來就會很別扭,因為用戶理解的cell是不包括灰條的,所以我們就需要修改刪除按鈕的高度,其實也就是修改UITableViewCellDeleteConfirmationView的高度。同時,我們也可以自定義按鈕的樣式,例如背景色,字體,添加圖片等。
  但是我發現這樣自定義刪除按鈕之后,刪除起來非常卡頓,因為每次cell布局都需要執行一次for循環,影響效率。所以我強烈建議用下來的方法。

- (void)willTransitionToState:(UITableViewCellStateMask)state {
    
    [super willTransitionToState:state];
    if (state == UITableViewCellStateShowingDeleteConfirmationMask) {

        for (UIView *subview in self.subviews) {
        if ([subview isKindOfClass:NSClassFromString(@"UITableViewCellDeleteConfirmationView")]) {
            // 修改左滑刪除按鈕的高度
            CGRect frame = subview.frame;
            frame.size.height = self.frame.size.height - 10;
            subview.frame = frame;
            
            for (UIButton*deleteBtn in subview.subviews) {
                
                if ([deleteBtn isKindOfClass:[UIButton class]]) {
                    
                    // deleteBtn 就是那個刪除按鈕 在這里自定義按鈕的字體、背景色、添加圖片等
                    [deleteBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
                }
            }
            
            break;
        }
    }
  }
}

  不過遍歷UITableViewCellDeleteConfirmationView必須延時執行,因為UITableViewCellDeleteConfirmationView是懶加載的,不延時執行,遍歷不到UITableViewCellDeleteConfirmationView。

2.刪除cell之后刷新表格

  刪除cell之后需要刷新表格,以前我都是用- (void)reloadData; ,但是如果表格里的cell很多,頻繁的刪除,手機就卡死了。因為我們只是刪除一行,卻需要刷新整個表格,太浪費資源了。我就想有沒有可能像只刷新一行、一段那樣的方法呢,后來我發現了下面的方法,非常好。

- (void)deleteSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation;

他的用法和刷新一行一段完全一樣,但是animation這個參數應該是無效的,因為你無論怎么改動畫都沒有改變。

    [self.tableView beginUpdates];
    [self.tableView deleteRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:index inSection:0]] withRowAnimation:UITableViewRowAnimationNone];
    [self.tableView endUpdates];

注意:1.一定要在刪除數據源之后,在執行上面的方法。否則會崩潰,報下面的錯誤:

*** Assertion failure in -[ShopCarTableView _endCellAnimationsWithContext:], /SourceCache/UIKit/UIKit-3318.16.21/UITableView.m:1582
libc++abi.dylib: terminate_handler unexpectedly threw an exception

2. [self.tableView beginUpdates]; [self.tableView endUpdates];兩句代碼可以省略,分別表示開始更新和結束更新,這個更新包括insert、delete、move、reload。
3.用這個方法容易報下面的錯誤。

attempt to delete row 4 from section 0 which only contains 4 rows before the update

這是友盟返回的錯誤,我定位到錯誤的位置,反復的刪除cell,也沒有閃退。我就百思不得其解,上網百度也沒有發現有價值的信息。后來我發現:假設一個表格有5行,每行的index分別是0,1,2,3,4。如果我刪除了第四行,每行的index就應該變為0,1,2,3,也就是說原來的第五行變成了第四行,index應該是3,但用於沒有刷新整個表格,他的index仍然是4,我按照index = 4來刷新這個cell就報上面的錯誤。是不是有點復雜,沒懂。簡單一句話,執行了deleteRowsAtIndexPaths方法,表里的每個cell的indexPath都應該發生變化,但是沒有 。其實不是每個,應該是刪除那個cell下面的cell會有變化,便於理解所以說每個。
  這就尷尬了,是不是又得刷新整個表格了。當然不是了,但是我們不能直接用cell的indexPath刷新它了。每個cell都有一個數據,根據這個數據在整個數據源中的位置來重新獲取它的indexPath,刷新cell。

    GoodsModel *goods = notification.object[@"goods"];
    NSUInteger index = [self.tableView.shopCarArr indexOfObject:goods];
    [self.tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:index inSection:0]] withRowAnimation:UITableViewRowAnimationNone];

  好司機,都是用汽油堆起來的!說的真對,做的項目多了,遇到的問題也就多,迫使我們去解決問題,閱讀蘋果的API。官網API寫的真的很詳細,也很易懂,不需要有多高的英語造詣,對提高自己真的很有幫助。另外,這幾天的一句經驗之談,任何一個小問題,都不能放過,趕緊解決,否則有可能會釀成大錯。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM