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的選中顏色

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章