主要是學習分類,挺不錯的樣子,還沒有加完,加完還需要2個類,一個資料類,一個cell子控件的大小類
大緻分了一下,一個uitableview需要幾個方法去實作, 建立一個tableivew,自定義一個cell,一個cell的模型,一個cell子控件的大小,複雜的話可以在加一個繼承tableview的方法1般是3種或者4種,或者是5種。
#import "ViewController.h"
@interface XXTableiewCell : UITableViewCell
@end
const float XXTableiewCell_fontSize = 12;
@interface XXTableiewCell ()
@property (strong, nonatomic) UILabel * titleLabel;
@property (strong, nonatomic) UILabel * showTilleLable;
@implementation XXTableiewCell
+ (instancetype)cellWithTableView:(UITableView *)tableView{
static NSString *cellID = @"cellId";
XXTableiewCell * cell = [[XXTableiewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
return cell;
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if(self){
[self setViewUI];
}
return self;
- (void)setViewUI{
_titleLabel = [[UILabel alloc] init];
_titleLabel.font = [UIFont systemFontOfSize:XXTableiewCell_fontSize];
_titleLabel.textColor = [UIColor grayColor];
_titleLabel.textAlignment = NSTextAlignmentLeft;
_titleLabel.backgroundColor = [UIColor clearColor];
[self.contentView addSubview:_titleLabel];
_showTilleLable = [[UILabel alloc] init];
_showTilleLable.font = [UIFont systemFontOfSize:XXTableiewCell_fontSize];
_showTilleLable.textColor = [UIColor grayColor];
_showTilleLable.textAlignment = NSTextAlignmentLeft;
_showTilleLable.backgroundColor = [UIColor clearColor];
[self.contentView addSubview:_showTilleLable];
- (void)layoutSubviews{
[super layoutSubviews];
_titleLabel.frame = CGRectMake(10, 0, self.contentView.frame.size.width-20, 20);
_showTilleLable.frame = CGRectMake(_titleLabel.frame.origin.x, _titleLabel.frame.size.height + _titleLabel.frame.origin.y + 5, _titleLabel.frame.size.width, _titleLabel.frame.size.height);
@interface ViewController () <UITableViewDataSource, UITableViewDelegate>
@property (strong,nonatomic)NSMutableArray * resultArry;
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
_resultArry = [NSMutableArray arrayWithArray:[UIFont familyNames]];
UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
tableView.dataSource = self;
tableView.delegate = self;
[self setExtraCellLineHidden:tableView];
[self.view addSubview:tableView];
// 隐藏多餘cell
-(void)setExtraCellLineHidden: (UITableView *)tableView
{
UIView *view = [UIView new];
view.backgroundColor = [UIColor clearColor];
[tableView setTableFooterView:view];
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
return _resultArry.count;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
XXTableiewCell * cell = [XXTableiewCell cellWithTableView:tableView];
cell.titleLabel.text = [NSString stringWithFormat:@"%ld",(NSInteger)indexPath.row + 1];
cell.showTilleLable.text = [NSString stringWithFormat:@"%@",_resultArry[indexPath.row]];
#pragma mark-設定每一組的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
return 50;
#pragma mark 設定選中處理方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"%ld",indexPath.row + 1);
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.