Tuesday, August 28, 2012

Format DateTime


a: AM/PM
A: 0~86399999 (Millisecond of Day)
 
c/cc: 1~7 (Day of Week)
ccc: Sun/Mon/Tue/Wed/Thu/Fri/Sat
cccc: Sunday/Monday/Tuesday/Wednesday/Thursday/Friday/Saturday
 
d: 1~31 (0 padded Day of Month)
D: 1~366 (0 padded Day of Year)
 
e: 1~7 (0 padded Day of Week)
E~EEE: Sun/Mon/Tue/Wed/Thu/Fri/Sat
EEEE: Sunday/Monday/Tuesday/Wednesday/Thursday/Friday/Saturday
 
F: 1~5 (0 padded Week of Month, first day of week = Monday)
 
g: Julian Day Number (number of days since 4713 BC January 1)
G~GGG: BC/AD (Era Designator Abbreviated)
GGGG: Before Christ/Anno Domini
 
h: 1~12 (0 padded Hour (12hr))
H: 0~23 (0 padded Hour (24hr))
 
k: 1~24 (0 padded Hour (24hr)
K: 0~11 (0 padded Hour (12hr))
 
L/LL: 1~12 (0 padded Month)
LLL: Jan/Feb/Mar/Apr/May/Jun/Jul/Aug/Sep/Oct/Nov/Dec
LLLL: January/February/March/April/May/June/July/August/September/October/November/December
 
m: 0~59 (0 padded Minute)
M/MM: 1~12 (0 padded Month)
MMM: Jan/Feb/Mar/Apr/May/Jun/Jul/Aug/Sep/Oct/Nov/Dec
MMMM: January/February/March/April/May/June/July/August/September/October/November/December
 
q/qq: 1~4 (0 padded Quarter)
qqq: Q1/Q2/Q3/Q4
qqqq: 1st quarter/2nd quarter/3rd quarter/4th quarter
Q/QQ: 1~4 (0 padded Quarter)
QQQ: Q1/Q2/Q3/Q4
QQQQ: 1st quarter/2nd quarter/3rd quarter/4th quarter
 
s: 0~59 (0 padded Second)
S: (rounded Sub-Second)
 
u: (0 padded Year)
 
v~vvv: (General GMT Timezone Abbreviation)
vvvv: (General GMT Timezone Name)
 
w: 1~53 (0 padded Week of Year, 1st day of week = Sunday, NB: 1st week of year starts from the last Sunday of last year)
W: 1~5 (0 padded Week of Month, 1st day of week = Sunday)
 
y/yyyy: (Full Year)
yy/yyy: (2 Digits Year)
Y/YYYY: (Full Year, starting from the Sunday of the 1st week of year)
YY/YYY: (2 Digits Year, starting from the Sunday of the 1st week of year)
 
z~zzz: (Specific GMT Timezone Abbreviation)
zzzz: (Specific GMT Timezone Name)
Z: +0000 (RFC 822 Timezone)

Ẩn bàn phím ảo trên iphone khi nhấn nút Return

1. Implement UITextFieldDelegate ở file.h
Giống dậy:

@interface MainViewController : UIViewController <UITextFieldDelegate>
{
    
    UIButton *btStartGame;
    UIButton *btHighScores;
    UITextField *tfName;
    
}
.......

2. Cho cái hàm ni dô file .m

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

3. Kích chuột phải do textfield kéo delegate đến File's Owner
Giống dậy


XONG...

@kent

Wednesday, August 22, 2012

Một số chú ý về quản lý bộ nhớ trên Iphon/Ipad

Nguồn: http://congdongios.com/showthread.php?108

+ Khi sử dụng alloc/copy/new bạn phải release ngay khi sử dụng chúng xong, thông thừong hãy đặt chúng trong dealloc 

+ Nếu là biến cục bộ thì release ngay trước khi return

+ Nếu nó là 1 biến được return lại trong hàm, thì hãy autorelease nó

+ cố gắng sử dụng alloc và release bằng tay hơn là sử dụng autorelease, chỉ sử dụng autorelease khi phải return về giá trị



Fine:
PHP Code:
- (NSDictionary *)testVariableValues {
    
NSMutableDictionary *returnValue = [[NSMutableDictionary alloc] init];
    [
returnValue setObject:[NSNumber      numberWithFloat:3.5] forKey:@”x”];
    [
returnValue setObject:[NSNumber numberWithFloat:23.8] forKey:@”y”];
    return [
returnValue autorelease];
}  
Better:
PHP Code:
- (NSDictionary *)testVariableValues {
    
NSMutableDictionary *returnValue = [NSMutableDictionary dictionary];
    [
returnValue setObject:[NSNumber numberWithFloat:3.5] forKey:@”x”];
    [
returnValue setObject:[NSNumber numberWithFloat:23.8] forKey:@”y”];
    return 
returnValue;
}  
Best:
PHP Code:
- (NSDictionary *)testVariableValues {
      return [
NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithFloat:3.5], @”x”, [NSNumber numberWithFloat:23.8], @”y”, nil];
}  


