2018年12月14日 星期五

better way 更新tableview

當你一個小改變要更新cell的時候,reloadData 會導致很大的效能浪費,更慘的是tableview會有不必要的動畫被看到。

有一種解法是放delegate去給tableviewcell執行,這樣不用重繪tableview的整個cell,但還是會給所有的cell執行一次。

更好的作法是先更新tableview data,但不去reloadData,然後只更新看得見的部份
for (EmployeeListTableViewCell *cell in self.tableView.visibleCells) {
            //TODO: update the UI
}
剩下的交給tableView自己,在滑動到其他cell的時候更新。

2018年11月6日 星期二

UIApplicationWillChangeStatusBarOrientationNotification v.s. UIApplicationDidChangeStatusBarOrientationNotification


結論:
UIApplicationWillChangeStatusBarOrientationNotification
會先被呼叫,而且userInfo 是傳送將要轉換的 orientation。

UIApplicationDidChangeStatusBarOrientationNotification
後被呼叫,但userInfo是傳送轉換前的 orientation。

code:

    [NSNotificationCenter.defaultCenter addObserver:self selector:@selector(statusOrientationWillChange:)
                                               name:UIApplicationWillChangeStatusBarOrientationNotification object:nil];
   
    [NSNotificationCenter.defaultCenter addObserver:self selector:@selector(statusOrientationDidChange:)
                                               name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];

- (void)statusOrientationWillChange:(NSNotification *)notification {
    if (notification.userInfo != nil && notification.userInfo[UIApplicationStatusBarOrientationUserInfoKey] != nil) {
        UIInterfaceOrientation orientation = (UIInterfaceOrientation)[(NSNumber *)notification.userInfo[UIApplicationStatusBarOrientationUserInfoKey] integerValue];
        if (UIInterfaceOrientationIsLandscape(orientation)) {
            self.isLandscape = YES;
        } else {
            self.isLandscape = NO;
        }
    }
   
    [self.tableView reloadData];
}

- (void)statusOrientationDidChange:(NSNotification *)notification {
    if (notification.userInfo != nil && notification.userInfo[UIApplicationStatusBarOrientationUserInfoKey] != nil) {
        UIInterfaceOrientation orientation = (UIInterfaceOrientation)[(NSNumber *)notification.userInfo[UIApplicationStatusBarOrientationUserInfoKey] integerValue];
        if (UIInterfaceOrientationIsLandscape(orientation)) {
            self.isLandscape = YES;
        } else {
            self.isLandscape = NO;
        }
    }
   
}

Strange thing: A - 2 > 0 is not equvalent to A > 2

(lldb) po self.customFields.count - 2 > 0
true

(lldb) po self.customFields.count > 2
false

(lldb) po self.customFields
<__NSArrayM 0x108e54540>(

)

(lldb) po self.customFields.count
<nil>

2018年11月2日 星期五

reuse custom tableview cell

當你用dynamic style cell的時候
所有關於cell的改變都要基於cell source
所以最好的方式就是直接create a Class for table view source
讓整個改動都變得有彈性
不要直接去改當下那個cell,那是不合邏輯的,而且你會修bug修不完
例如,你從coredate的一個表中拿到資料,不要直接拿來用
創一個class去包它,這個class可以自由加入你要的其他設定給你的custom table cell

2018年10月18日 星期四

提早拿到constraint後的frame

加入viewDidLoad可以拿到autolayout後的正確frame

加在animation中,可以在autolayout的情況下作用
[UIView animateWithDuration:0.5 animations:^{
            self.filterViewWidthConstraint.constant = 200;
            [self.view layoutIfNeeded];
        }];

用pdf取代png

在asset中放入pdf,設成all scale,可以用來取代svg當作向量圖。

如果是icon的話,還可以設成templete,可以用tintColor來改變icon的顏色。

超級實用的小技巧

2018年10月15日 星期一

Add touch began to pan gesture

!!!!!!一定要做#import "UIPanGestureRecognizerSubclass.h"
然後用category或是subclass去設定這個state,
才能用readwrite不然,原本的recognizer是readonly










@interface TouchableImageView:UIImageView
@property(strong, nonatomic) UIPanGestureRecognizer *panGR; // 這裡要改成自己的recognizer或是實作一個category
@end

@implementation TouchableImageView
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    self.panGR.state = UIGestureRecognizerStateBegan;
}
@end
//直接改狀態就能trigger gesture的began!!