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

原文地址:https://www.cnblogs.com/huiyi-520/p/7501226.html