zoukankan      html  css  js  c++  java
  • ios仿淘宝管理收货地址demo

    在上篇中出现了复用的问题,然后是用数组承接的,现在我换了一种方法

    (1)自定义cell.h

    @class MyTableViewCell;

    //创建一个代理

    @protocol myTabVdelegate <NSObject>

    -(void)myTabVClick:(MyTableViewCell *)cell; //因为我要用这个来判定选中的cell

    @end

    @interface MyTableViewCell : UITableViewCell

    //声明一个代码块

    @property(strong,nonatomic)void(^btnClick)();

    @property(strong,nonatomic)UIButton *btn;

    @property(assign,nonatomic)id<myTabVdelegate>delegate;

    @end

    (2)cell.m的实现

    #import "MyTableViewCell.h"

    @implementation MyTableViewCell

    - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier

    {

        self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];

        if (self) {

            _btn = [UIButton buttonWithType:UIButtonTypeCustom];

            _btn.frame = CGRectMake(10, 10, 100, 30);

            [_btn setTitle:@"test" forState:UIControlStateNormal];

            [_btn setBackgroundColor:[UIColor redColor]];

            

            [_btn addTarget:self action:@selector(test:) forControlEvents:UIControlEventTouchUpInside];

            [self addSubview:_btn];

        }

        return self;

    }

    //按钮事件

    -(void)test:(UIButton *)sender

    {

        

        [self.delegate myTabVClick:self];

        

    }

    - (void)awakeFromNib

    {

        // Initialization code

    }

    - (void)setSelected:(BOOL)selected animated:(BOOL)animated

    {

        [super setSelected:selected animated:animated];

        // Configure the view for the selected state

    }

    @end

    (3)VC.m的实现

    #import "ViewController.h"

    #import "MyTableViewCell.h"

    @interface ViewController ()

    @property(nonatomic,strong)NSIndexPath *lastPath;

    /*注释*/

    @property (nonatomic,strong)MyTableViewCell *myCell;

    @end

    @implementation ViewController

    - (void)viewDidLoad

    {

        [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

        

        _tableV = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];

        _tableV.delegate = self;

        _tableV.dataSource = self;

        [self.view addSubview:_tableV];

        

    }

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

    {

        return 20;

    }

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

    {

        

        

        

        static NSString *identify = @"identify";

        MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identify];

        if (!cell) {

            cell = [[MyTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identify];

        }

        

        //给代码赋值

        cell.btnClick = ^(){

            NSLog(@"222===%ld",indexPath.row);

        };

        //给代理赋值

        cell.delegate = self;

        

        NSInteger row = [indexPath row];

        

        NSInteger oldRow = [_lastPath row];

        

        if (row == oldRow && _lastPath!=nil)

        {

           [cell.btn setTitle:@"你好" forState:UIControlStateNormal];

        }

        else

        {

           [cell.btn setTitle:@"test" forState:UIControlStateNormal];

        }

            

        return cell;

    }

    //实现代理

    -(void)myTabVClick:(MyTableViewCell *)cell

    {

        NSIndexPath *indexPath = [_tableV indexPathForCell:cell];

        

        NSInteger newRow = [indexPath row];

        NSInteger oldRow = (self .lastPath !=nil)?[self .lastPath row]:-1;

        

        if (newRow != oldRow) {

            

            self.myCell = [_tableV cellForRowAtIndexPath:indexPath];

            

            [self.myCell.btn setTitle:@"你好" forState:UIControlStateNormal];

            

            self.myCell = [_tableV cellForRowAtIndexPath:self .lastPath];

            

            [self.myCell.btn setTitle:@"test" forState:UIControlStateNormal];

            

            self .lastPath = indexPath;

            

        }

        

    }

    - (void)didReceiveMemoryWarning

    {

        [super didReceiveMemoryWarning];

        // Dispose of any resources that can be recreated.

    }

    @end

  • 相关阅读:
    1、PHP入门二维数组与循环
    Nginx 配置反向代理后,页面中取绝对URL地址的问题显示代理端口
    苹果手机上点击WEUI日期控件不容易点中
    ios 不支持-,-时间。
    Newtonsoft.Json添加项
    Baidu地图Map api直接加https不起作用
    腾讯云cos封装
    linux连接工具隧道模式
    微信调试工具测试时有时候复制URL没有corpid解决
    WEUI控件JS用法
  • 原文地址:https://www.cnblogs.com/huiyi-520/p/7501226.html
Copyright © 2011-2022 走看看