2014年1月10日 星期五

判斷GPS訊號強度

/*
* @ 取得指定 Location 的 GPS 訊號強度
* - Judge the Gps singal strength of limit location.
*/
-(KRGpsSingalStrength)singalStrengthWithLocation:(CLLocation *)_location
{
    //水平精度
    //CLLocationAccuracy horizontal = newLocation.horizontalAccuracy;
    //垂直精度
    //CLLocationAccuracy vertical = newLocation.verticalAccuracy;
   
    CLLocationAccuracy _horizontalAccuracy = _location.horizontalAccuracy;
    KRGpsSingalStrength _singalStrength = KRGpsSingalStrengthNone;
    if( (_horizontalAccuracy > 0.0f) && (_horizontalAccuracy <= 10.0f) )
    {
        _singalStrength = KRGpsSingalStrengthPerfect;
    }
    else if( (_horizontalAccuracy > 10.0f) && (_horizontalAccuracy <= 30.0f) )
    {
        _singalStrength = KRGpsSingalStrengthStrong;
    }
    else if( (_horizontalAccuracy > 30.0f) && (_horizontalAccuracy <= 60.0f) )
    {
        _singalStrength = KRGpsSingalStrengthHigh;
    }
    else if( (_horizontalAccuracy > 60.0f ) && (_horizontalAccuracy <= 100.0f) )
    {
        _singalStrength = KRGpsSingalStrengthMiddle;
    }
    else if( (_horizontalAccuracy > 100.0f) && (_horizontalAccuracy <= 200.0f))
    {
        _singalStrength = KRGpsSingalStrengthLow;
    }
    return _singalStrength;
}

2014年1月9日 星期四

iOS 7 的UITableView上面會空一行空白的問題

//在你的UIViewController中加入這行,關掉該屬性。
if([self respondsToSelector:@selector(setAutomaticallyAdjustsScrollViewInsets:)]){
      [self setAutomaticallyAdjustsScrollViewInsets:NO];
}


2014年1月8日 星期三

直接顯示URL上的圖片

//設定圖片的url位址
                NSURL *url = [NSURL URLWithString:[content objectForKey:@"url"]];
                UIImage *urlImage = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:url]];

                cell.imageView.image = urlImage;

2014年1月7日 星期二

透過Xib初始化要記得實作initWithCoder

- (id)initWithCoder:(NSCoder *)aDecoder{
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self initWithParameter];
    }
    return self;

}

- (void)initWithParameter{
    //初始化參數
}

客製化UITableView

//在要用到的TableViewController中實作這裡
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //為某個種類的Cell給一個特定的名字作為區分
    static NSString *CellIdentifier = @"Cell";
    //如果TableView裡還有Cell的話,把cell叫出來使用
    StoresOnePlaceCell *cell = (StoresOnePlaceCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    //如果叫不出來cell,就new一個新的cell
    if (cell == nil)
    {
        cell = [[StoresOnePlaceCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    
    // Configure the cell...
    
    [cell setCustomViewWithStoreInfo:[self.stores objectAtIndex:indexPath.row]];
    
    return cell;
}


//在你客製化的Cell中實作這二個方法,在其中引入你要客製化的View
#import "StoresOnePlaceCell.h"
#import "MapStoreView.h"

static NSInteger _kMapStoreViewTag = 7001;

@implementation StoresOnePlaceCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

-(void)setCustomViewWithStoreInfo:(NSDictionary *)storeInfo
{
    MapStoreView *storeView = (MapStoreView *)[self.contentView viewWithTag:_kMapStoreViewTag];
    BOOL _isMakeNew         = NO;
    if( !storeView )
    {
        storeView  = [MapStoreView new];
        [storeView setTag:_kMapStoreViewTag];
        _isMakeNew = YES;
    }
    storeView.people = [[storeInfo objectForKey:@"instore_account_count"] integerValue];
    storeView.name = [storeInfo objectForKey:@"name"];
    storeView.items = [[storeInfo objectForKey:@"total_items"] integerValue];
    storeView.hotness = [[storeInfo objectForKey:@"hotness"] integerValue];
    storeView.followed = [[storeInfo objectForKey:@"total_followed"] integerValue];
    storeView.distance = [[storeInfo objectForKey:@"distance"] floatValue];
    [storeView make];
    [storeView setUserInteractionEnabled:NO];
    if( _isMakeNew )
    {
        [self.contentView addSubview:storeView];
    }
}

-(CGFloat)getCustomViewHeight
{
    MapStoreView *storeView = (MapStoreView *)[self.contentView viewWithTag:_kMapStoreViewTag];
    return storeView.frame.size.height;
}


@end

2014年1月3日 星期五

//TODO:超方便

在code加入//TODO: ...就會在method列表中用明顯的粗黑體顯示出來

你可以在共同開發時,告訴對方要做什麼

也可以在自己開發時,先將method架構寫好後先塞//TODO:(必須加上說明),之後再寫要實作的部份

注意,這個不能寫到Block中,不然不會有作用

2014年1月2日 星期四

NSDate的操作

時間與文字

//NSString => NSDate


NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *date = [dateFormatter dateFromString:@"2014-01-02 14:16:30"];
NSLog(@"%@", date);
[dateFormatter release];


//NSDate => NSString


NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSString *strDate = [dateFormatter stringFromDate:[NSDate date]];
NSLog(@"%@", strDate);
[dateFormatter release];



//時間的加減

NSDate * anHourAgo = [now dateByAddingTimeInterval:-60*60];
NSDate * anHourAfter = [now dateByAddingTimeInterval:60*60];

//只取某個時間單位

NSDateComponents *comps = [[NSCalendar currentCalendar] components:NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit | NSHourCalendarUnit fromDate:record.sleepDate];

index = comps.hour - startComps.hour;


//取得時間差
NSTimeInterVal duration = [now timeIntervalSinceDate:lastTime];

//時間的比較
if ([date compare:previousDate] == NSOrderedAscending) {
    //TODO:date 比 previousDate 來的小
}
if ([date compare:previousDate] == NSOrderedSame) {
    //TODO:date 跟 previousDate 是相同時間
}
if ([date compare:previousDate] == NSOrderedDescending) {
    //TODO:date 比 previousDate 來的大
}

//Sample codes
    date = [NSDate date];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"YYYY-MM-dd";
    formatter.timeZone = [NSTimeZone defaultTimeZone];
    NSMutableString *dateStr = [NSMutableString stringWithString:[formatter stringFromDate:date]];
    NSDateComponents *comps = [[NSCalendar currentCalendar] components:NSHourCalendarUnit | NSMinuteCalendarUnit fromDate:se.wakeUpDate];
    [dateStr appendFormat:@" %02d:%02d:00", comps.hour, comps.minute];

    formatter.dateFormat = @"YYYY-MM-dd HH:mm:ss";