2016年5月30日 星期一

tableViewWithXIB


首先都一樣,設置好viewcontroller,給他一個xib

一樣,注意:xib的file's owner   , connection inspector,

viewcontroller,放上tableview,




appdelegate記得設置root:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];

    return YES;

}




viewcontroller,記得給delegate:

<UITableViewDataSource,UITableViewDelegate>


客製化cell:

給一個tableviewcell類別,之後可以另外給他一個xib,

把tableviewcell這個元件,丟入cell的xib中,

之後cell不用設定file's owner,不過要設定cell的類別,


還有 identifier




viewcontroller中,

讓他依照不同的畫面,去加載不同的xib:

UINib* myCellNib = [UINib nibWithNibName:@"MyCell" bundle:nil];
    [self.myTableView registerNib:myCellNib forCellReuseIdentifier:@"myCell"];





&&&&&&&&&&&&&ps&&&&&&&&&&&&&&
這邊主要是設置說,當螢幕不同寬度,給不同的cell的xib

int device_width = [[NSNumber numberWithFloat:SCREEN_WIDTH] intValue];
    
    //NSLog(@"%d",device_width);
    
    if (device_width == 320) {
        [self.tableView registerNib:[UINib nibWithNibName:@"Br" bundle:nil]
             forCellReuseIdentifier:@"myCell"];
    } else if (device_width == 375) {
        [self.tableView registerNib:[UINib nibWithNibName:@"Bran" bundle:nil]
             forCellReuseIdentifier:@"myCell"];
    } else {
        [self.tableView registerNib:[UINib nibWithNibName:@"nc" bundle:nil]
             forCellReuseIdentifier:@"myCell"];

    };


下面這個則是說,當我近來這個controller之後,依照不同畫面大小,我要給他不同的xib

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    if (iphone6plus) {
        self = [super initWithNibName:@"h736" bundle:nibBundleOrNil];
    }
    else if(iphone6) {
        self = [super initWithNibName:@"h667" bundle:nibBundleOrNil];
    }
    else if (iphone5) {
        self = [super initWithNibName:@"h568" bundle:nibBundleOrNil];
    }
    else {
        self = [super initWithNibName:@"h480" bundle:nibBundleOrNil];
    }
    
    return self;

}


最後放上網址:

https://github.com/web8246/TestTableViewCellWithXIB


沒有留言:

張貼留言