2014年10月29日 星期三

使用DataSource接外部資料提供自己的模組之用

模組平常都是用delegate去呼叫外部method來當作Callback使用,然而我們也可以利用delegate來作為DataSource去接外部資料回來使用。

//在.m中這樣呼叫你由外部輸入的Data
NSString *myKey = [self.dataSource getMyKey:self];

//在.h中這樣宣告
@protocol ReportChartDataSource;

@interface ReportChart : UIView

@property (weak, nonatomic) id<ReportChartDataSource> dataSource;

@property (nonatomic, readonly) ReportDuration duration;

- (void)reloadData;
- (void)reloadDataWithDuration:(ReportDuration)duration;

@end

//宣告實作
@protocol ReportChartDataSource <NSObject>

- (NSInteger) getMyKey:(ReportChart *)chart;

@end

NSMutableArray initWithCapacity 的用途

對於Mutable的物件,init不會配置足夠的記憶體,隨著內容物件的增加而重新配置記憶體,這會造成效能上的損失,為了解決這個問題,我們可以使用這個方法,在初始化時就先指定之後會用到的記憶體大小給你的Array,但不用擔心overflow因為如果使用超過了,仍然會幫你增加。
Returns an array, initialized with enough memory to initially hold a given number of objects.

Return Value

An array initialized with enough memory to hold numItems objects. The returned object might be different than the original receiver.

Discussion

Mutable arrays expand as needed; numItems simply establishes the object’s initial capacity.
This method is a designated initializer.

2014年10月28日 星期二

對NSArray中的ManagedObjects做排序

NSArray *recordMOArray = [self.petInfoMO.recordEntities allObjects];
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"date" ascending:NO];
    NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
    NSArray *sortedArray = [recordMOArray sortedArrayUsingDescriptors:sortDescriptors];
    
    NSDate* end = [NSDate date];

    NSDate* start = ((RecordMO *)[sortedArray firstObject]).date;

2014年10月17日 星期五

修改狀態列樣式

Go to Info tab of the project target, Add Row:

UIViewControllerBasedStatusBarAppearance, set value NO

Then in appdelegate.m

- (BOOL)application:(UIApplication *)application
    didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{

[application setStatusBarStyle:UIStatusBarStyleLightContent];
}

判別iOS是否是64位元機器(pre-compile的code)

#if __LP64__
    \\You're running on 64 bit
#else
    \\You're running on 32 bit
#endif

2014年10月9日 星期四

viewWillAppear在NavigationController下無法發揮作用

先宣告<UINavigationControllerDelegate>
然後再把NavigationController.delegate = self;

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    if ([viewController isEqual:self]) {
            [viewController viewWillAppear:animated];
    } else if ([viewController conformsToProtocol:@protocol(UINavigationControllerDelegate)]){
            // Set the navigation controller delegate to the passed-in view controller and call the UINavigationViewControllerDelegate method on the new delegate.
            [navigationController setDelegate:(id<UINavigationControllerDelegate>)viewController];
            [[navigationController delegate] navigationController:navigationController willShowViewController:viewController animated:YES];
    }
}
- (void)navigationController:(UINavigationController *)navigationController 
       didShowViewController:(UIViewController *)viewController 
                    animated:(BOOL)animated
{
    if ([viewController isEqual:self]) {
        [self viewDidAppear:animated];
    }
}

不錯的iOS開發網站

星期五Q&A
https://mikeash.com/pyblog/

2014年10月8日 星期三

CoreData的資料型態

我要的重點是decimal type是一種不會被四捨五入掉的小數型態!

節錄自這本書「Core Data iOS Essentials」

Types of attributes

Using the Type drop-down list control, we select the data type (that is, numerical, string, date, and so on) of the attribute to specify the kind of information that can be stored in the attribute. The following is the list of data types:
  • Integer 16, Integer 32, and Integer 64 data types are for storing signed integers. The range of values that these types are able to store is as follows:
    • Integer 16:-32,768 to 32, 767
    • Integer 32:-2,147,483,648 to 2,147,483,647
    • Integer 64:-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
  • Decimal, Double, and Float data types are for storing fractional numbers. The Double data type uses 64 bits to store a value while the Float data type uses 32 bits for storing a value. The only limitation with these two data types is that they round off the values. To avoid any rounding of values, the Decimaldata type is preferred. The Decimal type uses fixed point numbers for storing values, so the numerical value stored in it is not rounded off.
  • String data type is used for storing text contents.
  • Boolean data type is used for storing YES or NO values.
  • Date data type is used for storing dates as well as timestamps.
  • Binary data type is used for storing binary data.
  • Transformable data type works along with Value Transformers that help us create attributes based on any Objective-C class, that is, we can create custom data types other than the standard data types. This data type can be used to store an instance of UIColor, UIImage, and so on. It archives objects to instances of NSData.

2014年10月6日 星期一

tableView顯示多層cell的動畫

//有Cell出現的動畫
[tableView reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationNone];

//沒有動畫
[tableView reloadData];

要用reloadSections達到這樣子的動畫效果,可以用一個NSArray來包多個NSMutableArray代表不同的Section內容(二元矩陣),降子在改動MutableArray之後,才不會因為reloadSections造成程式crash。