viewcontroller.m里的代码
#import "ViewController.h"
#import "TableViewCell.h"
@interface ViewController () <UITableViewDataSource,UITableViewDelegate>
{
UITableView *MytableView;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
MytableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
MytableView.delegate = self;
MytableView.dataSource = self;
[self.view addSubview:MytableView];
[MytableView registerClass:[TableViewCell class] forCellReuseIdentifier:@"cell"];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
TableViewCell *cell = [MytableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
return cell;
}
自定义的tableviewcell.h
#import <UIKit/UIKit.h>
@interface TableViewCell : UITableViewCell
@property (nonatomic,strong) UILabel *label ;
@end
tableviewcell.m
#import "TableViewCell.h"
@implementation TableViewCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
_label = [[UILabel alloc] initWithFrame:CGRectMake(200, 10, 50, 20)];
_label.text = @"1234 ";
_label.textColor = [UIColor redColor];
[self.contentView addSubview:_label];
}
return self;
}
- (void)awakeFromNib
{
// Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
- (void)layoutSubviews
{
[super layoutSubviews];
self.label.frame = CGRectMake(200, 10, 50, 20);
self.label.textColor = [UIColor blueColor];
self.label.text = @"123";
}
@end