源码-0203-UITableViewController

 

 

 

//
//  XMGTestViewController.m
//  04-UITableViewController
#import "XMGTestViewController.h"

@interface XMGTestViewController ()

@end

@implementation XMGTestViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSLog(@"%p %p", self.view, self.tableView);
}

#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 10;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row % 2 == 0) {
        return [self cell:tableView indexPath:indexPath];
    } else {
        return [self cell2:tableView indexPath:indexPath];
    }
}

#pragma mark - 创建、设置cell
- (UITableViewCell *)cell:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath
{
    // 0.重用标识
    // 被static修饰的局部变量:只会初始化一次,在整个程序运行过程中,只有一份内存
    static NSString *ID = @"cell";

    // 1.先根据cell的标识去缓存池中查找可循环利用的cell
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];

    // 2.覆盖数据
    cell.textLabel.text = [NSString stringWithFormat:@"cell - %zd", indexPath.row];

    return cell;
}

- (UITableViewCell *)cell2:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath
{
    // 0.重用标识
    // 被static修饰的局部变量:只会初始化一次,在整个程序运行过程中,只有一份内存
    static NSString *ID = @"cell2";
    
    // 1.先根据cell的标识去缓存池中查找可循环利用的cell
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    // 2.覆盖数据
    cell.textLabel.text = [NSString stringWithFormat:@"cell2 - %zd", indexPath.row];
    
    return cell;
}

#pragma mark - 代理方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"didSelectRowAtIndexPath");
}


@end
//
//  XMGTestViewController.h
//  04-UITableViewController

#import <UIKit/UIKit.h>

@interface XMGTestViewController : UITableViewController

@end

 

常见设置

//
//  XMGTableViewController.m
//  05-常见设置

#import "XMGTableViewController.h"

@interface XMGTableViewController ()

@end

@implementation XMGTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 分割线颜色
//    self.tableView.separatorColor = [UIColor redColor];

    // 隐藏分割线
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 80;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *ID = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    cell.textLabel.text = @"5345345";
    cell.imageView.image = [UIImage imageNamed:@"173890255948"];
//    // 取消选中的样式
//    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    // 设置选中的背景色
    UIView *selectedBackgroundView = [[UIView alloc] init];
    selectedBackgroundView.backgroundColor = [UIColor redColor];
    cell.selectedBackgroundView = selectedBackgroundView;
    
    // 设置默认的背景色 1
    cell.backgroundColor = [UIColor blueColor];
    
    // 设置默认的背景色 2
    UIView *backgroundView = [[UIView alloc] init];
    backgroundView.backgroundColor = [UIColor greenColor];
    cell.backgroundView = backgroundView;
    
    // 设置指示器
     cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
//    cell.accessoryView = [[UISwitch alloc] init];
    
    return cell;
}

#pragma mark - 代理方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"didSelectRowAtIndexPath----");
}

@end

 

posted @ 2017-02-23 12:55  laugh  阅读(241)  评论(0编辑  收藏  举报