javascript single if

運用operator
condition && document.write("condition is true");

或使用if讓可讀較佳
if (condition) document.write("condition is true");

又或是用在變數
var myVariable = (condition) ? "condition is true" : "condition is false";

Allow loading of JSON files in Visual Studio Express 2013 for Web

如果讀取JSON file遇到404.3 - Not Found,可按照下列步驟解決

● open cmd

● run command
   cd C:\Program Files (x86)\IIS Express

● run command
    appcmd set config /section:staticContent /+[fileExtension='.json',mimeType='application/json']

How to convert HashMap into ArrayList


// HashMap
HashMap< String, MyCustomDataClass> mData;

// Convert to ArrayList
ArrayList< MyCustomDataClass> myList = new ArrayList(mData.values());

Breaking out of nested loops in Java

如何跳出巢狀Function內某個特定的loop

label1 : for(int i =0;;)
{
     for(int g =0;;)
     {
         break label1;
     }
}

上述label1就是想跳出的loop區塊,幫他取名為label1後,就可以指名跳出!

[Note]ViewPager & ViewPagerAdapter & ViewPageStateAdapter

-ViewPager

  • allows u to flip left & right to show data
  • often conjunction with fragment, which is convenience to manage lifecycle of page
  • there are two standard adapter  'ViewPageAdapter' & 'ViewPageStateAdapter' implemented using fragment, which cover the most common use cases

-ViewPageAdapter
  • this adapter is best for use when there are static fragments to be paged through, such as a set of tabs


-ViewPageStateAdapter

  • this adapter is best for use when there are a large number of pages, working like the listview

iOS custom table cannot scroll to the bottom

最近用XCode開了一個簡單的*.nib file,裡面只有一個Table View,其中Table View有透過IB設定Row Height為80,並額外客製一個*.nib file給Table View Cell用,除此之外都用預設條件!

呈現資料時遇到一個小問題,最後一列資料老是被遮住一半
本來是想到Status Bar有自身高度需要扣掉的問題,在viewDidLoad寫了偵測高度並扣除的程式

    BOOL isPortrait = self.interfaceOrientation == UIInterfaceOrientationPortrait;
    CGSize statusBarSize = [UIApplication sharedApplication].statusBarFrame.size;
    CGFloat statusBarHeight = (isPortrait ? statusBarSize.height : statusBarSize.width);
    NSLog(@"statusBarHeight:%f", statusBarHeight);
    NSLog(@"statusBarHeight:%f", self.myTable.frame.size.height - statusBarHeight);
    self.myTable.frame= CGRectMake(self.myTable.frame.origin.x, self.myTable.frame.origin.y, self.myTable.frame.size.width, self.myTable.frame.size.height - statusBarHeight);

效果不好~還是有少部分被遮住@@

後來才發現是使用AutoLayout的後遺症,原來在AutoLayout爽爽用的情況下,也要注意Constraints設定,設定正確就可以免寫一堆醜陋程式碼汙染專案XD

設定前

設定後

UIActionSheet is deprecated in iOS 8

如題,需要依照System Version寫condition code,新版(>=8.0)用UIAlertController處理,舊版用則續用UIActionSheet

●取System Version
NSString *currentSystemVersion = [[UIDevice currentDevice] systemVersion];

●使用UIAlertController撰寫
UIAlertController Example in iOS
UIAlertController from Apple Document


真心覺得UIAlertController好用多了,每個actionItem所觸發的事件都能獨立開來寫,程式碼也比較好看XD
且h file也不用特地實作UIActionSheetDelegate protocol (UIActionSheetDelegate is also deprecated in iOS 8)

後來,我又補寫了一篇詳細很多的文章,主要是描述在新舊版(以iOS 8.0區分)撰寫ActionSheet,其中還包含安插圖片給Action
如何在iOS的ActionSheet加上圖片

Use marco globally in Object-C

之前寫Android專案會習慣把Common Function & Constants 統一定義在一支class,供其他class引用,這邊筆記一下iOS的作法

● 直接定義在*.pch file

● 統一定義在*.h file
需要用的人import此h file即可,例如MyConstants.h

//  MyConstants.h
#import
@interface CommonConstant : NSObject
#define TEST_MARCO1           @"MyFirsrMarco"

// other class use MyConstants.h
#import "MyConstants.h"
– (void)viewDidLoad
{
    NSString *test = TEST_MARCO1;
}

The logging tag can be at most 23 characters

Android Studio最近升級後會在logging tag的地方報錯
    『The logging tag can be at most 23 characters, ....
        Log tags are only allowed to be at most 23 characters long.』
解決方式不是改tag長度,就是把Error關掉或降低層級

從功能選單「Analyze→Inspect Code」開啟如下視窗,並點選紅色框起處

承上,開啟如下視窗後在搜尋框輸入log
會看到Too Long Log Tags,可以選擇關閉或是把右側的Security層級降低


參考資料
The logging tag can be at most 23 characters

取得UITableViewCell被點擊的UIButton所在cell index

網路上較常見的方法
1. 照順序用index替每個UIButton加tag,要用的時候就可以直接取tag
2. 透過UIButton上層(superview)是UITableViewCell的原理,取得該cell index
做法可參考 Get button click inside UI table view cell

後來找到一個較好的做法,如下
-(IBAction)actionBtn:(id)sender {
    CGPoint buttonPosition = [sender  convertPoint:CGPointZero  toView:self.myTable];
    NSIndexPath *indexPath = [self.myTable  indexPathForRowAtPoint:buttonPosition];
 
    ...
}
直接透過UIButton被click的位置,搭配 - (NSIndexPath *)indexPathForRowAtPoint:(CGPoint)point方法取得cell index

Call a selector in a different class

邊看內部iOS Project邊做練習,發現專案除了第三方的引用,完全沒code reuse耶XDD
這邊記錄一下怎麼改寫

假設A class內有個button,按下去會執行saveArray這個function
但是很多class都會執行一模一樣的saveArray,為了方便與易於維護,將saveArray移至MyLibrary class

●原本是所有function硬幹全寫在一個A class
[saveButton addTarget:self action:@selector(saveArray) forControlEvents:UIControlEventTouchUpInside];

此處的saveArray位於A class


●改呼叫MyLibrary class
[saveButton addTarget: [MyLibrary class] action:@selector(saveArray) forControlEvents:UIControlEventTouchUpInside];

此處的saveArray位於MyLibrary class,以後其他B、C、D class都可這樣寫,不用再瘋狂複製saveArray到各個class了