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);

}

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM