委派按钮单击不在tableview单元格中工作

时间:2014-05-13 11:34:55

标签: ios objective-c uitableview delegates

我正在使用Delegate方法来访问我的表格视图的单元格按钮点击方法的信息。我的表格视图单元格是自定义的。我已经在单独的xib文件中设计了单元格,并为单元格声明了一个tableViewCell类型类,并将该类赋予了我设计的单元格。我根据相同表格视图中的日期和一些其他条件加载不同的单元格设计。但问题是,当我单击我的单元格内的按钮时,它无法正常工作。我给了代码所在的地方设置了一个断点,但它没有进入断点。

以下是我遵循的步骤。当我使用storyboards在原型单元格内设计表格单元格时,它工作得很好。我使用xib做了同样的事情,但我不知道出了什么问题。

这是我定制的单元格.h类,我声明了委托     #import

@class CellForComingDates;

@protocol ComingDateRowDelegate <NSObject>

-(void)DeleteButtonAction_new:(CellForComingDates*)cell;


@end


@interface CellForComingDates : UITableViewCell

@property (strong, nonatomic) IBOutlet UIImageView *cellImage;
@property (strong, nonatomic) IBOutlet UILabel *personName;
@property (strong, nonatomic) IBOutlet UILabel *appoinmentTime;
@property (strong, nonatomic) IBOutlet UILabel *appoinmentDate;
@property (strong,nonatomic) NSString* appoinmentstartTime;
@property (strong,nonatomic) NSString* appoinmentendTime;
@property (strong,nonatomic) NSString* appoinmentID;
@property (strong,nonatomic) NSString* USER;

@property (weak) id<ComingDateRowDelegate> comingDateDel;


@end

这是我的IBAction方法所包含的.m类。 我已将IBaction正确连接到按钮

- (IBAction)DeleteButton:(id)sender {
[self.comingDateDel DeleteButtonAction_new:self];
}

然后,在tableView类中,我实现了click方法

-(void)DeleteButtonAction_new:(CellForComingDates*)cell{
     AppoinmentID = cell.appoinmentID;
   UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Delete Appoinment"
                                                message:@"click ok to delete"
                                               delegate:self
                                      cancelButtonTitle:@"OK"
                                      otherButtonTitles:@"Cancel",nil];
alert.tag = kAlertViewTwo;
[alert show];
}

但是当我点击按钮时它没有响应......有人可以告诉我这里发生了什么吗?

谢谢

修改

这是我的cellForRowAtIndexpath方法

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

