TableViewCell 初始化的时候有很多种,用的最多的应该就是在ViewController中通过registerNib或registerClass来进行注册,但是这种会让人有一种很多余的感觉(个人感觉),因为这些代码注册是cell,所以感觉这些都应该放到cell里面进行处理,所以今天跟大家分享一种新的注册cell的方法
首先OC版本
创建ABMyTableViewCell类
.h文件
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface ABMyTableViewCell : UITableViewCell
@property (weak, nonatomic) IBOutlet UIImageView *abImageView;
@property (weak, nonatomic) IBOutlet UILabel *abTitleLabel;
+ (instancetype)cellWithTableView:(UITableView*)tableView;
+ (CGFloat)cellHeight;
- (void)setCellData:(NSDictionary*)dic;
@end
NS_ASSUME_NONNULL_END
.m文件实现
#import "ABMyTableViewCell.h"
@implementation ABMyTableViewCell
- (void)awakeFromNib {
[super awakeFromNib];
// Initialization code
}
+ (instancetype)cellWithTableView:(UITableView *)tableView{
static NSString *identify = @"ABMyTableViewCell";
ABMyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identify];
if ( cell == nil ) {
cell = [[NSBundle mainBundle]loadNibNamed:identify owner:nil options:nil].firstObject;
}
return cell;
}
+ (CGFloat)cellHeight{
return 50;
}
- (void)setCellData:(NSDictionary *)dic{
_abImageView.image = [UIImage imageNamed:[dic objectForKey:@"imageName"]];
_abTitleLabel.text = [dic objectForKey:@"title"];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
}
@end
接下在ViewController来使用
#pragma mark - ----------------- UITableView Delegate --------------------
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
ABMyTableViewCell *cell = [ABMyTableViewCell cellWithTableView:tableView];
if ( indexPath.row < _dataSource.count ) {
[cell setCellData:_dataSource[indexPath.row]];
}
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return [ABMyTableViewCell cellHeight];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return _dataSource.count;
}
这样OC版本简单的实现了这样可以减少ViewController的臃肿和代码量
接下来是swift版本,由于swift版本没有.h 和 .m文件只有swift文件所以代码也很简单首先是cell
import UIKit
class MyTableViewCell: UITableViewCell {
@IBOutlet weak var titleLabel: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
}
public static func cellWithTableView(tableView:UITableView)->MyTableViewCell{
let identify:NSString = "MyTableViewCell"
var cell: MyTableViewCell?
cell = tableView.dequeueReusableCell(withIdentifier: identify as String) as? MyTableViewCell
if cell == nil {
cell = (Bundle.main.loadNibNamed(identify as String, owner: nil, options: nil)?.first as! MyTableViewCell)
}
return cell!
}
public static func cellHeight()->CGFloat{
return 45
}
func setCellData(string:String) {
titleLabel.text = NSToolObject.stringIsEmpty(value: string as AnyObject) ? "" : string
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
}
接下在ViewController来使用
import UIKit
class MyPageVController: ViewController,UITableViewDelegate,UITableViewDataSource {
var dataSource = NSArray()
override func viewDidLoad() {
super.viewDidLoad()
creatDataSource()
}
func creatDataSource() {
dataSource = ["实验室","帮助与反馈","退出登录"]
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return dataSource.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = MyTableViewCell.cellWithTableView(tableView: tableView)
if indexPath.row < dataSource.count {
cell.setCellData(string: dataSource[indexPath.row] as! String)
}
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return MyTableViewCell.cellHeight()
}
}
这样就可以更方便使用Cell和ViewController,cell和Controller的代码更加独立,cell只需要管理cell的内容,ViewController只需要给cell传递数据就可以了,代码也会更加清晰