Bad:
PHP Code:
- (NSString *)tenThousandGs {
    
NSString *s = “”; for (int i = 0; i < 10000; i++) s = [s stringByAppendingString:@“G”];
    return 
s;
}  
Good:
PHP Code:
- (NSString *)tenThousandGs {
     
NSMutableString *s = [[NSMutableString alloc] init];
     for (
int i = 0; i < 10000; i++)
         [
s appendString:@“G”];
     return [
s autorelease];
}  
Best:
PHP Code:
- (NSString *)tenThousandGs {
}
NSMutableString *s = [NSMutableString string]; for (int i = 0; i < 10000; i++) [s appendString:@“G”]; return s;  

Sunday, August 19, 2012

Custom Cell trong UItableView với XIB

Bài tut này sẽ hướng dẫn các bạn custom cell theo ý mình trong tableView.Kết quả sau khi các bạn thực hành theo bài tut sẽ như thế này:



Đầu tiên tạo project mới:




Sau khi tạo xong nó ra ri:


Bây giờ New File.. để thêm một view Controller mới hỉ:


Nhớ tick dô cái With XIB ở dưới nhé mấy bác!
Subclass of là: UIViewController

Nó ra ri:


Chừ tick dô file: CustomViewController.xib.
Các bác thêm một button (button ni chẳng qua là làm nên, để cho table view nó có bầu tròn 4 góc), chỉnh kích thước như hình vẽ:


Tiếp theo các bác cho một Table View dô..đè lên button nghe:



Qua phần code:

CustomViewController.h:


#import <UIKit/UIKit.h>

@interface CustomViewController : UIViewController<UITableViewDataSource, UITabBarDelegate>
{
    UIButton *btBackground;
    UITableView *tbView;
}

@property(nonatomic, retain) IBOutlet UIButton *btBackground;
@property(nonatomic, retain) IBOutlet UITableView *tbView;

@end


Nhớ là implement <UITableViewDataSource, UITabBarDelegate> dô nhé.


Phần CustomViewController.m

#import "CustomViewController.h"

@implementation CustomViewController

@synthesize tbView;
@synthesize btBackground;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) 
    {
        
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    
    [super didReceiveMemoryWarning];
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    
}

