Ios 使用节的UITableViewCell触发iAction
我有一个带有两个部分的Ios 使用节的UITableViewCell触发iAction,ios,objective-c,uitableview,ibaction,Ios,Objective C,Uitableview,Ibaction,我有一个带有两个部分的表视图。一个部分只有一行,标题标签显示日期。我正在使用来显示操作表。因此在我的视图控制器中,我声明了一个对象DateTableViewCell*dateCell下面是该表视图的委托和数据源方法: -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 2; } - (NSString *)tableView:(UITableView *)tableView titleFor
表视图。一个部分只有一行,标题标签显示日期。我正在使用来显示操作表。
因此在我的视图控制器中,我声明了一个对象DateTableViewCell*dateCell代码>下面是该表视图的委托和数据源方法:
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 2;
}
- (NSString *)tableView:(UITableView *)tableView
titleForHeaderInSection:(NSInteger)section {
if (section == 0) {
return @"Date";
}
else
{
return @"Select Location to View Schedule";
}
}
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{
if (section == 0) {
return 1;
}
else
{
return 5;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"locationCell";
HomeLocationTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
cellIdentifier];
static NSString *dateCellIdentifier = @"dateCell";
dateCell = [tableView dequeueReusableCellWithIdentifier:
dateCellIdentifier];
if (cell == nil) {
cell = [[HomeLocationTableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:cellIdentifier];
}
else if (dateCell == nil)
{
dateCell = [[DateTableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:dateCellIdentifier];
}
if (indexPath.section==0)
{
dateCell.textLabel.text = [NSString stringWithFormat:@"%@",[NSDate date]];
return dateCell;
}
else
{
cell.locationNameLabel.text = @"ABC Hospital";
cell.totalAppointmentLabel.text = @"15 Appointments";
return cell;
}
}
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section==0)
{
[dateCell targetForAction:@selector(selectADate:) withSender:self];
//[self performSelector:@selector(selectADate:) withObject:dateCell];
}
else
{
[self.tabBarController setSelectedIndex:1];
}
}
忽略第1节,因为它只是演示数据。这就是我将日期添加到单元格的方式,正如您在did select row for index path中看到的,我正在调用下面的方法,但没有调用它。我已经用注释行和未注释行尝试过了
- (void)selectADate:(UIControl *)sender {
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *minimumDateComponents = [calendar components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:[NSDate date]];
[minimumDateComponents setYear:2000];
NSDate *minDate = [calendar dateFromComponents:minimumDateComponents];
NSDate *maxDate = [NSDate date];
_actionSheetPicker = [[ActionSheetDatePicker alloc] initWithTitle:@"" datePickerMode:UIDatePickerModeDate selectedDate:self.selectedDate
target:self action:@selector(dateWasSelected:element:) origin:sender];
[(ActionSheetDatePicker *) self.actionSheetPicker setMinimumDate:minDate];
[(ActionSheetDatePicker *) self.actionSheetPicker setMaximumDate:maxDate];
[self.actionSheetPicker addCustomButtonWithTitle:@"Today" value:[NSDate date]];
[self.actionSheetPicker addCustomButtonWithTitle:@"Yesterday" value:[[NSDate date] TC_dateByAddingCalendarUnits:NSCalendarUnitDay amount:-1]];
self.actionSheetPicker.hideCancel = YES;
}
- (void)dateWasSelected:(NSDate *)selectedDate element:(id)element {
self.selectedDate = selectedDate;
//may have originated from textField or barButtonItem, use an IBOutlet instead of element
dateCell.dateLabel.text = [self.selectedDate description];
}
如果我尝试-(iAction)选择日期:(UIControl*)sender
不是我现在正在使用的,它告诉我将它连接到某个东西,我想将它连接到Cell,但即使我尝试实现它UITableViewCell
类,也无法实现这种连接。在自定义单元格HomeLocationTableViewCell
中创建按钮的iAction。在单元格中实现协议,并将视图控制器(实现表视图的数据源和委托)设置为其委托
现在,当触发该操作时,向您的代理发送一条消息,说明cell希望选择一个日期。在代理消息中传递单元格,以便代理可以在选择时设置其日期标签
在您的HomeLocationTableViewCell.h中
@协议HomeLocationTableViewCellDelegate
-(void)homeLocationTableViewCell:(homeLocationTableViewCell*)单元格日期按钮;
@结束
并在您的viewController中的cellForRowAtIndexPath
cell.delegate=self
然后在viewController中实现此委托方法
-(void)homeLocationTableViewCell:(homeLocationTableViewCell*)单元格didTapDateButton{
//在此处显示操作表,并使用此单元格设置其标签
}
您可以将您的方法设置为:
- (void)selectADate:(UITableViewCell *)cell {
// your method body!
}
然后从didselectRowAtindexpath
调用该方法
YorTableViewCellClass *cell = [tableView cellForRowAtIndexPath:indexPath];
[self selectADate:cell];
更新:
从selectADate
方法中删除发件人,使其看起来像
- (void)selectADate{
}
第二件事是替换线下的内容
_actionSheetPicker = [[ActionSheetDatePicker alloc] initWithTitle:@"" datePickerMode:UIDatePickerModeDate selectedDate:self.selectedDate
target:self action:@selector(dateWasSelected:element:) origin:sender];
与
对于创建操作,我使用了self.view
而不是sender
请访问此链接,希望它能帮助您。为什么需要发件人?在这种情况下,发件人是什么?是手机吗?@UB先生,我不需要。我只是选择了什么来打开pickerview并将日期标签设置为用户选择的任何内容。好的,那么您想设置所选单元格的日期吗?@Mr.UB yes!在那一部分永远只有一个细胞。。。因此,这不是一种合适的方法,因为单元格不需要UIButton。这就是自定义UITableViewCell的全部要点。根据Apple文档,您可以在UITableViewCell中添加子视图。随着您的更改和我的方法实现,如我的问题selectADate
中所述,@selector(dateWasSelected:element:)
在ActionSheetDatePicker
的init方法中没有被调用。这就是为什么我在评论中问第一个问题,什么是发送者!!!我正在更新我的答案,如果它有效!!对不起,我错过了你的评论。。。在这里回答!我想在这种情况下,发送者应该是手机。但如果有其他方法,我真的不需要发送者。由于该部分中只有一个单元格,当它被选中时,我想显示picker view.nope仍然相同。。。发生的情况是,它进入了selectADate
方法,完全执行它,但没有显示ActionPicker,也没有进入dateWasSelected
方法。但是您在哪里显示ActionPicker
btw?如果您只想选择日期,为什么不使用ios的原生UIDatePicker
?
_actionSheetPicker = [[ActionSheetDatePicker alloc] initWithTitle:@"" datePickerMode:UIDatePickerModeDate selectedDate:self.selectedDate
target:self action:@selector(dateWasSelected:element:) origin:self.view];