使用Section的UITableViewCell来触发IBAction

时间:2016-09-15 11:33:36

标签: ios objective-c uitableview ibaction

我有一个tableView,有2个部分。一个部分只有一个row,其titleLabel显示日期。我使用Action Sheet Picker来显示操作表。
所以在我的视图控制器中我已经声明了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部分,因为它只是演示数据。所以这就是我在单元格中添加日期的方式,正如你所看到的那样,索引路径的选择行我调用下面的方法,但它没有被调用。我已经用注释和未注释的行尝试了它。

- (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];
}

如果我尝试使用- (IBAction)selectADate:(UIControl *)sender代替我现在使用的内容,它会告诉我将其连接到某些内容并且我想将其连接到Cell但是即使我尝试将其连接到也没有这样的连接实现它UITableViewCell class。

3 个答案:

答案 0 :(得分:0)

你可以使你的方法像,

  - (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];

    _actionSheetPicker = [[ActionSheetDatePicker alloc] initWithTitle:@"" datePickerMode:UIDatePickerModeDate selectedDate:self.selectedDate
                                                           target:self action:@selector(dateWasSelected:element:) origin:self.view];

我使用self.view代替sender

答案 1 :(得分:0)

对于创建操作,

Table view cell as button

访问此链接,希望它能为您提供帮助。

答案 2 :(得分:-1)

在自定义单元格HomeLocationTableViewCell中创建按钮的IBAction。在单元格中实现协议,并将视图控制器(实现表视图的数据源和委托)设置为其委托。

现在,当触发操作时,请向您的代理发送一条消息,告知单元格要选择日期。在委托消息中传递您的单元格,以便您的委托可以在选择时设置其日期标签。

HomeLocationTableViewCell.h @protocol HomeLocationTableViewCellDelegate - (void)homeLocationTableViewCell:(HomeLocationTableViewCell *)cell didTapDateButton; @end中 在cellForRowAtIndexPath的viewController中 cell.delegate = self

然后在viewController中实现此委托方法

- (void)homeLocationTableViewCell:(HomeLocationTableViewCell *)cell didTapDateButton { //show your action sheet here and use this cell to set its label }

相关问题