- (void)viewDidUnload
{
    [super viewDidUnload];
   
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Table Section
//Khi mà implement <UITableViewDataSource, UITabBarDelegate>, ít nhất phải có mấy hàm sau //nghe mấy bác, chứ không là nó báo warning đó:

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

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{
   
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}

@end

Quay lại file CustomViewController.xib...Mấy bác kích chuột phải dô File's Owner...rồi kéo mấy cái tbView với btBackground dô đúng vị trí nghe:



Phần quan trọng, cái này mấy bác hay quên: kích chuột phải dô table view kéo dataSource với cái delegate dô chô File's Owner nhé:


Xong được một phần rồi, để đó xíu quay trở lại xúc tiếp...Chừ New File...tạo cái cell theo ý mình.
New File...chọn...:



Mấy bác nhớ Subclass là: UITableViewCell nhé:


Nó giống ri:



Viết code luôn hỉ:

CustomCell.h


#import <UIKit/UIKit.h>

@interface CustomCell : UITableViewCell
{
    UILabel *lbTile;
    UILabel *lbSmallTitle;
}

@property(nonatomic, retain) IBOutlet UILabel *lbTile;
@property(nonatomic, retain) IBOutlet UILabel *lbSmallTitle;

@end

CustomCell.m

#import "CustomCell.h"

@implementation CustomCell

@synthesize lbTile;
@synthesize lbSmallTitle;

- (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) dealloc
{
    [lbTile release];
    [lbSmallTitle release];
    [super dealloc];
}


@end

Tiếp tục New File (tạo XIB để thiết kế cái cell theo ý mình đó)...




ok...Kích dô cái CustomCell.xib, xoá cái view đi, thay vào đó là cái Table View Cell:




ok..trên cell này bạn có thể thiết kế cell kiểu gì cũng được, tuỳ ý nhé: ở đây mình cho 2 label ở hàng, nó giống dậy:


Kích dô hai cái label kéo nó dài ra..cho đủ thấy chữ nhé bác. Chọn cái label dưới set màu chữ cho nó màu xám đi, chỗ phần Text Color bên trái:


Tiếp tục cho cell mình xuất hiện cái nút hình tròn có dầu > bên phải, chọn theo hình:
Chọn Table view Cell trước nhé, rùi kích dô Accessory bên phải nhé (cái nút này hàm ý nhắc người dùng bấm vào một cell là ra một view controller mới...):


Tiếp theo là 2 bước quan trọng nhé: để nối xib ni với lớp CustomCell:

1. Chọn cái Table View Cell đánh giống cái tên lớp dô vị trí như hình vẽ:


2. Chọn lớp CustomCell như hình vẽ:



Tiếp theo kích chuột phải dô Custom Cell, khung Objects á...kéo hai cái lbSmaillTitile (label phía dưới) với lbTitle (label phía trên) dô đúng vị trí nhé:


Xong lớp Custom Cell rồi đó mấy bác. ^^

Chừ dô CustomViewController.m quất code tiếp: 
Sửa 2 chỗ nửa thôi:
Nhưng trước tiên phải có cái này trên đầu cùng: #import "CustomCell.h"

1 là chỗ lày:

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.btBackground.alpha=0.3; //cho background của button nó trong lại...giá trị càng nhỏ càng trong suốt.
    self.tbView.backgroundColor=[UIColor colorWithRed:0 green:0 blue:0 alpha:0];//Cho thèn table view mất màu luôn..
    self.view.backgroundColor=[UIColor blackColor]; //cho background của view controller màu đen
    self.btBackground.enabled=NO; // có bấm thèn button cũng không có dấu hiệu chi, nó chỉ background thui mà :)
}



và chỗ lày:

#pragma mark - Table Section

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  
    //Phần thiết lập nội dung cell

    static NSString *CellIdentifier = @"CustomCell";
    
    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];    
    if (cell == nil)
    {
        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:nil options:nil];
        
        for(id currentObject in topLevelObjects)
        {
            if([currentObject isKindOfClass:[CustomCell class]])
            {
                cell = (CustomCell *)currentObject;
                break;
            }
        }
    }
    
    cell.lbTile.text=@"Title Cell";
    cell.lbSmallTitle.text=@"Small Tiltle Cell";
    return cell;
    
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{
    
    return 15; //Số hàng trong một section, trong một bảng
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 61.0f; // chiều cao của hàng
}

Trước hết kiếp khổ đâu mấy bác, chừ làm cho View Controller: CustomViewController xuất hiện nửa là xong:

