0819-TableView(tableVeiw控件的代理)(tableView单组 lol数据展示)(tableView-汽车品牌logo 右侧a-z)(KVC)(tableView - 添加删除按钮出现)(自定义代理delegate)(内存)

------------------

tableVeiw控件的代理(dataSource) 与delegate属性类似 只是名称不一样

self.dataSource = self;

设置tableView控件的dataSource 为viewController

设置tableView的样式为 group / plain  (group有分组效果)

1、tableView上下左右 20 0 0 0加约束

2、节组数 行数 单元格 节组头文字 节组底文字

Sections(tableView中的节组数 numberOfSectionsInTableView)、 Rows(节组中的行数 numberOfRowsInSection)、

cell(tableView的单元 cellForRowAtIndexPath) 、

titleForHeaderInSection  (节组头部的文字)、titleForFooterInSection (节组底部的问题)

 

#import "ViewController.h"

@interface ViewController ()<UITableViewDataSource>

@end

@implementation ViewController


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 5;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 3;
    }else{
        return 5;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
    cell.textLabel.text = [NSString stringWithFormat:@"第%d行---第%d例",indexPath.section,indexPath.row];
    return cell;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return @"头部文字";
}

- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
    return @"脚部文字";
}
@end

------------

tableView单组 lol数据展示

1、做Hero数据模型

2、heros数据懒加载

3、tableView懒加载 (新建tableView样式为plain  设置代理datasource  )

4、实现sourceData代理协议方法

 5、指定tableView.delegate = self  设置每一行不同高度 (heightForRowAtIndexPath)

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return (indexPath.row % 2)? 60 : 40;
}

(方法2:不用delegate 那么可以 tableView.rowHeight = 44//默认的 效率高 但是每一行只能固定都是这么多了)

 6、Cell右边的箭头设置

(

 1设置 tableView.delegate = self

 2cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 会触发行的选中

cell.accessoryView不会触发行的选中 

)

7、UITableView点击哪一行

1设置tableView.delegate = self

2didSelectRowAtIndexPath  (取消选中某一行didDeSelectRowAtIndexPath 很少用到 先点击第二行 再点击第三行  这时候执行了取消选中第2行)

 8、点击小箭头执行的方法

accessoryButtonTappedForRowAtInDexPath;

9、点击accessoryButton类型控件等

accessoryButtonTappedForRowWithIndexPath; //只能监听accessoryButton类型控件  其他类型控件需要单独添加监听方法

10、tableView根据 可重复使用cell标记取出 取出cell   ,  如果没有,那么新建cell ,并cell自己设置可重用标记

 

    static NSString *ID = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; //tableView取可以用的cell
    if (cell == nil) {//如果没有取到
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID]; //cell设置可重用标识符
        UISwitch *switcher = [[UISwitch alloc] init];
        [switcher addTarget:self action:@selector(switchChange:) forControlEvents:UIControlEventValueChanged];
        //    cell.accessoryType = UITableViewCellAccessoryDetailButton;
        cell.accessoryView = switcher;
    }

 

11、设置cell的背景图片 无需指定view大小  backgroundView无需指定 自动根据图片大小来显示

UIImage *bgImage = [UIImage imageNamed:@"img_01"];
cell.backgroundView = [[UIImageView alloc] initWithImage:bgImage];

12、tabelView设置分割线 分割线颜色

tableView.seperatorStyle = UITableViewCellSeperatorStyleSingleLine;

tableView.seperatorColoer = [UIColor redColor];

13、设置tableView最顶部和最底部View

self.tableView.tableHeaderView
self.tableView.tableFooterView

 

#import "ViewController.h"
#import "FFHero.h"
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) NSArray *heros;

@end

@implementation ViewController

- (NSArray *)heros
{
    if (_heros == nil) {
        _heros = [FFHero heros];
    }
    return _heros;
}

-(UITableView *)tableView
{
    if (_tableView == nil) {
        //创建tableView
        _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
        //设置datasource delegate
        _tableView.delegate = self;
        _tableView.dataSource = self;
        
        [self.view addSubview:_tableView];
    }
    return _tableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self tableView];
    self.tableView.rowHeight = 120;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    self.tableView.separatorColor = [UIColor colorWithWhite:0.0 alpha:0.5];
    
    //头部视图
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 130)];
    view.backgroundColor = [UIColor redColor];
    self.tableView.tableHeaderView = view;
    
    // 尾部视图
    UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 44)];
    view2.backgroundColor = [UIColor redColor];
    self.tableView.tableFooterView = view2;
    
    NSLog(@"sss");
}

//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    return (indexPath.row % 2) ? 60 : 44;
//}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"行选中");
}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"取消行选中");
}

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"箭头选中");
}

// _______________节组数  每个节组的行数  cell单元细节 节组头部标题 节组尾部标题



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
//    return self.heros.count;
    return 20;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    static NSString *ID = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; //tableView取可以用的cell
    if (cell == nil) {//如果没有取到
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID]; //cell设置可重用标识符
        UISwitch *switcher = [[UISwitch alloc] init];
        [switcher addTarget:self action:@selector(switchChange:) forControlEvents:UIControlEventValueChanged];
        //    cell.accessoryType = UITableViewCellAccessoryDetailButton;
        cell.accessoryView = switcher;
        
