版权声明:本文为延成原创文章,转载请标明出处
ViewController.h
#import "ViewController.h"
#import "TestTableViewCell.h"
#import "UILableViewController.h"
#import "UIButtonViewController.h"
#import "UIViewViewController.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
{
UITableView* _tableView;
NSArray *ceshiArray;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor=[UIColor grayColor];
[self getInfo];
[self createTableView];
}
- (void)getInfo{
ceshiArray = @[@"UILabel",@"UIButton",@"UIView"];
}
-(void)createTableView{
_tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
_tableView.delegate=self;
_tableView.dataSource=self;
_tableView.separatorInset = UIEdgeInsetsMake(0, 50, 0, 0);
// _tableView.separatorInset = UIEdgeInsetsZero;
// _tableView.separatorColor = [UIColor blueColor];
// _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
// _tableView.backgroundColor = [UIColor clearColor];
_tableView.tableFooterView = [[UIView alloc]init];
[self.view addSubview:_tableView];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 50;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return ceshiArray.count;
}
设置数据视图的组数
//- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
// return 2;
//}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString* indentify = @"cellr4rfrklfkre";
TestTableViewCell* cell=[_tableView dequeueReusableCellWithIdentifier:indentify];
if (!cell) {
cell = [[TestTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indentify];
}
cell.nameLable.text = ceshiArray[indexPath.row];
cell.FistImage.image=[UIImage imageNamed:[NSString stringWithFormat:@"heart%ld",indexPath.row]];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"点击条目%ld",indexPath.row);
}
@end
TestTableViewCell.h
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface TestTableViewCell : UITableViewCell
@property (nonatomic, strong)UIImageView *FistImage;
@property (nonatomic, strong)UILabel *nameLable;
@end
NS_ASSUME_NONNULL_END
TestTableViewCell.m
#import "TestTableViewCell.h"
@implementation TestTableViewCell
- (void)awakeFromNib {
[super awakeFromNib];
// Initialization code
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
[self createUI];
}
return self;
}
- (void)createUI{
_FistImage = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 30, 30)];
[self.contentView addSubview:_FistImage];
_nameLable = [[UILabel alloc] initWithFrame:CGRectMake(120, 0, 100, 50)];
_nameLable.textAlignment = NSTextAlignmentCenter;
[self.contentView addSubview:_nameLable];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
@end