Dô file ni: AppDelegate.m  sửa lại cái hàm ni mấy bác:
Opp! import đã chớ, quất dòng ni dô đầu tiên nhé: #import "CustomViewController.h"

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
    
CustomViewController *customVC=[[CustomViewController alloc] initWithNibName:@"CustomViewController" bundle:[NSBundle mainBundle]];
    [self.window addSubview:customVC.view];
    self.window.backgroundColor = [UIColor blackColor];
    
[self.window makeKeyAndVisible];
    return YES;
}


OK..Xong kiếp khổ rồi đó mấy bác...Mấy bác bấm Run chạy thử xem kết quả giống như hình đầu tiên không...Hi vọng sẽ giúp được mấy bác. Hẹn bài tút khác...Bye

@kent











Thursday, July 5, 2012

Get to link: http://www.colourlovers.com/
And:


textLabel.textColor = [UIColor colorWithRed:0x06/255.0 green:0x87/255.0 blue:0x21/255.0 alpha:1];
Hex color: 068721

iPhone SDK Crayons Color


Clover Color  : [UIColor colorWithRed:0.000 green:0.557 blue:0.165 alpha:1.000]
Steel Color : [UIColor colorWithRed:0.475 green:0.475 blue:0.475 alpha:1.000]
Maraschino Color : [UIColor colorWithRed:1.000 green:0.118 blue:0.094 alpha:1.000]
Stell Color : [UIColor colorWithRed:0.475 green:0.475 blue:0.475 alpha:1.000]
Maroon Color : [UIColor colorWithRed:0.592 green:0.082 blue:0.310 alpha:1.000]
Tungsten Color : [UIColor colorWithRed:0.259 green:0.259 blue:0.259 alpha:1.000]
Ocean Color : [UIColor colorWithRed:0.000 green:0.333 blue:0.557 alpha:1.000]
Midnight Color : [UIColor colorWithRed:0.008 green:0.122 blue:0.549 alpha:1.000]
Iron Color : [UIColor colorWithRed:0.369 green:0.369 blue:0.373 alpha:1.000]
Plum Color : [UIColor colorWithRed:0.596 green:0.141 blue:0.553 alpha:1.000]
Tangerine Color : [UIColor colorWithRed:1.000 green:0.568 blue:0.196 alpha:1.000]

Crayons Color.png

http://tuduri.pe.kr/xe/iPhone/618

Sunday, July 1, 2012

Creating a custom Table View Cell programmatically

Ohhk, quite a lot of things done but this one is something which I have used most often. Custom cells can sometimes greatly push ahead the usability of your application. In this post I am going to create a test project which will demonstrate how to create custom cells and use them appropriately to provide better usability. The application will finally look like this:

So to start open xcode and create a new project, chose the template as “Navigation Based” and name it as “CustomCellTestProject”. What template you chose does not matter, refer my previous posts to find how you can start working on any template.
First thing we will do is create a customCell. Right click on Classes and add a new UITableViewCell subclass. Name it as “CustomCell”. Now open CustomCell.h and add the following code:
#import
@interface CustomCell : UITableViewCell {
UILabel *primaryLabel;
UILabel *secondaryLabel;
UIImageView *myImageView;
}
@property(nonatomic,retain)UILabel *primaryLabel;
@property(nonatomic,retain)UILabel *secondaryLabel;
@property(nonatomic,retain)UIImageView *myImageView;
@end