//        
//        UIImage *bgImage = [UIImage imageNamed:@"img_01"];
//                cell.backgroundView = [[UIImageView alloc] initWithImage:bgImage];
//        
//                UIImage *selectedBGImage = [UIImage imageNamed:@"img_02"];
//                cell.selectedBackgroundView = [[UIImageView alloc] initWithImage:selectedBGImage];
    }

    // 取出英雄对象
    FFHero *hero = self.heros[indexPath.row];
    
//    设置cell的textlabel 标题 detailTextLabel imageView.image
    cell.textLabel.text = hero.name;
    cell.detailTextLabel.text = hero.intro;
    cell.imageView.image = [UIImage imageNamed:hero.icon];

    
    return cell;
}
- (void)switchChange:(UISwitch *)sender
{
    NSLog(@"switch change");
}

//- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;
//- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section;
View Code

 ------------

tableView-汽车品牌logo 右侧a-z

步骤:

1、导入Log分类文件夹

2、重写carGroup 和Car类的description方法   改变输出格式 NSLog(@"%@",carGroup);

3、设置tableView.datasource 实现UIDataSourceDelegate协议方法

4、右侧 节点索引列表 sectionIndexTitlesForTableView

5、使用KVC的方式 设置右侧索引列表

遍历所有房间Lists

遍历所有人员Lists

-------

KVC

     // 如果取值的对象是一个数组,同样返回一个数组   数组中包含dictinary取值方法
return [self.carGroups valueForKeyPath:@"title"];

 // NSArray *arr = [self.carGroups valueForKeyPath:@"cars.name"] //数组数组中dictionary中所有 cars键下的name键对应的值

//        [self setValuesForKeysWithDictionary:dict];
  //      [self setValue:dict[@"title"] forKey:@"title"];

------

tableView - 添加删除按钮出现

tableView代码创建,并实现协议

1、实现向左滑动删除出现 commitStyleEditing

2、开启删除模式  左侧出现红色删除红圈圈(点击后等同于向左滑动删除出现动作) self.tableView.editting = YES;

3、删除模式下 左侧出现添加按钮红圈圈

 

2 +按钮出现需要实现方法  并设置delegate self.tableView.delegate = self;

tableVieweditingStyleForRowAtIndexPath

红圈圈,绿圈圈

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
 //   if (indexPath.row % 2) {
  //      return UITableViewCellEditingStyleInsert;
//    } else {
  //      return UITableViewCellEditingStyleDelete;
  //  }
    return UITableViewCellEditingStyleInsert;
}

3、

commitEditingStyle 提交添加操作后执行

[self.dataList removeObjectAtIndex:indexPath.row];

[self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationMiddle];

[self.tableView insertRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationMiddle];

 

代码:

//
//  HMViewController.m
//  06-表格的修改
//
//  Created by apple on 14-8-19.
//  Copyright (c) 2014年 itcast. All rights reserved.
//

#import "HMViewController.h"

@interface HMViewController () <UITableViewDataSource, UITableViewDelegate>
/** 数据列表 */
@property (nonatomic, strong) NSMutableArray *dataList;
@property (nonatomic, strong) UITableView *tableView;
@end

@implementation HMViewController

- (UITableView *)tableView
{
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
        
        _tableView.dataSource = self;
        _tableView.delegate = self;
        
        [self.view addSubview:_tableView];
    }
    return _tableView;
}

- (NSMutableArray *)dataList
{
    if (_dataList == nil) {
        _dataList = [NSMutableArray arrayWithObjects:@"zhangsan", @"lisi", @"wangwu", @"zhangsan", @"lisi", @"wangwu", @"zhangsan", @"lisi", @"wangwu", @"zhangsan", @"lisi", @"wangwu", @"zhangsan", @"lisi", @"wangwu", @"zhangsan", @"lisi", @"wangwu", @"zhangsan", @"lisi", @"wangwu", @"zhangsan", @"lisi", @"wangwuwangwuwangwuwangwuwangwu", nil];
    }
    return _dataList;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self tableView];

    // 开始编辑,一旦editing == YES就默认开启删除模式
    self.tableView.editing = YES;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *ID = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    
    // 设置表格
    cell.textLabel.text = self.dataList[indexPath.row];
    
    return cell;
}

// 只要实现了此方法,就能够支持手势拖拽删除了,删除需要自己干!
/**
 UITableViewCellEditingStyleNone,
 UITableViewCellEditingStyleDelete,     删除
 UITableViewCellEditingStyleInsert      添加
 */
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        NSLog(@"要删除");
        
        // MVC => 数据是保存在模型中
        // 1. 删除self.dataList中indexPath对应的数据
        [self.dataList removeObjectAtIndex:indexPath.row];
        NSLog(@"%@", self.dataList);
        
        // 2. 刷新表格(重新加载数据)
        // 重新加载所有数据
