++ xib ++

xib文件實質是一個xml文件,類似於android用於描述界面;要先設置File』s Owner中的class屬性(目的是在xib中能找到Owner中的方法,類似android中xml與View的綁定);並建立File』s Owner跟控制項間的聯繫

[[NSBundle mainBundle] loadNibNamed:@"RowView" owner:self options:nil]

載入過程:

解析xib的xml文件 —-> 創建Objects下所有對象(xml所有控制項)—->根據xml的控制項及設定的屬性生成設置的相關代碼(同手動創建控制項的一樣)—->添加連線的事件(同手動用代碼創建的一樣)—-> 返回創建好的對象數組

[即是解析xib文件,根據xml描述生成創建控制項、設置屬性、添加事件的代碼,並放在一個數組中返回]

[但一般不用xib的File』s Owner綁定控制項中的事件,這樣耦合度高。使該View不能被其他ViewController使用;可使用Tag取得控制項並添加事件;但常用的是MVC模式,即一個xib用一個view來控制(類似於android中創建一個view.xml然後創建一個View類,通過該View類控制view的生成及數據填充)如下:

+(id)rowView{

//可在這裡面設置圖標和label

return [[NSBundle mainBundle] loadNibNamed:@"RowView" owner:nil options:nil][0];

}

+(id)rowView:(NSString *)icon name:(NSString *)name{

UIView *view = [RowView rowView];

UIButton *iconBtn = (UIButton *)[view viewWithTag:1];

[iconBtn setImage:[UIImage imageNamed:icon] forState:UIControlStateNormal];

UILabel *nameLabel = (UILabel *)[view viewWithTag:2];

nameLabel.text = name;

return view;

}

也可不用viewWithTag與尋找控制項,可直接綁定控制項到xib

1、將File』s Owner的class置空;

2、設置View的class為創建的view名(RowView);將View下的控制項與列表中出現的屬性名連線綁定;

+(id)rowViewIcon:(NSString *)icon withName:(NSString *)name{

RowView *view = (RowView *)[RowView rowView];

// UIButton *iconBtn = (UIButton *)[view viewWithTag:1];

[view.iconBtn setImage:[UIImage imageNamed:icon] forState:UIControlStateNormal];

// UILabel *nameLabel = (UILabel *)[view viewWithTag:2];

view.nameLable.text = name;

return view;

}

++ UIScrollView ++

scroll.showsVerticalScrollIndicator :滾動條

scroll.bounces;//彈簧效果

scroll.contentInset://在指定的方向上增加額外的區域

scroll.contentSize://滾動範圍

scrollView.contentOffset://scrollView滾動的位置,可用代碼來控制圖片滾動及分頁的索引

UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@「2.jpg」]];//可直接創建帶圖片的imageView,創建出來的imageView默認和圖片的寬高一樣

** 說明:-True-YES-true

objective-c 中的BOOL 實際上是一種對帶符號的字元類型(signed char)的類型定義(typedef),它使用8位的存儲空間。通過#define指令把YES定義為1,NO定義為0。

注意:objective-c 並不會將BOOL作為僅能保存YES或NO值的真正布爾類型來處理。編譯器仍將BOOL認作8位二進位數,YES 和 NO 值只是在習慣上的一種理解。

問題:如果不小心將一個大於1位元組的整型值(比如short或int)賦給一個BOOL變數,那麼只有低位位元組會用作BOOL值。如果該低位位元組剛好為0(比如8960,寫成十六進位為0x2300),BOOL值將會被認作是0,即NO值。而對於bool類型,只有true和false的區別,即0為false,非0為true。

++ UITableView ++

類似於 android 中的ListView,但功能比ListView強大(類似android中的ExpendListView);

設置數據源:實現代理:<UITableViewDataSource>

方法:

#pragma mark 數據源方法,第section組一共多少行,,若不實現,默認就是一組

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

#pragma mark 一共多少組(類似android的getCount)

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

#pragma mark 返回每一行顯示的內容(類似android中Adapter中的getView)

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

#pragma mark 返回每一行的頭部標題

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section

#pragma mark 返回每一行的尾部注釋

-(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section

#pragma mark 返回右邊的索引列表

-(NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView

//設置右邊的圖片,此處是沒向右的箭頭

cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

//自定義右邊的圖片,此處是為一個加號

cell.accessoryView = [UIButton buttonWithType:UIButtonTypeContactAdd;

#pragma mark 取消選中該行

-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath

+單組展示 +設置組數為1即可

UITableViewDelegate為table的代理方法可高,可設置列表的高寬

#pragma mark 設置每一行的高度,UITableViewDelegate下的代理方法

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

+對話框+(類似android)

alert.alertViewStyle =UIAlertViewStyleLoginAndPasswordInput;//自帶兩個輸入框,用戶名和密碼

[alert textFieldAtIndex:0].text =name;//訪問每一個文本框並設值

//使用自定義的xib的view,但創建的rowItem須指定Custom Class為自己的Cell類型

[_tableView registerNib:@"rowItem" forCellReuseIdentifier:@"key」];

推薦閱讀:

相關文章