Thứ Ba, 25 tháng 10, 2016

Các khai báo cơ bản trong Object c

- Khai báo hằng:

#define SWITCH_TYPE ((int) 2)
#define MY_STRING_CONSTANT @"my_constant"

- Sử dụng NSArray:

Khai báo:
NSArray *arrayOne = @[@"Mercedes-Benz", @"BMW", @"Porsche",
                    @"Opel", @"Volkswagen", @"Audi"];

NSArray *arrayTwo = [NSArray arrayWithObjects:@"Aston Martin",
                    @"Lotus", @"Jaguar", @"Bentley", nil];

NSLog(@"phần tử đầu tiên: %@", arrayOne[0]);
NSLog(@"phần tử đầu tiên: %@", [arrayTwo objectAtIndex:0]);

Kiểm tra phần tử:
if ([arrayOne containsObject:@"BMW"]) {
    NSLog(@"BMW is a German auto maker");
}

- Sử dụng NSMulableArray:
// khai báo
NSMutableArray *arrayOne = [NSMutableArray new];

NSMutableArray *arrayTwo = [NSMutableArray arrayWithObjects: @"Audi A6", @"BMW Z3", @"Audi Quattro", @"Audi TT", nil];

// sử dụng
[arrayOne addObject:@"new obj"];
[arrayOne removeLastObject];
[arrayOne removeAllObjects];
[arrayOne removeObjectAtIndex:0];

// Add BMW F25 to front
[arrayOne insertObject:@"BMW F25" atIndex:0];
    
// Remove BMW F25 from front
[arrayOne removeObjectAtIndex:0];
    
// Remove Audi Quattro
[arrayOne removeObject:@"Audi Quattro"];

// Change second item to Audi Q5
[arrayOne replaceObjectAtIndex:1 withObject:@"Audi Q5"];

...


Thứ Hai, 3 tháng 10, 2016

Customize UITableView

- Tạo CustomCell:
+ Tạo file xib:

+ sửa file ViewController.m:


- (void)viewDidLoad {
    [super viewDidLoad];
    
    _uiTable.dataSource = self;
    _uiTable.delegate = self;
    
    [_uiTable registerNib:[UINib nibWithNibName:@"CustomCell" bundle:nil] forCellReuseIdentifier:@"MyCell"];

}

...

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyCell" forIndexPath:indexPath];
    
    [cell.label setLineBreakMode:NSLineBreakByWordWrapping];
    cell.label.numberOfLines = 0;
    cell.label.text = _dataArray[indexPath.row];
    cell.backgroundColor = [UIColor clearColor];
    return cell;
}
...

- Set background color:


- Căn trái sperator:

- Clear Footer operator:

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    // This will create a "invisible" footer
    return 0.01f;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    // To "clear" the footer view
    return [UIView new];

}

- Set height wrap label content:


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyCell" forIndexPath:indexPath];
    
    // label multi line:
    [cell.label setLineBreakMode:NSLineBreakByWordWrapping];
    cell.label.numberOfLines = 0;
    
    cell.label.text = _dataArray[indexPath.row];
    cell.backgroundColor = [UIColor clearColor];
    return cell;

}

// wrap label:
-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return UITableViewAutomaticDimension;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return UITableViewAutomaticDimension;

}

- Set background selected:

// change background color of selected cell
UIView *bgColorView = [[UIView alloc] init];
[bgColorView setBackgroundColor:[UIColor clearColor]];

[cell setSelectedBackgroundView:bgColorView];

- Khoá select

_contentTableView.allowsSelection = NO;

- Khoá kéo Scroll

_menuTableView.scrollEnabled = NO;

Thứ Bảy, 1 tháng 10, 2016


Đặt file .xib làm màn hình chính của ứng dụng

1. tạo project
2. New file như hình






3. Sửa nội dung file AppDelegate.m


#import "AppDelegate.h"
#import "MainViewController.h"

@interface AppDelegate ()

@property (strong, nonatomic) MainViewController *mainViewController;
@property (strong, nonatomic) UINavigationController *navController;

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    _window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    _navController = [[UINavigationController alloc] init];
    _navController.navigationBarHidden = YES;
    _mainViewController = [[MainViewController alloc] init];
    
    _window.rootViewController = [_navController initWithRootViewController:_mainViewController];
    
    return YES;

}

...