We create the three elements and added them to the contentView of our cell.
synthesize all the three elements in CustomCell.m as we are going to access these elements from other classes.
@synthesize primaryLabel,secondaryLabel,myImageView;
Here we have simply added a primary label to display the primary text, a secondary label and an imageView. These elements will be created and added into the content view of our custom cell. So open CustomCell.m and add the following code
- (id)initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuseIdentifier {
if (self = [super initWithFrame:frame reuseIdentifier:reuseIdentifier]) {
// Initialization code
primaryLabel = [[UILabel alloc]init];
primaryLabel.textAlignment = UITextAlignmentLeft;
primaryLabel.font = [UIFont systemFontOfSize:14];
secondaryLabel = [[UILabel alloc]init];
secondaryLabel.textAlignment = UITextAlignmentLeft;
secondaryLabel.font = [UIFont systemFontOfSize:8];
myImageView = [[UIImageView alloc]init];
[self.contentView addSubview:primaryLabel];
[self.contentView addSubview:secondaryLabel];
[self.contentView addSubview:myImageView];
}
return self;
}


Now, we have already added the UI elements into our cell but you must have noticed, we have not yet defined how these elements will appear inside cell. Go ahead and add the following code for that:
- (void)layoutSubviews {
[super layoutSubviews];
CGRect contentRect = self.contentView.bounds;
CGFloat boundsX = contentRect.origin.x;
CGRect frame;
frame= CGRectMake(boundsX+10 ,0, 50, 50);
myImageView.frame = frame;
frame= CGRectMake(boundsX+70 ,5, 200, 25);
primaryLabel.frame = frame;
frame= CGRectMake(boundsX+70 ,30, 100, 15);
secondaryLabel.frame = frame;
}

You can do anything in this method to define the lay out of cell. I have simply assigned frames to all the elements.
You can also find a method in CustomCell.m
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
This method can be used to define how your cell should react when it is selected. You can describe what should be the highlight color or may be you want to flash one of the labels anything of your choice. I am leaving this method as it is.
We are done with creating our custom cell and now we have to use it. Open RootViewController.m and import CustomCell.h at the top.

#import “CustomCell.h”
I am going to create 5 cells here, you can just use your own logic of specifying number of cells and data. So change the following method to look like this:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 5;
}
Now we are going to use our custom cell. If you look at the cellForRow method, you will find that a UItableViewCell has been created and re used. Now all we have to do is to replace this cell with our new cell. Change the code inside this method to look like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @”Cell”;
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[CustomCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
// Set up the cell…
switch (indexPath.row) {
case 0:
cell.primaryLabel.text = @"Meeting on iPhone Development";
cell.secondaryLabel.text = @"Sat 10:30";
cell.myImageView.image = [UIImage imageNamed:@"meeting_color.png"];
break;
case 1:
cell.primaryLabel.text = @"Call With Client";
cell.secondaryLabel.text = @"Planned";
cell.myImageView.image = [UIImage imageNamed:@"call_color.png"];
break;
case 2:
cell.primaryLabel.text = @"Appointment with Joey";
cell.secondaryLabel.text = @"2 Hours";
cell.myImageView.image = [UIImage imageNamed:@"calendar_color.png"];
break;
case 3:
cell.primaryLabel.text = @"Call With Client";
cell.secondaryLabel.text = @"Planned";
cell.myImageView.image = [UIImage imageNamed:@"call_color.png"];
break;
case 4:
cell.primaryLabel.text = @"Appointment with Joey";
cell.secondaryLabel.text = @"2 Hours";
cell.myImageView.image = [UIImage imageNamed:@"calendar_color.png"];
break;
default:
break;
}
return cell;
}

I have added some dummy data. You can use your data source to provide data for primaryLabel and secondaryLabel. Please note that I have used three images here. You can use any image of your choice. All you need to to do is copy the images and paste it into your project root folder (which in this case is CustomCellTestProject folder). After pasting the files, in xcode right click on Resources (or any group), select Add >> ExistingFiles and then select all the images you want to add in you project. Once added you can simply use them by their names.
Thats it, go ahead and run the project. You will find something wrong when you compare you simulator screen with mine. If you noticed in the CustomCell.m, I have given some frames to the UI elements. You need to make sure that the height of your cell large enough to accomodate all the elements. So add this following code and you fixed the issue:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 50;
}

http://jainmarket.blogspot.com/2009/05/creating-custom-table-view-cell.html