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
  • 相关阅读:
    Python爬虫案例:爬取微信公众号文章
    MySQL计算两坐标距离并排序
    删库了一定要跑路吗?
    在python中列表删除和多重循环退出
    软件设计模式修炼 -- 观察者模式
    C# WPF:快把文件从桌面拖进我的窗体来!
    C#(七)基础篇—基本I/O操作
    (8)ASP.NET Core3.1 Ocelot Consul服务注册与发现
    iNeuOS工业互联操作系统,图表与数据点组合成新组件,进行项目复用
    对于经常接触的分页你确定你真的会吗
  • 原文地址:https://www.cnblogs.com/laugh/p/6432962.html
Copyright © 2011-2022 走看看