2016年4月15日 星期五

iOS筆記:UITableView Programming

這次在練習利用code把UITableView建立出來
1. 在'h'中宣告delegate 跟 datasource.

@interface ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
@property (strong, nonatomic) UITableView *tableview;
@end
    2.

@interface ViewController ()
{
    NSMutableArray *testArray;
}
@end

@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    _tableview = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    [self.view addSubview:_tableview];
    _tableview.delegate = self;
    _tableview.dataSource = self;
    testArray = [NSMutableArray arrayWithObjects:@"Iron Man", @"Spiderman", @"Superman", @"Batman", nil];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [testArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
    }
    cell.textLabel.text = [testArray objectAtIndex:indexPath.row];
    return  cell;
}
@end

Error

Solution:
1. http://stackoverflow.com/questions/25826383/when-to-use-dequeuereusablecellwithidentifier-vs-dequeuereusablecellwithidentifi
2. http://stackoverflow.com/questions/12737860/assertion-failure-in-dequeuereusablecellwithidentifierforindexpath

參考:
1. https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/TableView_iPhone/CreateConfigureTableView/CreateConfigureTableView.html

2016年4月14日 星期四

iOS筆記:iAd Banner

  1. 將iAd framework加到專案中:
    在左側的 Project Navigator 中點選Project
    再點選TARGETS中的專案, 標籤選擇 General
    在 Linked Frameworks and Libraries 點選'+'
    尋找iAd.framework 再按 'add'便成功加進專案

enter image description here

  1. 在 ViewController.h 中 import 'iAd.h'檔案並加上 delegate
#import <iAd/iAd.h>

@interface ViewController : UIViewController <ADBannerViewDelegate>
  1. 拖曳或是建立一個Banner View
    (也要將BannerView的Delegate連到ViewController)
    enter image description here

or

@interface ViewController ()
{
    ADBannerView *bannerTestView;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    bannerTestView = [[ADBannerView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - 50, 320, 50)];
    bannerTestView.delegate = self;
    // 預設為隱藏 load成功後再在 delegate 的 method 中顯示
    bannerTestView.alpha = 0.0;
    [self.view addSubview:bannerTestView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)bannerViewDidLoadAd:(ADBannerView *)banner
{
    //banner 廣告載入
    NSLog(@"Ad Banner did load ad.");
    // Show the ad banner.
    [UIView animateWithDuration:0.5 animations:^{
        bannerTestView.alpha = 1.0;
    }];
}

- (BOOL)bannerViewActionShouldBegin:(ADBannerView *)banner willLeaveApplication:(BOOL)willLeave
{
    //使用者點了banner廣告後開啟廣告內容畫面
    return YES;
}

-(void)bannerViewActionDidFinish:(ADBannerView *)banner
{
    //使用者關掉廣告內容畫面
}

- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
    //廣告載入錯誤時會呼叫此方法
}

2016年4月2日 星期六

C/C++ 筆記:typedef struct

typedef用法

定義一個已知資料型態的別名, 也就是說可以用這個名稱代替設定的資料型態.

    typedef int NewINT; //幫int取一個別名叫NewINT

typedef struct

    typedef struct Node
    {
        int no;
        char Name[20];
    } NewNode;

上述程式可以分成兩個部分來看:
1. 就是定義一個Node的結構.
2. 幫這個Node的結構取一個別名叫NewNode.

結合起來的結論就是“定義一個Node的結構並給他一個等義的別名NewNode”

Example


``````
    typedef struct CSNode
    {
        int no;
        char Name[20];
    } MyNode, CSTree;

意義:宣告一個CSTree變數, 其為一個CSNode的結構, 別名為MyNode.