iOS UIActionSheet

UIActionSheet在部份選單中還蠻好用的,其中如果有不需要的按鈕只要將String部份輸入為"nil"按鈕就會自動消失了!

UIActionSheet *myActionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"cancel" otherButtonTitles:@"AAA", @"BBB", @"CCC", @"DDD", nil];
myActionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;
[myActionSheet showInView:self.view];


使用時如果有需要知道按下的按鈕是哪一個記得在.h檔中加入"UIActionSheetDelegate",並在.m檔中加入以下Delegate,"buttonIndex"就是由上往下算的按鈕數目,從"0"開始。
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
NSLog(@"buttonIndex = %d", buttonIndex);
}


相關連結
iOS Developer Library
iOS Developer Library - UIActionSheet

iOS UIWebView loadRequest

這在iOS中算是蠻常用的東西,不過我每次用完就忘了,在這Memo一下。

其中因為WebView放在ViewController內,所以使用"self.view.frame"來取得CGRect
UIWebView *myWeb = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:@"URL Here!"]];
[myWeb loadRequest:request];
[self.view addSubview:myWeb];


如果需要在NavigationController內直接push出一個ViewController請使用以下方式
UIViewController *myViewController = [[UIViewController alloc] init];
UIWebView *myWeb = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, myViewController.view.frame.size.width, myViewController.view.frame.size.height)];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:@"URL Here!"]];
[myWeb loadRequest:request];
[myViewController.view addSubview:myWeb];
[self.navigationController pushViewController:myViewController animated:YES];


相關連結
iOS Developer Library
iOS Developer Library - UIWebView Class Reference

iOS UITableView update cell 更新TableViewo單一cell內容

tableView 真的是iOS中相當關鍵的一個UI,一方面是列表方式在呈現資料可以說是最好的方式,讓使用者快速得到想要的資訊,在其中也因用途不同需要客製化的部份相當多,就如下面要介紹的更新單一cell。

以下myTableView為UITableView的名稱
//更新指定cell
NSIndexPath *path = [NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section];
NSArray *myArray = [NSArray arrayWithObjects:path, nil];
[myTableView reloadRowsAtIndexPaths:myArray withRowAnimation:UITableViewRowAnimationFade];


如果需要更新所按下的cell可以搭配UITableViewDelegate使用
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSIndexPath *a = [NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section];
NSArray *my = [NSArray arrayWithObjects:a, nil];
[myTableView reloadRowsAtIndexPaths:my withRowAnimation:UITableViewRowAnimationFade];
}


其中UITableViewRowAnimation有相當多的動畫方式可以使用,當然也可以不要動畫,只要在"withRowAnimation:"輸入"UITableViewRowAnimationNone"即可達到直接修改cell並且無動畫。

NSArray的部份也可以一次加入多個需要更新的cell,如果是需要一次更新所有的cell可以使用以下方式
[myTableView reloadData]

相關連結
iOS Developer Library
iOS Developer Library - UITableView Class Reference

iOS navigation to the address 導航到指定地址

Google的導航真的很好用,在iOS上Google Maps的使用方式也比起在Android上好用很多,光是要申請KEY,還需要弄一堆有的沒的就夠麻煩了,這邊就來解說一下如何使用iOS的Maps導航功能,順便就將寫成了個function來方便使用。

-(void)navivationToTheAddress:(NSString *)address{
//導航起點的經緯度
NSString* myLatitude = @"0.0";
NSString* myLongitude = @"0.0";
NSString* urlString = [NSString stringWithFormat:@"http://maps.google.com/maps?f=d&source=s_d&saddr=%@,%@&daddr=%@", myLatitude, myLongitude, address];

//將地址的中文轉碼
NSString *escaped = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

//開啟Maps導航
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:escaped]];
}


相關連結
iOS Developer Library

iOS UITableView cell and section height

TableView在iOS4所寫相同的Code,在iOS5 build起來卻有不同的結果,最後找出原因在iOS4以前TableView預設的section高度為0(簡單來說就是看不到),但在iOS5卻為預設高度(好像是20,剛好一行字)。上架(Apple Store)後並不會出現這問題,所以若已經確定為上架版本可以放心。

iOS5上需要特別去修正過section的高度來讓他消失,此function回傳值即為section的高度。
//section height
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 0.0;
}


修改Cell高度即為
//cell height
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;{
//set cell size
return 100.0;
}


相關連結
iOS Developer Library
iOS Developer Library - UITableView
iOS Developer Library - UITableViewController

iOS UITableView seleted blue background

TableView預設在點選同時會產生藍色的背景作為Highlight,在某些狀況這是好事,可以清楚的標示出目前所點到的那行,但部份狀況下是不需要他的,其實只要簡單的一行就可以讓他消失掉。

只需要在UITableView的Delegate內加入這行,也就是下面這function內加入

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {}


// cell 為cell的名稱,也就是所定義的"UITableViewCell *cell"
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];