//        [self.tableView reloadData];
        // deleteRowsAtIndexPaths让表格控件动画删除指定的行
        [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationMiddle];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        NSLog(@"要添加数据");
        
        // 1. 向数组添加数据
        [self.dataList insertObject:@"王小二" atIndex:indexPath.row + 1];
        // 2. 刷新表格
//        [self.tableView reloadData];
        // insertRowsAtIndexPaths让表格控件动画在指定indexPath添加指定行
        // 新建一个indexPath
        NSIndexPath *path = [NSIndexPath indexPathForRow:indexPath.row + 1 inSection:indexPath.section];
 
        [self.tableView insertRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationMiddle];
    }
}

// 只要实现此方法,就可以拖动控件
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
    // 界面数据UITableView已经完成了
    // 调整数据即可
//    [self.dataList exchangeObjectAtIndex:sourceIndexPath.row withObjectAtIndex:destinationIndexPath.row];
    // 1. 将源从数组中取出
    id source = self.dataList[sourceIndexPath.row];
    // 2. 将源从数组中删除
    [self.dataList removeObjectAtIndex:sourceIndexPath.row];
    NSLog(@"%@", self.dataList);
    
    // 3. 将源插入到数组中的目标位置
    [self.dataList insertObject:source atIndex:destinationIndexPath.row];
    
    NSLog(@"%@", self.dataList);
}

#pragma mark - 代理方法
// 返回编辑样式,如果没有实现此方法,默认都是删除
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
//    if (indexPath.row % 2) {
//        return UITableViewCellEditingStyleInsert;
//    } else {
//        return UITableViewCellEditingStyleDelete;
//    }
    return UITableViewCellEditingStyleInsert;
}

@end
View Code

 

 

------------

自定义代理delegate

写 控制器<***delegate>遵守协议是为了方便敲代码 有提示      遵守了协议就等同于写了这个方法协议方法的声明   需要写出方法的实现部分  

1、定义appView的delegatez协议

@class HMAppInfo, HMAppView;

// 1. 协议名以类名开始+Delegate
@protocol HMAppViewDelegate <NSObject>

@optional
// 2. 协议方法,以类名开始(没有类前缀),第一个参数是自己
// 只是定义方法名,不做具体实现
- (void)appViewDidClickDownloadButton:(HMAppView *)appView;

@end

// 3. 定义代理属性,遵守了HMAppViewDelegate协议的任意一个对象,都可以成为代理
@property (nonatomic, weak) id<HMAppViewDelegate> delegate;

2、设置appView的代理为控制器  

appView.delegate = self;

3、判断代理时候实现了该代理方法 self.delegate reponseToSelector:selector(appViewDidClickDownloadButton:)

/** appView - xib里的按钮 连线监听方法 */
- (IBAction)click:(UIButton *)button
{

    if ([self.delegate respondsToSelector:@selector(appViewDidClickDownloadButton:)]) {
        // 2> 如果实现了,再通知代理去工作
        [self.delegate appViewDidClickDownloadButton:self];
    }

}

//  ViewController.m

#import "ViewController.h"
#import "FFAppView.h"
@interface ViewController () <FFAppViewDelegate>

@property (nonatomic,strong) FFAppView *vv;
@end

@implementation ViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    FFAppView *appView = [[FFAppView alloc]init];
    appView.delegate = self;
    [appView.delegate appViewDidClick:appView];
    
}

- (void)appViewDidClick:(FFAppView *)appView
{
    NSLog(@"delegate协议方法执行了");
}

@end

 

//  FFAppView.h

#import <UIKit/UIKit.h>
@class FFAppView;

@protocol FFAppViewDelegate <NSObject>
@optional
// 2. 协议方法,以类名开始(没有类前缀),第一个参数是自己
// 只是定义方法名,不做具体实现
- (void)appViewDidClick:(FFAppView *)appView;
@end

@interface FFAppView : UIView // 3. 定义代理属性,遵守了HMAppViewDelegate协议的任意一个对象,都可以成为代理 @property (nonatomic, weak) id<FFAppViewDelegate> delegate; @end

 

//  FFAppView.m
#import "FFAppView.h"
@implementation FFAppView

@end

 ------

内存

IOS程序启动

iphone手机应用程序 栈区占据1M内存空间     mac占据的是8M

 

 

------------

其他不重要的

数组中取出的都是id类型的  只能使用get方法  不能用 点. 语法

 

可变数组和不可变数组的使用

NSArray *array = @["1","2"];

_arrayList = [NSMutableArray arrayWithObjects:@"张1",@"张2",@"张3",@"张4",@"张5",@"张6",@"张7",@"张8",@"张9",@"张10",@"张11",@"张12", nil];

 

对象包装成数组 @[obj]

 

posted @ 2016-02-26 21:20  海龙王来了  阅读(219)  评论(0)    收藏  举报
友情链接:废钢破碎机  带式压滤机