您好我是iOS开发的新手,我正在使用Objective C.我在UITableview中遇到了问题。也就是说,我需要为多个UIButton编写动作,并将其加载到tableview中(DutiesTableView.m& .h) )作为自定义单元格(BreakTimeCell.m& .h)。该tableview(DutiesTableView.m& .h)作为使用XIB的自定义单元加载到另一个tableview(ViewController.m& .h,TableName是PersonalTable)中。 [Shift(自定义单元格),职责(UITableview),休息时间(UITableview)],[这是我的ViewController.m
这是我的示例输出。当我点击Break time UIButton时,它应该在Main Tableview上显示UIDatePicker
答案 0 :(得分:1)
创建一个按钮,
UIButton *btnSample = [[UIButton alloc]initWithFrame:CGRectMake(215,10,100,50)];
[btnSample setTitle:@"Button Title" forState:UIControlStateNormal];
[yourView addSubview:btnSample];
向按钮添加操作
[btnSample addTarget:self action:@selector(your action or method) forControlEvents:UIControlEventTouchUpInside];
答案 1 :(得分:0)
在cellForRow
中添加 yourButton.tag = indexPath.row;
[yourButton addTarget:self action:@selector(methodName:) forControlEvents:UIControlEventTouchUpInside];
按以下方法执行任务
- (void)methodName:(id)sender
{
// you can get index of button from sender.tag
}
答案 2 :(得分:0)
您可以使用委托从自定义单元格到视图控制器进行回调,例如在代码中的自定义单元格中,BreakTimeCell.h
文件声明协议并在BreakTimeCell.xib
中添加按钮并提供出口以及对自定义单元格的操作,如下所示,
#import <UIKit/UIKit.h>
@class BreakTimeCell; //forword declaration
@protocol BreakTimeCellDeleagte<NSObject>
- (void)breakTimeCell:(BreakTimeCell *)cell didTapTheButton:(UIButton *)aButton;
@end
@interface BreakTimeCell : UITableViewCell
@property (weak, nonatomic) IBOutlet UIButton *aButton;
@property (weak, nonatomic) id<BreakTimeCellDeleagte> cellDelegate;//this is important
- (IBAction)myButtonAction:(id)sender;
+ (BreakTimeCell *)getBreakTimeCell; //to get the custom cell
@end
并在BreakTimeCell.h
文件中定义按钮的操作
#import "BreakTimeCell.h"
@implementation BreakTimeCell
+ (BreakTimeCell *)getBreakTimeCell {
NSArray *nibContents = [[NSBundle mainBundle] loadNibNamed:@"BreakTimeCell" owner:self options:NULL];
NSEnumerator *nibEnumerator = [nibContents objectEnumerator];
BreakTimeCell *customCell = nil;
NSObject* nibItem = nil;
while ((nibItem = [nibEnumerator nextObject]) != nil) {
if ([nibItem isKindOfClass:[BreakTimeCell class]]) {
customCell = (BreakTimeCell *)nibItem;
break;
}
}
return customCell;
}
- (void)awakeFromNib {
[super awakeFromNib];
// Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
//call the delegate method in the button action
- (IBAction)myButtonAction:(id)sender {
if([self.cellDelegate respondsToSelector:@selector(breakTimeCell:didTapTheButton:)])
{
[self.cellDelegate breakTimeCell:self didTapTheButton:sender];
}
}
@end
并在视图控制器中将单元委托设置为视图控制器,
#import "ViewController.h"
#import "BreakTimeCell.h"
@interface ViewController ()<UITableViewDelegate, UITableViewDataSource, BreakTimeCellDeleagte>
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
BreakTimeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MY_BREAK_CELL"];
if (cell == nil) {
cell = [BreakTimeCell getBreakTimeCell]; //get the cell
}
cell.cellDelegate = self; //set the delegate to controller (self)
return cell;
}
//this is the delegate method called for the button action.
- (void)breakTimeCell:(BreakTimeCell *)cell didTapTheButton:(UIButton *)aButton {
NSIndexPath *indexPath = [self.personalTable indexPathForCell:cell];
NSLog(@"button tapped for index:%ld",(long)indexPath.row);
}
就是这样,你会得到如下所示的输出,
2017-12-13 11:44:24.693161+0530 Test[3082:100133] button tapped for index:3
2017-12-13 11:44:25.177169+0530 Test[3082:100133] button tapped for index:4
2017-12-13 11:44:26.223166+0530 Test[3082:100133] button tapped for index:0
2017-12-13 11:44:27.663060+0530 Test[3082:100133] button tapped for index:1
2017-12-13 11:44:28.495780+0530 Test[3082:100133] button tapped for index:2