简体   繁体   English

委托按钮单击在表格单元格中不起作用

[英]Delegate for a Button Click not working in tableview cell

I am using Delegate method to access my table view's cell's information for button click methods.my table view cells are customised. 我正在使用Delegate方法访问我的表视图单元格的信息以获取按钮单击方法。我的表视图单元格是自定义的。 i have design the cells in a separate xib file and also declare a tableViewCell type class for the cell.and given that class to the cell which i have designed. 我已经在单独的xib文件中设计了单元格,还为该单元格声明了tableViewCell类型类,并将该类提供给我设计的单元格。 I am loading different cell designs according to a date and some other conditions in the same table view. 我在同一张表格视图中根据日期和其他一些条件加载不同的单元格设计。 But the problem is when i click a button inside my cell, it is not working. 但是问题是当我单击单元格内的按钮时,它不起作用。 I have put a break point to the place where the code is, but it is not going to the breakpoint. 我已经在代码所在的位置放置了一个断点,但是它并没有到达断点。

Here is the steps i followed. 这是我遵循的步骤。 It was worked fine when i designed my table cell inside the prototype cell using storyboards . 当我使用storyboards在原型单元中设计表格单元时,效果很好。 I did the same thing using xib, but i don't know what is the wrong. 我使用xib做过同样的事情,但是我不知道怎么了。

this is my customised cell's .h class which i declare the delegate #import 这是我的自定义单元格的.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

Here is the .m class which my IBAction method is included. 这是包含我的IBAction方法的.m类。 and i have connect the IBaction to the button correctly 我已经正确地将IBaction连接到按钮了

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

Then, inside the tableView class, i have implemented the click method 然后,在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];
}

but when i click the button it is not responding... can someone tell me what happened here ? 但是当我单击按钮时它没有响应...有人可以告诉我这里发生了什么吗?

thank you 谢谢

EDIT 编辑

here is my cellForRowAtIndexpath method 这是我的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;

} }

I think you forgot [cell setComingDateDel:self]; 我认为您忘记了[cell setComingDateDel:self];

Add this calling in cellForRowAtIndexPath . 将此调用添加到cellForRowAtIndexPath

UPDATE: 更新:

Try to add [cell setComingDateDel:self]; 尝试添加[cell setComingDateDel:self]; right before you do return . 就在你return之前。

Inside cellForRowAtIndexPath delegate use this code: cellForRowAtIndexPath委托内部使用此代码:

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

then 然后

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

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

add button in CellForComingDates class and connect IBAction to the button, then 在CellForComingDates类中添加按钮,并将IBAction连接到该按钮,然后

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

then after write your delete method in your .m which have tableview. 然后在您的.m文件中添加具有tableview的delete方法。

Small change in the code you have written for cellForRowAtIndexPath 您为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
  }

Check below function by debugging: 通过调试检查以下功能:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM