iOS UITableViewCell初始化的方法OC和swift版

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传递数据就可以了,代码也会更加清晰

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

王 哪跑!!!

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值