自定义cell上的控件怎么添加点击事件

2025-05-14 17:55:07
推荐回答(1个)
回答1:

  一种方式给Button加上tag值:这里分为两种:一种是直接在原生的UITableViewCell上添加UIButton按钮,然后给UIButton设置tag值,然后在控制器里的方法里通过取数据,做界面跳转等。还是举个例子吧,省的回忆半天。
[objc]view plaincopy

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

{

staticNSString*identifier =@"Cell";

UITableViewCell*cell = [tableViewdequeueReusableCellWithIdentifier:reuseIdentifier];

if(cell ==nil) {

cell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:identifier];

cell.selectionStyle= UITableViewCellSelectionStyleNone;

}

User*user = _users[indexPath.row];

cell.user= user;

//拍照button

UIButton *photographButton = [UIButtonbuttonWithType:UIButtonTypeCustom];

photographButton.frame= CGRectMake(221,10,100,44);

[photographButtonsetImage:[UIImageimageNamed:@"camera.png"]forState:UIControlStateNormal];

[photographButtonaddTarget:selfaction:@selector(photographButtonClicked:)forControlEvents:UIControlEventTouchUpInside];

photographButton.tag= indexPath.row;

[cell.contentViewaddSubview:photographButton];

returncell;

}

然后在点击事件中取数据,加信息
[objc]view plaincopy

- (void)photographButtonClicked:(UIButton*)sender{

User*user = _users[sender.tag];

PhotoPickerController*photoPicker = [[PhotoPickerControlleralloc]init];

photoPicker.user= user;

[self.navigationControllerpushViewController:photoPickeranimated:YES];

}