iOS_14_tableViewController_xib建立和封裝自訂cell,ios自訂viewxib

來源:互聯網
上載者:User

iOS_14_tableViewController_xib建立和封裝自訂cell,ios自訂viewxib

最終:


xib+自訂cell的封裝標準步驟:(按此步驟者,事半功倍)


xib+自訂cell的封裝標準步驟:(按此步驟者,事半功倍) 1,建立GirlCell.xib,往裡面拖一個tableViewCell,在cell中拖入各種控制項 2,建立GirlCell類,繼承自uitableViewCell 3,將GirlCell.xib介面中cell的類名更改成GirlCell,並且指定重用的identifer,一般寫類名 4,將GirlCell.xib介面中所有的各種控制項,連線到類GirlCell中,使它們都成為GirlCell類的成員 5,建立資料模型 Girl,方法列表如下                // UI控制項用weak,字串用copy,其他對象用strong        // 頭像圖片名        @property(nonatomic,copy)NSString *headImgName;        // 姓名        @property(nonatomic,copy)NSString *name;        // 判詞        @property(nonatomic,copy)NSString *verdict;        // 類方法,字典 轉 對象 類似javaBean一次性填充        + (Girl *)girlWithDict:(NSDictionary *)dict;        // 對象方法,設定對象的屬性後,返回對象        - (Girl *)initWithDict:(NSDictionary *)dict;  6,封裝的GirlCell,方法列表如下          @property (weak, nonatomic) IBOutlet UIImageView *headImg;         @property (weak, nonatomic) IBOutlet UILabel *name;         @property (weak, nonatomic) IBOutlet UILabel *verdict;                  // 返回xib介面中cell的高度         + (CGFloat)cellHeight;                  // 返回xib介面上寫的重用cellID         + (NSString *)cellID;                  // 從xib中載入 執行個體化一個girlCell對象         + (GirlCell *)girlCell;                  // 參數是資料模型girl對象,填儲值到xib中各個控制項 ,並返回封裝好資料之後的,girlCell對象         - (GirlCell *)cellWithGirl:(Girl *)girl;  7,控制器中 cellForRow方法如下     - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath     {         // 把cell封裝之後,控制 器知道的東西就非常少了~                  // 先從緩衝池中,根據ID取出自已的cell類         GirlCell *girlCell = [tableView dequeueReusableCellWithIdentifier:[GirlCell cellID]];         if (girlCell == nil) {         // 如果池中沒取到,則類方法,重建一個嶄新的girlCell,xib介面中指定了重用cellID         girlCell = [GirlCell girlCell];         }         // 設定cell中獨一無二的內容         Girl *girl = [_girls objectAtIndex:indexPath.row];         // 參數為girl對象,填充其各個成員值到xib中各個控制項 ,並返回填充好的cell         girlCell = [girlCell cellWithGirl:girl];         // 返回cell         return girlCell;               }

建立tableViewController的標準步驟:


建立tableViewController的標準步驟: 1,建立工程,然後,刪除viewController.h和.m檔案 2,new File 選擇OC 類,繼承自UITableViewController 3,到main.storyboard中刪除 預設的viewController 4,拖動一個黃色的tableViewController到main.storyboard,並更改file's owner為自已經建立的BeyondTableViewController 5,仍然到main.storyboard中刪除 自動產生的cell控制項 6,運行既可



Girl.h


////  Girl.h//  12_tableView的增刪改////  Created by beyond on 14-7-27.//  Copyright (c) 2014年 com.beyond. All rights reserved.//#import <Foundation/Foundation.h>@interface Girl : NSObject// UI控制項用weak,字串用copy,其他對象用strong// 頭像圖片名@property(nonatomic,copy)NSString *headImgName;// 姓名@property(nonatomic,copy)NSString *name;// 判詞@property(nonatomic,copy)NSString *verdict;// 類方法,字典 轉 對象 類似javaBean一次性填充+ (Girl *)girlWithDict:(NSDictionary *)dict;// 對象方法,設定對象的屬性後,返回對象- (Girl *)initWithDict:(NSDictionary *)dict;@end



