ios仿淘寶管理收貨地址demo

在上篇中出現了複用的問題,而後是用數組承接的,如今我換了一種方法數組

(1)自定義cell.hide

@class MyTableViewCell;atom

 

//建立一個代理spa

@protocol myTabVdelegate <NSObject>代理

 

-(void)myTabVClick:(MyTableViewCell *)cell; //由於我要用這個來斷定選中的cellcode

 

@endorm

 

@interface MyTableViewCell : UITableViewCell事件

 

//聲明一個代碼塊get

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

 

@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

相關文章
相關標籤/搜索