【问题标题】:UIButton action handle inside table view cell表格视图单元格内的 UIButton 操作句柄
【发布时间】:2014-04-19 14:10:43
【问题描述】:

我有一个这样的表格视图。

我所做的是,我为我的单元创建了一个单独的类,并且我在为单元创建的 .m 文件中有 IBAction。

在我的 TableViewController 类中,我有一个由 Web 服务检索的字典。[我正在从中加载这些详细信息。] 现在我想通过单击接受按钮将某些内容发送回我的 Web 服务。

由于我的详细信息字典在 TableViewController 类中,而 IBAction 在单独的类中,我不知道该怎么做。我知道如何做到这一点的逻辑。但我不知道如何从 tableviewControler 的 cellForRowAtIndexPath 方法中的字典变量中获取某些信息..

谁能给我一些想法?请...

编辑:

这里是我的 cellForRowAtIndexPath 方法

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

}

在 tempDictionary 中我有一些东西要使用。该值应该通过按接受按钮传递给服务。这就是我想要做的

【问题讨论】:

    标签: ios dictionary uitableview


    【解决方案1】:

    委托模式正是您所需要的。这是good documentation。 您应该将TableViewController 设为delegatetableViewCell

    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 
    }
    

    【讨论】:

    • 实际上我是 IOS 新手。我对代表一无所知。你能用代码解释一下吗?或者你能给我一些关于它的教程吗。。[关于我的问题]
    • 谢谢你..我会搜索它并尝试一下:)
    • 先生,我有一个问题......在 cellForIndexPath 下面的 MYTAbleviewControler.m 类中,我必须实现我的点击事件对吗?所以这个方法也有我的单元格参数..是IOS本身将单元格传递给这个方法吗??
    • 不确定我是否理解您的问题。您能否在cellForIndexPath 方法中发布您所拥有的内容?我们的想法是保留您拥有的所有内容并添加cell.delegate = self; 行。
    • 我已经发布了,先生...你可以看看吗?
    【解决方案2】:

    有两个简单的解决方案:

    • 使用自定义协议。这里是a link! .
    • 使用键值观察。这是a link

    我的建议是使用自定义协议。

    【讨论】:

      【解决方案3】:

      在 UITABLEVIEWDATASOURCE 方法下面,

      - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)
      {
      // give buttons to your cell`s contentview.
      
      then,
      [Acceptbutton or CancelButton  addTarget:nil action:@selector(yourbtnselector:) forControlEvents:UIcontrolEventTouchUPInside];
      
      }
      

      那么, 在按钮选择器中,

      -(voide)ButtonSelector: (id)sender
      {
      
      if(sender == accept Button)
      {
      // DO your Stuff.
      }
      
      if(sender == Cancel Button)
      {
      // DO your Stuff.
      }
      
      
      }
      

      所以,您不需要为您的案例实现 Tableview didselectrow 方法。

      【讨论】:

      • 先生,我能理解您提出的建议,除了一件事...在您提到的 cellforRowatIndexpath 方法中,您可以为您的单元格内容视图提供按钮.. 那是什么?你的意思是按钮的实例?你能进一步解释一下吗?
      • 您可以分配,在cellforrow atindexpath中初始化您的按钮,然后根据您在单元格中的需要设置它的位置,然后执行[cell.contentview addsubview:您的按钮];以编程方式实现 CustomCell。
      • 如果回答对您有帮助,请接受并放弃,以供他人参考
      【解决方案4】:

      在@dariaa 的解决方案之后回答您的疑问是,当您单击将调用 MyCell 类中的操作方法的按钮时,再次通过该方法将控制权交还([self.delegate buttonTappedInCell:self])连同参数self(当前类对象)到单击按钮的控制器类。

      【讨论】:

        猜你喜欢
        • 2015-05-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-07-16
        • 1970-01-01
        相关资源
        最近更新 更多