2011年1月12日

[iPhone]設計自已的UITableViewCell


我想要做一個自已的UITableViewCell,於是在iCodeBlog找到了一篇文章"Custom UITableViewCell Using Interface Builder",上面還附有影片,可以很清楚的了解每一個步驟。

1.首先,我們先建立一個tableViewController和他的xib檔,並記得要設定section數量和列數量。
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return 10;
}

2.設定完成之後,新建一個Object-C class "yourStyleCell.m" ,subclass選項選UITableViewCell,並建立一個xib檔,
3.接下來我們使用Interface Builder,把原本的view刪除,從Library把UITableViewCell拉進來,並且設計自已想要的樣式,然後把這個xib檔的類別設定為"yourStyleCell"
4.接下來我們在"yourStyleCell"類別中建立所需要的IBOutlet並且和xib檔做link
5.最後我們回到tableViewController的.h檔中,import剛剛建立的"yourStyleCell.h"檔,
如果有變更cell的高度,請記得在tableViewController的table也要同樣變更row的高度

接下來我們要對下面這個function進行修改
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"yourStyleCell";//這是我們在yourStyleCell.xib檔中對cell進行的定義值,
yourtStyleCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
//初始化cell
if (cell == nil)
{
NSArray *nibObjs = [[NSBundle mainBundle] loadNibNamed:@"yourStyleCell" owner:nil options:nil];
for ( id currentObj in nibObjs)
{
if ([currentObj isKindOfClass:[ yourtStyleCell class]])
{
cell = ( yourtStyleCell *)currentObj;
}
}
}
// 定義cell的內容
NSInteger row = [indexPath row];//get row number
// 如果是我們有一個Label,名稱為Name,我們可以這樣設定
[[cell Name] setText:@"Name"];
// 如果是我們有一個UIImageView,名稱為image,我們可以這樣設定
[[cell image] setImage:[UIImage imageNamed:@"yourImageFile.png"]];
return cell;
}


這樣就完成了我們自已style的cell了。

2011年1月8日

[iPhone]如同keyborad滑出的datepicker



我要做一個點選table
cell
之後,會像鍵盤一樣滑出的datepicker



首先我先在要顯示datepicer的tableview類別的.h檔中宣告了一個UIDatePicker的變數
UIDatePicker *datepicker;//代表datepicker的物件


並定義他的property
@property (nonatomic,retain) UIDatePicker *datepicker;


接下來到tableview的.m檔當中synthesize
@synthesize datepicker;


要記得在最後要把這個變數release
- (void)dealloc
{
[datepicker release];
[super dealloc];
}


同時我們要定義一個記錄datepicker狀態的bool變數,用來記錄datepicker下一次要顯示還是隱藏
BOOL datepickerShow;
在 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath
*)indexPath 
這個function中,初始化datepicker
NSInteger row = [indexPath row];
switch (row)
{
  case 0:
  if (datepicker == nil)
  //
如果datepicker還沒定義要先初始化
  {
    self.datepicker = 
          [[UIDatePicker alloc]initWithFrame:CGRectMake(0.0,367,320,216)];
    //決定datepicker的初始位置,因為在我的程式裡有tabbar,因此我設定y值為367


    [self.view addSubview:datepicker];//把datepicker物件加入目前的view中
    datepickerShow= YES;//把狀態初始化成yes
   }
[self showDatepicker:datepickerShow];
//呼叫顯示datepicker的function,會依照目前datepicker狀態來決定要顯示或隱藏
  break;

case 1:
[self showDatepicker:NO];//點選其他行datepicker會隱藏
  break;

case 2:
[self showDatepicker:NO];//點選其他行datepicker會隱藏
  break;
default:
  break;
}




最後加入顯示datepicker的function
- (void)showDatepicker:(bool)show
{
 CGRect startRect = CGRectMake(0.0,367,320,216);//定義datepicker開始的位置(在view外面)
 CGRect endRect = CGRectMake(0.0,171,320,216);//定義datepicker結束的位置(在view裡面)
 [UIView beginAnimations:nil context:NULL];
 [UIView setAnimationCurve:5];
 if(YES==show) //若要顯示,要從view外面移進裡面
   [datepickersetFrame:endRect];
 else//從view裡移到外面
   [datepicker setFrame:startRect];
 [UIView commitAnimations];
 datepickerShow =!show;//變更狀態
}




這樣就完成我們的滑動datepicker了

[iPhone] setDateFormat 顯示格式代號


我想用12小時制來顯示時間,以下是我的程式碼…

    

NSDate *currentDateandTime = [NSDate date];
    
NSDateFormatter *date_formater=[[NSDateFormatter alloc]init];
[date_formater setDateFormat:@"YYYY / MM / dd"];
NSString *date = [date_formater stringFromDate:currentDateandTime];//date data
    
[date_formater setDateFormat:@"KK : mm aa"];
NSString *time = [date_formater stringFromDate:currentDateandTime];//time data
 
 


 

首先是先取得目前的時間和日期物件NSDate,存入currentDateandTime,

接下來指定顯示的格示,setDateFormat:@"YYYY/MM/dd",這樣會顯示成 2011/01/08

setDateFormat:@"KK:mm aa",會顯示成 02:51 PM


 

以下是setDateFormat可使用的英文代號:



紀元的顯示:
G:顯示AD,也就是公元
 
年的顯示:
yy:年的後面2位數字
yyyy:顯示完整的年
 
月的顯示:
M:顯示成1~12,1位數或2位數
MM:顯示成01~12,不足2位數會補0
MMM:英文月份的縮寫,例如:Jan
MMMM:英文月份完整顯示,例如:January


日的顯示:
d:顯示成1~31,1位數或2位數
dd:顯示成01~31,不足2位數會補0
 
星期的顯示:
EEE:星期的英文縮寫,如Sun
EEEE:星期的英文完整顯示,如,Sunday

 

上/下午的顯示:
aa:顯示AM或PM


小時的顯示:
H:顯示成0~23,1位數或2位數(24小時制)
HH:顯示成00~23,不足2位數會補0(24小時制)
K:顯示成0~12,1位數或2位數(12小時制)
KK:顯示成0~12,不足2位數會補0(12小時制)


分的顯示:
m:顯示0~59,1位數或2位數
mm:顯示00~59,不足2位數會補0


秒的顯示:
s:顯示0~59,1位數或2位數
ss:顯示00~59,不足2位數會補0
S: 毫秒的顯示

時區的顯示:
z / zz /zzz :PDT
zzzz:Pacific Daylight Time
Z / ZZ / ZZZ :-0800
ZZZZ:GMT -08:00
v:PT
vvvv:Pacific Time