iOS控件学习笔记 - UITableView

初始化

#import "ViewController.h"
#define kWidth [UIScreen mainScreen].bounds.size.width
#define kHeight [UIScreen mainScreen].bounds.size.height
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView *tableView;
@end
@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
	//基本配置
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0,0,kWidth,kHeight) style:UITableViewStylePlain];
    self.tableView.delegate = self;//设置代理
    self.tableView.dataSource = self;//设置数据源
    [self.view addSubview:self.tableView];
}

//实现UITableViewDelegate和UITableViewDataSource协议
#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 40;
}

#pragma mark - UITableViewDataSource
 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 10;
}

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if(!cell){
    	//初始化系统提供的UITableViewCell样式
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    cell.textLabel.text = [NSString stringWithFormat:@"测试%ld",indexPath.row];
    return cell;
}
@end
typedef NS_ENUM(NSInteger, UITableViewCellStyle) {
    UITableViewCellStyleDefault,	// 左侧显示textLabel(不显示detailTextLabel),imageView可选(显示在最左边) 
    UITableViewCellStyleValue1,		// 左侧显示textLabel、右侧显示detailTextLabel(默认蓝色),imageView可选(显示在最左边)
    UITableViewCellStyleValue2,		// 左侧依次显示textLabel(默认蓝色)和detailTextLabel,imageView可选(显示在最左边) 
    UITableViewCellStyleSubtitle	// 左上方显示textLabel,左下方显示detailTextLabel(默认灰色),imageView可选(显示在最左边)
};             // available in iPhone OS 3.0

补充

修改Header和Footer的背景颜色

- (void)tableView:(UITableView *)tableView willDisplayFooterView:(UIView *)view forSection:(NSInteger)section {
    view.tintColor = [UIColor blackColor];
}

- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section {
}

获取HeaderView和FooterView

- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section{
}
- (void)tableView:(UITableView *)tableView willDisplayFooterView:(UIView *)view forSection:(NSInteger)section{
}

不显示没有内容的cell

self.tableView.tableFooterView = [[UIView alloc] init];

取消cell的分割线

self.tableview.separatorStyle = UITableViewCellSeparatorStyleNone;

取消cell的选中颜色

发布了38 篇原创文章 · 获赞 5 · 访问量 9056

猜你喜欢

转载自blog.csdn.net/zj382561388/article/details/103013987
今日推荐