Girl.m


////  Girl.m//  12_tableView的增刪改////  Created by beyond on 14-7-27.//  Copyright (c) 2014年 com.beyond. All rights reserved.//#import "Girl.h"@implementation Girl// 類方法,字典 轉 對象 類似javaBean一次性填充+ (Girl *)girlWithDict:(NSDictionary *)dict{    // 只是調用對象的initWithDict方法,之所以用self是為了對子類進行相容    return [[self alloc]initWithDict:dict];}// 對象方法,設定對象的屬性後,返回對象- (Girl *)initWithDict:(NSDictionary *)dict{    // 必須先調用父類NSObject的init方法    if (self = [super init]) {        // 設定對象自己的屬性        self.name = dict[@"name"]   ;        self.headImgName = dict[@"headImg"] ;        self.verdict = dict[@"verdict"];    }    // 返回填充好的對象    return self;}@end



GirlCell.xib


GirlCell.h


////  GirlCell.h//  14_tableViewController_xib自訂cell////  Created by beyond on 14-7-28.//  Copyright (c) 2014年 com.beyond. All rights reserved./* xib+自訂cell的封裝標準步驟:(按此步驟者,事半功倍) 1,建立GirlCell.xib,往裡面拖一個tableViewCell,在cell中拖入各種控制項 2,建立GirlCell類,繼承自uitableViewCell 3,將GirlCell.xib介面中cell的類名更改成GirlCell,並且指定重用的identifer,一般寫類名 4,將GirlCell.xib介面中所有的各種控制項,連線到類GirlCell中,使它們都成為GirlCell類的成員 5,建立資料模型 Girl,方法列表如下                // UI控制項用weak,字串用copy,其他對象用strong        // 頭像圖片名        @property(nonatomic,copy)NSString *headImgName;        // 姓名        @property(nonatomic,copy)NSString *name;        // 判詞        @property(nonatomic,copy)NSString *verdict;        // 類方法,字典 轉 對象 類似javaBean一次性填充        + (Girl *)girlWithDict:(NSDictionary *)dict;        // 對象方法,設定對象的屬性後,返回對象        - (Girl *)initWithDict:(NSDictionary *)dict;  6,封裝的GirlCell,方法列表如下          @property (weak, nonatomic) IBOutlet UIImageView *headImg;         @property (weak, nonatomic) IBOutlet UILabel *name;         @property (weak, nonatomic) IBOutlet UILabel *verdict;                  // 返回xib介面中cell的高度         + (CGFloat)cellHeight;                  // 返回xib介面上寫的重用cellID         + (NSString *)cellID;                  // 從xib中載入 執行個體化一個girlCell對象         + (GirlCell *)girlCell;                  // 參數是資料模型girl對象,填儲值到xib中各個控制項 ,並返回封裝好資料之後的,girlCell對象         - (GirlCell *)cellWithGirl:(Girl *)girl;  7,控制器中 cellForRow方法如下     - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath     {         // 把cell封裝之後,控制 器知道的東西就非常少了~                  // 先從緩衝池中,根據ID取出自已的cell類         GirlCell *girlCell = [tableView dequeueReusableCellWithIdentifier:[GirlCell cellID]];         if (girlCell == nil) {         // 如果池中沒取到,則類方法,重建一個嶄新的girlCell,xib介面中指定了重用cellID         girlCell = [GirlCell girlCell];         }         // 設定cell中獨一無二的內容         Girl *girl = [_girls objectAtIndex:indexPath.row];         // 參數為girl對象,填充其各個成員值到xib中各個控制項 ,並返回填充好的cell         girlCell = [girlCell cellWithGirl:girl];         // 返回cell         return girlCell;               }    */#import <UIKit/UIKit.h>@class Girl;@interface GirlCell : UITableViewCell@property (weak, nonatomic) IBOutlet UIImageView *headImg;@property (weak, nonatomic) IBOutlet UILabel *name;@property (weak, nonatomic) IBOutlet UILabel *verdict;// 返回xib介面中cell的高度+ (CGFloat)cellHeight;// 返回xib介面上寫的重用cellID+ (NSString *)cellID;// 從xib中載入 執行個體化一個girlCell對象+ (GirlCell *)girlCell;// 參數是資料模型girl對象,填儲值到xib中各個控制項 ,並返回封裝好資料之後的,girlCell對象- (GirlCell *)cellWithGirl:(Girl *)girl;@end



