[英]Delegate for a Button Click not working in tableview cell
我正在使用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;
}
我認為您忘記了[cell setComingDateDel:self];
將此調用添加到cellForRowAtIndexPath
。
更新:
嘗試添加[cell setComingDateDel:self];
就在你return
之前。
在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];
}
在CellForComingDates類中添加按鈕,並將IBAction連接到該按鈕,然后
- (IBAction)DeleteButton:(id)sender {
[self.comingDateDel DeleteButtonAction_new:self];
}
然后在您的.m文件中添加具有tableview的delete方法。
您為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
}
通過調試檢查以下功能:
- (IBAction)DeleteButton:(id)sender {
if ([self.comingDateDel respondsToSelector:@selector(DeleteButtonAction_new:)]) {
[self.comingDateDel DeleteButtonAction_new:self];
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.