iOS簡單實現UITableView,及自定義UITableViewCell

##以下演示最簡單的實現UITabelView
//自定義cell
//.h文件

#import <UIKit/UIKit.h>
@interface MyTableViewCell : UITableViewCell
@property (nonatomic, strong) UILabel *labelTitle;
@end

//.m文件

#import "MyTableViewCell.h"

@implementation MyTableViewCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        UILabel *labelTitle = [[UILabel alloc]init];
        labelTitle.frame=CGRectMake(0, 0, 100, 20);
        [self setLabelTitle:labelTitle];
        [self addSubview:labelTitle];
    }
    return self;
}
@end

//ViewController文件

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong) UITableView * tableView;
@end

- (void)viewDidLoad {
    [super viewDidLoad];
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake([[UIScreen mainScreen] bounds];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    [self.view addSubview:_tableView];
}
//每組返回行數 Rows 行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 20;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  static NSString * cellId = @"cellID";
    MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (cell == nil){
        cell = [[MyTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
    }
    [cell.labelTitle setText:@"哦哦哦"];
    return cell;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章