封装Cell(-去掉TableView那些碍眼的分割线)

237 阅读1分钟

talk is cheap ! I show you code

  • CustomTableViewCell.h
//
//  CustomTableViewCell.h
//  CustomTableViewCell
//
//  Created by 王磊磊 on 17/3/6.
//  Copyright © 2017年 宇宙黑客. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface CustomTableViewCell : UITableViewCell

@property(nonatomic, strong) NSString *titleText;

@end
  • CustomTableViewCell.h
//
//  CustomTableViewCell.m
//  CustomTableViewCell
//
//  Created by 王磊磊 on 17/3/6.
//  Copyright © 2017年 宇宙黑客. All rights reserved.
//

#import "CustomTableViewCell.h"

@implementation CustomTableViewCell {
    
    UILabel *titleLabel;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.backgroundColor = [UIColor redColor];
        
        titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 20)];
        titleLabel.textAlignment = NSTextAlignmentCenter;
        [self addSubview:titleLabel];
        
    }
    return self;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setTitleText:(NSString *)titleText {
    _titleText = titleText;
    titleLabel.text = [NSString stringWithFormat:@"自定义的部分+外界传入的值:%@", titleText];
    

    /*
     *  下面的是设置分割线为空  ;为什么在这里设置而不在初始化的时候设置呢
     */
    
    UITableView *tableView = (UITableView *)self.superview.superview;
    
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end

  • 正常调用
//
//  ViewController.m
//  CustomTableViewCell
//
//  Created by 王磊磊 on 17/3/6.
//  Copyright © 2017年 宇宙黑客. All rights reserved.
//

#import "ViewController.h"
#import "CustomTableViewCell.h"

@interface ViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) NSMutableArray *dataArray;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    [self.tableView registerClass:[CustomTableViewCell class] forCellReuseIdentifier:@"cellID"];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
//    [self.view addSubview:self.tableView];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 5;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellID" forIndexPath:indexPath];
    
    if (indexPath.row < self.dataArray.count) {
        NSString *str = [NSString stringWithFormat:@"%@", self.dataArray[indexPath.row]];
        cell.titleText = str;
        
        return cell;
    }
    else {
     
         return [UITableViewCell new];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (NSMutableArray *)dataArray {
    
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
        
        for (NSInteger index = 0; index < 4; index ++) {
            [_dataArray addObject:[NSString stringWithFormat:@"%ld", index]];
        }
    }
    
    return _dataArray;
}

@end