{

UITableViewCell *cell;
NSDate *today = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    // display in 12HR/24HR (i.e. 11:25PM or 23:25) format according to User Settings
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
NSString *currentTime = [dateFormatter stringFromDate:today];
NSDate *date=[dateFormatter dateFromString:currentTime];

if(indexPath.row==0){
    VCPerson *person = self.persons[0];

    cell = [self createPersonCell:person];

}

else if ([self datePickerIsShown] && (self.datePickerIndexPath.row == 1)){

    cell = [self createPickerCell:date];

}

else{

    NSDictionary* detailsToShowTemp= [[ScheduleView getRequestForDate]objectAtIndex:indexPath.row-1];
    NSInteger Mailcount = [[[detailsToShowTemp valueForKey:@"Appointment" ]valueForKey:@"SentMailCount"] integerValue];
    NSInteger ReviewID = [[[detailsToShowTemp valueForKey:@"AppointmentReview" ]valueForKey:@"ReviewId"] integerValue];
    NSString* timestam = [[[detailsToShowTemp valueForKey:@"Appointment"]valueForKey:@"DayTimeSlot"]valueForKey:@"Date"];
    NSString* dateText = [self ConvertDate:timestam];

    NSString* FirstName = [[detailsToShowTemp valueForKey:@"PatientProfile"]valueForKey:@"FirstName"];

    NSString* LastName = [[detailsToShowTemp valueForKey:@"PatientProfile"]valueForKey:@"LastName"];
    NSString* partioalImage = [[detailsToShowTemp valueForKey:@"PatientProfile"]valueForKey:@"ImageURL"];
    NSString* partioalImageURL=nil;

    if (![partioalImage isEqualToString:@""""]) {
        partioalImageURL = [partioalImage substringFromIndex:3];
    }
    else{
        partioalImageURL = @"images/profile-picture.png";
    }

    NSString* imageURL = [NSString stringWithFormat:@"%@%@",BaseURLString,partioalImageURL];
    NSURL *url = [NSURL URLWithString:imageURL];

    BOOL requestDateStatus = [self checkFutureDateRequest:dateText];

    if (requestDateStatus==NO) {
        if (Mailcount==0) {
            cellForDatePickCell *cell = (cellForDatePickCell*)[self.tableView dequeueReusableCellWithIdentifier:kOtherCellIdentifier];
            cell.delegate_Dtepick = self;

            //if (indexPath.row <= [ScheduleView getRequestForDate].count) {

            //NSLog(@"IndxPath.row : %ld",(long)indexPath.row);

            // NSDictionary* detailsToShowTemp= [[ScheduleView getRequestForDate]objectAtIndex:indexPath.row-1];




            cell.appoinment_Dtepick = [[detailsToShowTemp valueForKey:@"Appointment"]valueForKey:@"AppointmentId"];

            cell.USER_Dtepick = [[detailsToShowTemp valueForKey:@"Appointment"]valueForKey:@"UserId"];
            userID = cell.USER_Dtepick;


            cell.startTime_Dtepick =[[[detailsToShowTemp valueForKey:@"Appointment"]valueForKey:@"DayTimeSlot"]valueForKey:@"StartTime"];
            cell.endTime_Dtepick =[[[detailsToShowTemp valueForKey:@"Appointment"]valueForKey:@"DayTimeSlot"]valueForKey:@"EndTime"];
            cell.Time_Dtepick.text = [NSString stringWithFormat:@"%@ - %@",[[[detailsToShowTemp valueForKey:@"Appointment"]valueForKey:@"DayTimeSlot"]valueForKey:@"StartTime"], [[[detailsToShowTemp valueForKey:@"Appointment"]valueForKey:@"DayTimeSlot"]valueForKey:@"EndTime"]];




            cell.profileImage_Dtepick.imageURL = url;

            cell.Time_Dtepick.text = [NSString stringWithFormat:@"%@ - %@",[[[detailsToShowTemp valueForKey:@"Appointment"]valueForKey:@"DayTimeSlot"]valueForKey:@"StartTime"], [[[detailsToShowTemp valueForKey:@"Appointment"]valueForKey:@"DayTimeSlot"]valueForKey:@"EndTime"]];


            cell.Name_Dtepick.text = [NSString stringWithFormat:@"%@ %@",FirstName,LastName];

            cell.Date_Dtepick.text = dateText;

            return cell;

        }

        else if (ReviewID==0){
            NotReviewedCell *cell = (NotReviewedCell*)[tableView dequeueReusableCellWithIdentifier:@"NotReviewedCell"];
            cell.notReviewDelegate = self;
            if (! cell) {
                NSArray *parts = [[NSBundle mainBundle] loadNibNamed:@"NotReviewedCell" owner:nil options:nil];
                cell = [parts objectAtIndex:0];
            }
            cell.appoinmentID_green_Nreview = [[detailsToShowTemp valueForKey:@"Appointment"]valueForKey:@"AppointmentId"];
            cell.USER_green_Nreview = [[detailsToShowTemp valueForKey:@"Appointment"]valueForKey:@"UserId"];
            userID = cell.USER_green_Nreview;
            cell.appoinmentstartTime_green_Nreview =[[[detailsToShowTemp valueForKey:@"Appointment"]valueForKey:@"DayTimeSlot"]valueForKey:@"StartTime"];
            cell.appoinmentendTime_green_Nreview =[[[detailsToShowTemp valueForKey:@"Appointment"]valueForKey:@"DayTimeSlot"]valueForKey:@"EndTime"];

            cell.cellProfileImage.imageURL = url;

            cell.appoinmentTime_green_Nreview.text = [NSString stringWithFormat:@"%@ - %@",[[[detailsToShowTemp valueForKey:@"Appointment"]valueForKey:@"DayTimeSlot"]valueForKey:@"StartTime"], [[[detailsToShowTemp valueForKey:@"Appointment"]valueForKey:@"DayTimeSlot"]valueForKey:@"EndTime"]];

            cell.personName_green_Nreview.text = [NSString stringWithFormat:@"%@ %@",FirstName,LastName];

            cell.appoinmentDate_green_Nreview.text = dateText;

            return cell;
        }
        else{

            ReviewedCell *cell = (ReviewedCell*)[tableView dequeueReusableCellWithIdentifier:@"ReviewedCell"];
            cell.ReviewedDelegate = self;
            if (! cell) {
                NSArray *parts = [[NSBundle mainBundle] loadNibNamed:@"ReviewedCell" owner:nil options:nil];
                cell = [parts objectAtIndex:0];
            }
            cell.appoinmentID_Reviewed = [[detailsToShowTemp valueForKey:@"Appointment"]valueForKey:@"AppointmentId"];
            cell.USER_Reviewed = [[detailsToShowTemp valueForKey:@"Appointment"]valueForKey:@"UserId"];
            userID = cell.USER_Reviewed;
            cell.appoinmentstartTime_Reviewed =[[[detailsToShowTemp valueForKey:@"Appointment"]valueForKey:@"DayTimeSlot"]valueForKey:@"StartTime"];
            cell.appoinmentendTime_Reviewed =[[[detailsToShowTemp valueForKey:@"Appointment"]valueForKey:@"DayTimeSlot"]valueForKey:@"EndTime"];

            cell.ReviewedProfileImage.imageURL = url;

            cell.appoinmentTime_Reviewed.text = [NSString stringWithFormat:@"%@ - %@",[[[detailsToShowTemp valueForKey:@"Appointment"]valueForKey:@"DayTimeSlot"]valueForKey:@"StartTime"], [[[detailsToShowTemp valueForKey:@"Appointment"]valueForKey:@"DayTimeSlot"]valueForKey:@"EndTime"]];

            cell.personName_Reviewed.text = [NSString stringWithFormat:@"%@ %@",FirstName,LastName];

            cell.appoinmentDate_Reviewed.text = dateText;
            return cell;


        }
    }
    else{
        CellForComingDates *cell = (CellForComingDates*)[self.tableView dequeueReusableCellWithIdentifier:@"ComingDateCell"];
        //cell.comingDateDel =self;
        [cell setComingDateDel:self];
        if (! cell) {
            NSArray *parts = [[NSBundle mainBundle] loadNibNamed:@"comingDayCell" owner:nil options:nil];
            cell = [parts objectAtIndex:0];
        }
        cell.appoinmentID = [[detailsToShowTemp valueForKey:@"Appointment"]valueForKey:@"AppointmentId"];
        cell.USER = [[detailsToShowTemp valueForKey:@"Appointment"]valueForKey:@"UserId"];
        userID = cell.USER;
        cell.appoinmentstartTime =[[[detailsToShowTemp valueForKey:@"Appointment"]valueForKey:@"DayTimeSlot"]valueForKey:@"StartTime"];
        cell.appoinmentendTime =[[[detailsToShowTemp valueForKey:@"Appointment"]valueForKey:@"DayTimeSlot"]valueForKey:@"EndTime"];

        cell.cellImage.imageURL = url;

        cell.appoinmentTime.text = [NSString stringWithFormat:@"%@ - %@",[[[detailsToShowTemp valueForKey:@"Appointment"]valueForKey:@"DayTimeSlot"]valueForKey:@"StartTime"], [[[detailsToShowTemp valueForKey:@"Appointment"]valueForKey:@"DayTimeSlot"]valueForKey:@"EndTime"]];

        cell.personName.text = [NSString stringWithFormat:@"%@ %@",FirstName,LastName];

        cell.appoinmentDate.text = dateText;
        return cell;
    }

    }


    return cell;

}

5 个答案:

答案 0 :(得分:1)

我想你忘记了[cell setComingDateDel:self];

cellForRowAtIndexPath

中添加此调用

<强>更新

尝试在[cell setComingDateDel:self];之前添加return

答案 1 :(得分:0)

内部cellForRowAtIndexPath委托使用此代码:

[cell.deleteButton setTag:indexPath.row];
[cell.deleteButton addTarget:self 
                       action:@selector(deletebuttonClicked:)
            forControlEvents:UIControlEventTouchDown];

然后

-(void)deletebuttonClicked:(UIButton*)button {

    long int selectedIndex=(long int)[button tag];
 }

答案 2 :(得分:0)

在CellForComingDates类中添加按钮并将IBAction连接到按钮,然后

- (IBAction)DeleteButton:(id)sender {
    [self.comingDateDel DeleteButtonAction_new:self];
}

然后在你的.m中写下你的删除方法,它有tableview。

答案 3 :(得分:0)

您为cellForRowAtIndexPath

撰写的代码中的细微更改
>>[cell setComingDateDel:self];  // Move this line to the if block

 if (! cell) 
 {
   NSArray *parts = [[NSBundle mainBundle] loadNibNamed:@"comingDayCell" owner:nil options:nil];
   cell = [parts objectAtIndex:0];
   [cell setComingDateDel:self]; // Now this is proper
  }

答案 4 :(得分:0)

通过调试检查以下功能:

- (IBAction)DeleteButton:(id)sender {
if ([self.comingDateDel respondsToSelector:@selector(DeleteButtonAction_new:)]) {
    [self.comingDateDel DeleteButtonAction_new:self];
}
}
相关问题