相關連結
iOS Developer Library
iOS Developer Library - UITableView
iOS Developer Library - UITableViewController

Android Gallery switch to position移動至指定位置

需要將Gallery、ListView、GridView移動至指定位置皆可使用此語法。

setSelection(position, animate)

Sample
imageGallery.setSelection(position, animate)


相關連結
Android Developer
Android Developer - Gallery
Android Developer - ListView
Android Developer - GridView

MySQL Client on Android

現在App對於網路的依賴度越來越高,畢竟資料如果是死的,沒辦法分享給朋友,不能按讚、+1、Tweet、Plurk......等的因素實在很痛苦,Android的SDK對於SQLite有完整的支援,但SQLite畢竟還是個local的資料存取,想要更新資料還是需要透過網路,一般App在這種狀態下大概可以分為三種方式。

1. 每次開啟App將所有資料更新
優點:一次將所有資料更新完成,在後續使用上可以完全不必在進行網路存取,減少每次等候時間,常見於電子書
缺點:安全性倍受考驗,若資料有安全性考量應盡量避免,資料量若太大會佔用到過多的容量空間,Mobile Device不是電腦上的HDD,容量是用G來計算

2. 即時存取 - 使用Api界接
優點:由後台對資料整理過後再進行傳輸,減少掉不必要的資訊,快速達成所需,製作一次可供多種平台做使用
缺點:執行製作上必須多花一份工

3. 即時存取 - 直接Query資料庫
優點:省去後台多做一次工
缺點:每次使用都需要與資料庫連線才可以取得資料,Mobile Device上的網路狀況並不像電腦的網路環境,常常會遇上無法順利與資料庫產生連線導致無法取得資訊

不過今天所要談的重點為MySQL Client,所以當然是講"3. 即時存取 - 直接Query資料庫",在這邊我選擇直接使用JDBC來溝通,直接進入重點

Step 1.下載JDBC http://dev.mysql.com/downloads/connector/j/5.0.html#downloads
因為Android對於JAVA支援版本關係,無法使用最新版,經過測試後發現3.0.17這版本就以足夠使用


Step 2. 將JDBC的Jar檔案加入至專案中

Step 3. Sample code
private Connection connect = null;
private Statement statement = null;
private ResultSet resultSet = null;

public static final String MYSQL_IP = "192.168.0.100";
public static final String MYSQL_DBNAME = "Book";
public static final String MYSQL_USERNAME = "isken";
public static final String MYSQL_PASSWORD = "isken";

public ArrayList bookList() throws Exception {
ArrayList results = new ArrayList();
try {
String script = "SELECT id, name, URL FROM Book";
Log.e("Isken", "script = "+script);

// This will load the MySQL driver, each DB has its own driver
Class.forName("com.mysql.jdbc.Driver");

Log.e("Isken", "jdbc:mysql://"+MYSQL_IP+"/+MYSQL_DBNAME+?"+ "user="+MYSQL_USERNAME+"&password="+MYSQL_PASSWORD);

// Setup the connection with the DB
connect = (Connection) DriverManager.getConnection("jdbc:mysql://"+MYSQL_IP+"/+MYSQL_DBNAME+?"+ "user="+MYSQL_USERNAME+"&password="+MYSQL_PASSWORD);

Log.e(PAGETAG, "connection is success");

// Statements allow to issue SQL queries to the database
statement = (Statement) connect.createStatement();

// Result set get the result of the SQL query
resultSet = statement.executeQuery(script);

while (resultSet.next()) {
MyObj obj = new MyObj();
String id = resultSet.getString("id");
String name = resultSet.getString("name");
String url = resultSet.getString("URL");
obj.setId(id);
obj.setName(name);
obj.setURL(url);
results.add(obj);
}
Log.e(PAGETAG, "results size = "+results.size());
} catch (Exception e) {
throw e;
} finally {
close();
}
return results;
}



相關連結
Android Developer
MySQL Developer Zone
Sample Code

Android Google Map Streetview 街景服務

街景服務也是個Google很強大的功能之一,但這功能如果只能使用在一般的PC上實在有點可惜,當然iOS、Android也有相關服務可以使用,在此介紹Android上的使用方式

double objLatitude = 25.066319;
double objLongitude =  121.557541;
Intent streetView = new Intent(android.content.Intent.ACTION_VIEW,Uri.parse("google.streetview:cbll="+ objLatitude+","+objLongitude+"&cbp=1,99.56,,1,-5.27&mz=21"));
startActivity(streetView);


相關連結
Android Developer
Android Developer - Activity

Git

Git init
git init

Saving State
git commit -m "[content]"

Saving State
git commit "[content]"

Add all files
git add .

Add all file
git add [file name] [file name] [file name] ...

Remove file
git rm [file name]

Rename file
git mv [old name] [new name]

Log
git log

Undo
git reset --hard [path]