程序
- 新建UITableViewCell型別的.h和.m檔案
- 在UITableViewCell型別的.h檔案中添加需要使用的屬性
- 在UITableViewCell型別的.m檔案中寫固定的兩個方法
- 在ViewController.m檔案中進行應用
一、新建 UITableViewCell型別的檔案
程序如圖:

二、在.h檔案中添加需要使用的屬性
下面以一個label一個imageView和一個button為例:
@interface testTableViewCell : UITableViewCell
@property (nonatomic, strong) UILabel *label;
@property (nonatomic, strong) UIButton *button;
@property (nonatomic, strong) UIImageView *imageView;
@end
三、在.m檔案中寫固定的方法
@implementation testTableViewCell
//固定方法一:
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
self.label = [[UILabel alloc] init];
[self.contentView addSubview:_label];
self.button = [[UIButton alloc] init];
[self.contentView addSubview:self.button];
self.imageView = [[UIImageView alloc] init] ;
[self.contentView addSubview: _imageView];
return self;
}
//固定方法二:
- (void)layoutSubviews {
//設定button的位置和圖片等
self.Button.frame = CGRectMake(15, 15, 60, 60);
[self.Button setImage:[UIImage imageNamed:@"xxx.png"] forState:UIControlStateNormal];
//設定label的位置及文字
_label.frame = CGRectMake(85, 32, 100, 30);
_label.text = @"XXX";
//設定imageView的位置和圖片
_imageView.frame = CGRectMake(150, 150, 100, 100);
_imageView.image = [UIImage imageNamed: @"xxx.jpg"];
}
cell的自定義就完成了
四、在ViewController.m檔案中應用
首先需要在ViewController.h檔案中加入協議
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
<
UITableViewDelegate,
UITableViewDataSource
>
//定義一個資料視圖物件
@property (nonatomic, strong)UITableView* tableView;
@end;
然后在ViewController.m中應用:
#import "ViewController.h"
#import "testTableViewCell.h"
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//創建資料視圖
_tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
//設定代理
tableView.delegate = self;
tableView.dataSource = self;
//對cell進行注冊
[tableView registerClass:[TAYTableViewCell class] forCellReuseIdentifier:@"cellFirst"];
//將資料視圖添加到主視圖上
[self.view addSubview:_tableView];
}
//設定資料視圖的組數
- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
return 6;
}
//獲取每組單元格的個數
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 1;
}
//獲取單元格高度
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 60
}
//創建單元格物件函式(創建幾個單元格下面的函式就要被呼叫幾次)
- (UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
testTableViewCell *cellFirst = [tableView dequeueReusableCellWithIdentifier:@"cellFirst" forIndexPath:indexPath]
//設定自定義cell中按鈕的資訊
[cellFirst.button addTarget:self action:@selector(pressButton) forControlEvents:UIControlEventTouchUpInside];
return cellFirst;
}
//其中注冊過的cell不需要判空查看是否可以復用
//注冊是為某一identifier 注冊一個Class
//當識別符號為identifier 的Cell佇列中沒有可復用的cell時,系統會自動創建一個系結的Class型別的cell,所以無需自己判空
//按鈕的事件函式
- (void) pressButton {
}
@end
以上就是自定義cell的程序
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/289688.html
標籤:其他