GirlCell.m


////  GirlCell.m//  14_tableViewController_xib自訂cell////  Created by beyond on 14-7-28.//  Copyright (c) 2014年 com.beyond. All rights reserved.//#import "GirlCell.h"#import "Girl.h"@implementation GirlCell// 返回xib介面中cell的高度+ (CGFloat)cellHeight{    // 查看一下xib中cell的高度    return 80;}// 返回xib介面上寫的重用cellID+(NSString *)cellID{    // 必須和介面上的一致    return @"GirlCell";}// 從xib中載入 執行個體化一個girlCell對象+ (GirlCell *)girlCell{    // mainBundel載入xib,副檔名不用寫.xib    NSArray *arrayXibObjects = [[NSBundle mainBundle] loadNibNamed:@"GirlCell" owner:nil options:nil];    return arrayXibObjects[0];}// 返回封裝好資料之後的,girlCell對象- (GirlCell *)cellWithGirl:(Girl *)girl{    // 前面,通過連線,將xib中的各個控制項,串連到GirlCell類,成為它的成員屬性了,這樣一來就不用通過tag取得xib中每一個控制項了    _name.text = girl.name;    _headImg.image = [UIImage imageNamed:girl.headImgName];    _verdict.text = girl.verdict;    // 返回封裝好資料之後的,girlCell對象    return self;}@end



main.storyboard


BeyondTableViewController.h

////  BeyondTableViewController.h//  14_tableViewController_xib自訂cell////  Created by beyond on 14-7-28.//  Copyright (c) 2014年 com.beyond. All rights reserved.//#import <UIKit/UIKit.h>// tableViewController 繼承自UIViewController且已經遵守了資料來源和代理,其內部的view就是tableView,且已經自己連線了資料來源和代理/* 建立tableViewController的標準步驟 1,建立工程,然後,刪除viewController.h和.m檔案 2,new File 選擇OC 類,繼承自UITableViewController 3,到main.storyboard中刪除 預設的viewController 4,拖動一個黃色的tableViewController到main.storyboard,並更改file's owner為自已經建立的BeyondTableViewController 5,仍然到main.storyboard中刪除 自動產生的cell控制項 6,運行既可 */@interface BeyondTableViewController : UITableViewController@end


BeyondTableViewController.m


////  BeyondTableViewController.m//  14_tableViewController_xib自訂cell////  Created by beyond on 14-7-28.//  Copyright (c) 2014年 com.beyond. All rights reserved.//#import "BeyondTableViewController.h"#import "Girl.h"#import "GirlCell.h"@interface BeyondTableViewController (){    // 從plist檔案中載入的所有girls,返回所有的對象組成的數組    NSMutableArray *_girls;}@end@implementation BeyondTableViewController// 隱藏頂部的狀態列- (BOOL)prefersStatusBarHidden{    return YES;}- (void)viewDidLoad{    [super viewDidLoad];        // 初始化 對象數組    _girls = [NSMutableArray array];        // plist轉成對象數組    [self plistToObjects];    }// plist轉成對象數組- (void)plistToObjects{    // sg_bundle模板代碼,1,獲得.app主要的包;2,返回主要的包中某個檔案的fullPath全路徑    NSBundle *mainBundle = [NSBundle mainBundle];    NSString *fullPath = [mainBundle pathForResource:@"girls.plist" ofType:nil];        // 從plist檔案中根據全路徑,返回字典數組    NSArray *arrayWithDict = [NSArray arrayWithContentsOfFile:fullPath];        // 模型的類方法返回對象,參數只要一個字典數組即可    for (NSDictionary *dict in arrayWithDict) {        // 參數只要字典,這樣一來,控制器就不用知道太多東西了        Girl *girl = [Girl girlWithDict:dict];        // 添加到對象數組        [_girls addObject:girl];    }}#pragma mark - 資料來源方法- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{    // 返回行數    return _girls.count;}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    // 把cell封裝之後,控制 器知道的東西就非常少了~        // 先從緩衝池中,根據ID取出自已的cell類    GirlCell *girlCell = [tableView dequeueReusableCellWithIdentifier:[GirlCell cellID]];    if (girlCell == nil) {        // 如果池中沒取到,則類方法,重建一個嶄新的girlCell,xib介面中指定了重用cellID        girlCell = [GirlCell girlCell];    }    // 設定cell中獨一無二的內容    Girl *girl = [_girls objectAtIndex:indexPath.row];    // 參數為girl對象,填充其各個成員值到xib中各個控制項 ,並返回填充好的cell    girlCell = [girlCell cellWithGirl:girl];    // 返回cell    return girlCell;        }#pragma mark - 代理方法// 每一行的高度- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{    // 調用類方法,得到xib中的cell的行高    return [GirlCell cellHeight];}// 取消預設點擊後,藍色高亮背景- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];}@end



girls.plist



tableViewController層次圖















怎在tableview不同行中放多個自訂cell

1.建立TableViewCell類,繼承父類為UITableViewCell

1.1 "TableCell.h"
#import <UIKit/UIKit.h>

/**
* 房間桌子顯示結構
* /
@interface TableCell : UITableViewCell {
    UILabel *tableNoLable; // 桌子號
    UIImageView *tableImageView; // 桌子圖片
   UIImageView *tableStateImageView; // 桌子狀態圖片
}

@property (nonatomic ,retain) IBOutlet UILabel *tableNoLable;// 桌子號
@property (nonatomic ,retain) IBOutlet UIImageView *tableImageView;// 桌子圖片
@property (nonatomic ,retain) IBOutlet UIImageView *tableStateImageView;// 桌子狀態圖片

1.2 TableCell. m
#import "TableCell.h"
@implementation TableCell
@synthesize tableNoLable; // 桌子號
@synthesize tableImageView; // 桌子圖片
@synthesize tableStateImageView; // 桌子狀態圖片

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
// Initialization code
}
return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {

[super setSelected:selected animated:animated];

// Configure the view for the selected state
}

- (void)dealloc {
[tableNoLable release];
[tableImageView release];

[hand3ImageView release];
[super dealloc];
}

@end

1.3 布置布局檔案( xib 檔案)指明 class 為自己定義的 tabelcellview
new 一個Empty Xib檔案,其中不包含view, 在libriary中拖一個UITableCell到兩個表徵圖的框中,雙擊cellview,指明class為cell.m檔案,開始編輯

2. 頁面中在 tableView 中載入自己的 c......餘下全文>>
 
對於ios開發,類似聊天介面的布局,在tableview中的cell上添加一個長按事件,顯示自訂

自訂方法:
UIMenuItem *share = [[UIMenuItem alloc] initWithTitle:@"分享"action:@selector(shareClick:)];
UIMenuItem *report = [[UIMenuItem alloc] initWithTitle:@"舉報"action:@selector(report:)];
UIMenuController *menu = [UIMenuController sharedMenuController];
[menu setMenuItems:[NSArray arrayWithObjects:share, report, nil]];
[menu setTargetRect:cell.c_post_content.frame inView:cell.c_post_content.superview];
[menu setMenuVisible:YES animated:YES];
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.