简体   繁体   中英

UIButton action handle inside table view cell

我有这样的表格视图

I have a table view like this.

What i did was, I created a separate class for my cell, and i have IBAction in the .m file which i created for the cell.

In my TableViewController class i have got a dictionary which is retrieved by a web service.[im loading these details from that.] Now I want to send something back to my web service by clicking accept button.

Since my details dictionary is in TableViewController class and the IBAction is in separate class i have no idea how to do that. i know the logic how to do that. but i have no idea how do i get the certain information from my dictionary variable which is inside tableviewControler's cellForRowAtIndexPath method..

Can anyone give me some idea ? please...

Edit:

here my cellForRowAtIndexPath method

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
  {
  static NSString *CellIdentifier = @"Cell";
   TacleCell *cell = (TacleCell*)[tableView     dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

NSDictionary *tempDictionary= [self.googlePlaces objectAtIndex:indexPath.row];
NSString* FirstName = [[tempDictionary valueForKey:@"PatientProfile"]valueForKey:@"FirstName"];
NSString* LastName = [[tempDictionary valueForKey:@"PatientProfile"]valueForKey:@"LastName"];
//NSString* FullName = [NSString stringWithFormat:@"%@ %@",FirstName,LastName];

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




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

cell.profileImage.image = [UIImage imageNamed:@"bookmark.png"];
// cell.textLabel.text = [[tempDictionary valueForKey:@"Appointment"]valueForKey:@"AgencyName"];

if([[tempDictionary valueForKey:@"Appointment"]valueForKey:@"AgencyId"] != NULL)
{
    cell.Date.text = [NSString stringWithFormat:@"AppoinmentID: %@",[[tempDictionary valueForKey:@"Appointment"]valueForKey:@"AgencyId"]];
}
else
{
    cell.Date.text = [NSString stringWithFormat:@"Not Rated"];
}

return cell;

}

in tempDictionary i have something to use.and that value should pass to the service by pressing Accept button.. that is what i want to do

The delegation pattern is just what you need for that. Here is a good documentation . You should make your TableViewController a delegate of tableViewCell .

MYCell.h:

@class MYCell;

@protocol MYCellDelegate <NSObject>

- (void)buttonTappedInCell:(MYCell *)cell;

@end

@interface MYCell : UITableViewCell

@property (weak, nonatomic) id<MYCellDelegate> delegate;

@end

MYCell.m:

...
- (IBAction)buttonCliked:(id)sender
{
    [self.delegate buttonTappedInCell:self];
}

MYTableViewController.m:

...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath     *)indexPath
{
    MYCell *cell = /// Create your cell
    cell.delegate = self;

    return cell;
}

- (void)buttonTappedInCell:(MYCell *)cell
{
   ///  Send something back to your web service 
}

in below UITABLEVIEWDATASOURCE method,

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)
{
// give buttons to your cell`s contentview.

then,
[Acceptbutton or CancelButton  addTarget:nil action:@selector(yourbtnselector:) forControlEvents:UIcontrolEventTouchUPInside];

}

then, in Button selector,

-(voide)ButtonSelector: (id)sender
{

if(sender == accept Button)
{
// DO your Stuff.
}

if(sender == Cancel Button)
{
// DO your Stuff.
}


}

so, you dont need to implement Tableview didselectrow method for your case.

@dariaa解决方案后您的疑问的答案是,当您单击将调用MyCell类中的操作方法的按钮,然后再次通过方法([self.delegate buttonTappedInCell:self])与参数一起将控件重新交出控件时self(当前类对象)到单击按钮的控制器类。

There 2 simple solutions for that:

  • Using custom protocols. Here's a link ! .
  • Using Key-Value Observing. Here's a link !

My suggest is using custom protocol.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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