ios - 委托(delegate)按钮单击在 tableview 单元格中不起作用

标签 ios objective-c uitableview delegates

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

这是我遵循的步骤。当我使用 storyboards 在原型(prototype)单元格内设计我的表格单元格时,它运行良好。我使用 xib 做了同样的事情,但我不知道哪里出了问题。

这是我自定义的单元格的 .h 类,我声明了委托(delegate) #导入

@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 类中,我实现了点击方法

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

最佳答案

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

cellForRowAtIndexPath 中添加此调用。

更新:

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

关于ios - 委托(delegate)按钮单击在 tableview 单元格中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23630145/

相关文章:

objective-c - 循环缓冲录音iOS : Possible?

iphone - 为东部时区的 future 日期设置本地通知

ios - 如何清除 Restkit 中 UITableView 的 NSFetchedResultsController 结果

iphone - 没有替换图像和单元格的 UITableView 重绘

ios - 如果数据源计数可能发生变化,是否可以同时使用 reloadItemsAtIndexPaths 和 reloadData?

ios - TabBarController 中的 PersonViewController

ios - 将圆角和边框设置为 ios7 中 UISearchBar 的背景(不是 UITextField)

iphone - "ExtAudioFileWriteAsync -50"表示什么错误?

ios - UITableViewCell 动态高度不适用于大小类

ios - 检测单元格内 UILabel 上的点击