国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

ios–使用UINib加載xib文件實現UITableViewCell

2019-11-14 19:49:53
字體:
來源:轉載
供稿:網友

 

xib文件的實質是xml,描述界面對象,每個對象都有一個很重要的屬性,identity inspector面板中class屬性,加載xib文件的時候實際上是實例化界面對象相對應的這些class。

xib文件的加載過程:

  1.將xib文件從磁盤載入內存,有兩種技術可以加載xib文件:NSBundle和UINib。

  2.執行unarchive和initialize操作,該過程主要由NSCoding PRotocol中的initWithCoder:(NSCoder *)decoder完成。

  3.建立connections:Outlets和Actions。Outlets使用IBOutlet關鍵字標示,使用setValue:forKey:方法建立每個Outlet,所以每個Outlet的建立都會發送KVO通知。Actions使用IBAction關鍵字標示,替換void返回值,通過調用addTarget:action:forControlEvents:方法建立每個Action連接。注意,這里構建Outlets和Actions是有先后順序的,先建立Outlets連接,隨后建立Actions連接。因為,Actions的建立依賴之前建立的Outlets。

  4.調用awakeFromNib方法,首先要調用super的awakeFromNib方法,之后可以設置一些個性化的操作,以及一些無法在設計時設定的操作。注意,awakeFromNib消息只發往在Interface Builder中指定的Custom Class,不會發送給Files's Owner,First Responder等占位對象。

  之后,該對象的加載完成,可以進行各式各樣的操作了。

使用NSBundle加載xib文件:

 

[[NSBundle mainBundle]loadNibNamed:<(NSString *)> owner:<(id)> options:<(NSDictionary *)>];

這是最常見的一種,loadNibNamed:owner:options:方法返回的是一個NSArray*,里面包含了所加載xib文件包含的界面對象(class)。

NSBundle每次都從磁盤上載入xib文件,而UINib則只是第一次從磁盤上載入xib文件,之后將xib文件緩存在內存中,每次新生成一個對象時,直接訪問內存中的xib文件執行上面描述的2-4步,所以性能上會有很大的提升,并且開發者文檔也建議對于那些重復使用的xib文件使用UINib 加載技術,當收到低內存警告時,會從內從中卸載xib文件,當再次訪問的時候會從磁盤中載入。下面看一下UINib的定義:

NS_CLASS_AVAILABLE_IOS(4_0) @interface UINib : NSObject {  @private    id storage;}// If the bundle parameter is nil, the main bundle is used.// Releases resources in response to memory pressure (e.g. memory warning), reloading from the bundle when necessary.+ (UINib *)nibWithNibName:(NSString *)name bundle:(NSBundle *)bundleOrNil;// If the bundle parameter is nil, the main bundle is used.+ (UINib *)nibWithData:(NSData *)data bundle:(NSBundle *)bundleOrNil;// Returns an array containing the top-level objects from the NIB.// The owner and options parameters may both be nil.// If the owner parameter is nil, connections to File's Owner are not permitted.// Options are identical to the options specified with -[NSBundle loadNibNamed:owner:options:]- (NSArray *)instantiateWithOwner:(id)ownerOrNil options:(NSDictionary *)optionsOrNil;@end

前面兩個方法很清楚,分別以不同的方式載入,第三個方法則是實例化(將對象創建出來)

表視圖實例:

具體的細節就不說了

創建一個名為Empty的xib文件

9ABC0D43-DDDA-401A-8DCA-30264F5C2CDB

注意看Table View Cell的class屬性是TableViewCell,不是默認的UITableViewCell,TableViewCell.h如下:

@interface TableViewCell : UITableViewCell@property (weak, nonatomic) IBOutlet UILabel *lb1;@property (weak, nonatomic) IBOutlet UILabel *lb2;- (IBAction)bt:(id)sender;@end

三個屬性都和xib文件進行了鏈接,應該能看出來。

然后在UITableViewDataSource代理中分別進行如下操作:

//頭文件內聲明UINib *nib;//實例化self->nib = [UINib nibWithNibName:@"Empty" bundle:nil];

然后在來看tableView:cellForRowAtIndexPath:方法,這個方法標準的實現方法如下:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {  static NSString *CellIdentifier = [NSString stringWithFormat:@"Cell"];  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];  if (cell == nil) {    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];  }  //config the cell  return cell;}

這是使用代碼的方式創建cell ,下面看使用UINib:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    static NSString *identifier = @"Cell";    TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];    if (cell == nil) {        cell = [[nib instantiateWithOwner:nil options:nil] objectAtIndex:0];    }        switch (indexPath.section) {        case redSection:            cell.lb1.text = @"lb1";            cell.lb2.text = @"lb2";            break;        case blueSection:            break;        default:            [[cell textLabel] setText:@"Unknow"];    }    return cell;}

效果圖如下:

7B576949-8D4E-4584-BC0E-2DB65F6CF6C1

以上只是個人理解,有錯誤之處歡迎指正。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 嵊州市| 凤城市| 海林市| 元阳县| 始兴县| 永新县| 宜丰县| 洛扎县| 临颍县| 平乡县| 远安县| 岳池县| 荔浦县| 乐都县| 斗六市| 日土县| 安徽省| 天气| 武宣县| 凤阳县| 孝义市| 长泰县| 苏尼特左旗| 含山县| 宣威市| 仲巴县| 介休市| 怀宁县| 连州市| 澄城县| 衢州市| 特克斯县| 阿拉善右旗| 汉阴县| 桓台县| 平凉市| 武宣县| 泾阳县| 泸定县| 聂荣县| 墨竹工卡县|