iOS 获取cell.accessoryView自定义视图以及点击事件

- (void)viewDidLoad {

    [super viewDidLoad];

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

    _tableView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStyleGrouped];

    [self.view addSubview:_tableView];

    _tableView.dataSource = self;

    _tableView.delegate = self;

    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];

    

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

    return 3;

}

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

{

    switch (section) {

        case 0:

            return 1;

            break;

            case 1:

            return 2;

            break;

            case 2:

            return 3;

        default:

            break;

    }

    return 0;

}

//- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

//{

//    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 375, 40)];

//    view.backgroundColor = [UIColor cyanColor];

//    return view;

//}

//- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section

//{

//    return 40;

//}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

{

    return 80;

}

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

{

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];

    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

//    cell.accessoryView

    UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 100, 80)];

    button.backgroundColor = [UIColor redColor];

    cell.accessoryView = button;

    [button addTarget:self action:@selector(surebutton:) forControlEvents:UIControlEventTouchUpInside];

    cell.textLabel.text = @"测试";

    return cell;

}

- (void)surebutton:(UIButton *)sender

{

    UITableViewCell *cell = (UITableViewCell *)sender.superview;

    NSIndexPath *indexpath = [_tableView indexPathForCell:cell];

    NSLog(@"%ld",indexpath.row);

}

 

posted @ 2016-04-08 16:19  枫锅锅  阅读(2523)  评论(0编辑  收藏  举报