zoukankan      html  css  js  c++  java
  • 源码-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
    本人无商业用途,仅仅是学习做个笔记,特别鸣谢小马哥,学习了IOS,另日语学习内容有需要文本和音频请关注公众号:riyuxuexishuji
  • 相关阅读:
    Golang-字符串常用的系统函数
    35.HTML--网页自动跳转 5种方法
    34.js----JS 开发者必须知道的十个 ES6 新特性
    33. 禁止鼠标右键保存图片、禁止拖动图片
    32.js 判断当前页面是否被浏览
    31.JS实现控制HTML5背景音乐播放暂停
    30.get和post的区别
    29.html5 移动端开发总结
    28.json数组,select选择,input输出对应数据
    27.给input边框和背景颜色设置全透明
  • 原文地址:https://www.cnblogs.com/laugh/p/6432962.html
Copyright © 2011-2022 走看看