如何使用按钮操作(带参数)从自定义UITableViewCell推送视图

时间:2014-04-16 08:26:38

标签: ios objective-c uitableview

我在自定义单元格上添加了一个按钮。它推动了一个观点。我在CustomTableViewCell.h上为按钮创建了属性

@property (weak, nonatomic) IBOutlet UIButton *selectedMapButton;

我想使用selectedMapButton来推送TableViewController.m上的视图 我试过这段代码,但有些事情是不对的。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cord = cellSight[@"S_Coodinates"];

    [cell.selectedMapButton addTarget:self action:@selector(sightMapButton:cord) forControlEvents:UIControlEventTouchUpInside];

    //When I call sightMapButton method. It gives an error

    return cell;
}

- (void)sightMapButton: (NSString *)withCoordinates
{
    TOCGSightseeingMapKitViewController *mapKitController = [[TOCGSightseeingMapKitViewController alloc] init];
    mapKitController.sightCoordinates = withCoordinates;

    [self.navigationController pushViewController:mapKitController animated:YES];
}

我该如何解决?感谢。

7 个答案:

答案 0 :(得分:3)

委托/协议的替代方式(我最好的方式);

-创建一个“CustomCellDelegate”类。 (基类是NSObject)
 -打开CustomCellDelegate.h并添加此代码;

@protocol CustomCellDelegate <NSObject>
   - (void) uiTapButtonWithParameter:(NSString *)parameter;    
@end

-关闭CustomCellDelegate.h
 -打开CustomCell.h并添加此代码;

#import "CustomCellDelegate"

@interface CustomCell : UITableViewCell
@property (nonatomic) id <CustomCellDelegate> delegate;

-关闭CustomCell.h
 -打开CustomCell.m并在UIButton操作方法中添加代码;

[_delegate uiTapButtonWithParameter:@"hello world!"];

-关闭CustomCell.m
 -打开CustomViewController.h并添加此代码;

@interface CustomViewController : UIViewController <UITableViewDelegate,   UITableViewDataSource, CategoryItemsCellDelegate>

-关闭CustomViewController.h并打开CustomViewController.m
 -添加此代码;

- ( NSInteger )tableView:( UITableView * )tableView numberOfRowsInSection:( NSInteger    )section; {
    return 20;
}

- ( UITableViewCell * )tableView:( UITableView * )tableView cellForRowAtIndexPath:( NSIndexPath * )indexPath; {

  CategoryItemsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CustomCell"];

  cell.delegate = self;

  return cell;
}

- (void) uiTapButtonWithParameter:(NSString *)parameter{
     NSLog(@"%@",parameter)
 }

Mertkardeşim委托协议endoğruçözümoralactır;)

答案 1 :(得分:1)

您无法为按钮操作处理程序指定自己的参数。其带有1个参数的变体将接受“发送者”,即触发操作的按钮。您的工作流程应遵循:

  1. 查找单击按钮的单元格的indexPath
  2. 获取与该indexPath对应的cellSight对象
  3. 使用来自cellSite对象的信息填充控制器。
  4. (有点伪)代码看起来像,假设你有SiteObjects数组:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        cell = ...  // create/init cell somehow
    
        [cell.selectedMapButton addTarget:self action:@selector(sightMapButton:) forControlEvents:UIControlEventTouchUpInside];
    
        return cell;
    }
    
    - (void)sightMapButton: (UIButton *)sender
    {
        NSIndexPath *indexPath = ... // Find indexPath of button
        SiteObject cellSight = sitesArray[indexPath.row];
        TOCGSightseeingMapKitViewController *mapKitController = [[TOCGSightseeingMapKitViewController alloc] init];
        mapKitController.sightCoordinates = cellSight[@"S_Coodinates"];
    
        [self.navigationController pushViewController:mapKitController animated:YES];
    }
    

    如何查找包含按钮的indexPath,例如in my other answer

答案 2 :(得分:1)

在自定义单元格类中为按钮添加action并定义协议(Delegate)从按钮操作调用委托方法。在cellForRowAtIndexPath中将您的单元格的delegate设置为self并实施委托方法。 如果您有任何疑问,请告诉我。

答案 3 :(得分:0)

我认为问题出在@selector(sightMapButton:cord)中,用于检索方法的地址。很可能你不允许在这里传递参数。

作为查找单击按钮的单元格的indexPath的替代方法: 您可以子类化UIButton并将所需信息作为属性添加到其中。然后你可以按照弗拉基米尔建议的方式行事,并在将它发送到你的Button类之后从发送者处获取你的电话。

答案 4 :(得分:0)

弗拉基米尔的回答是正确的,我试过了。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    cell.selectedMapButton.tag = indexPath.row;

    [cell.selectedMapButton addTarget:self action:@selector(sightMapButton:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)sightMapButton:(UIButton *)sender
{
    TOCGSightseeingMapKitViewController *mapKitController = [[TOCGSightseeingMapKitViewController alloc] init];
    mapKitController.sightCoordinate = self.sights[sender.tag][@"S_Coordinates"];
    [self.navigationController pushViewController:mapKitController animated:YES];
}

它也有效。

答案 5 :(得分:0)

  • 我们可以使用块来获取cellIndexPath。
  • 而不是使用CellDelegate来调用按钮操作 更好的选择。
  

@interface WACustomCell:UITableViewCell

@property(readwrite,copy)void (^cellCallBackMethod)(NSInteger rowIndexPath);

-(IBAction)buttonTappedFromCell:(id)sender;

@end
 @implementation WACustomCell

 @synthesize cellCallBackMethod;


     -(IBAction)buttonTappedFromCell:(id)sender{

 self.cellCallBackMethod(0);

 //any value can be given as we will retrive this value from CellForRowAtIndex method


 }

在View Controller中

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{


cel = ....
cell.cellCallBackMethod = ^(NSInteger cellIndex){

    [self cellButtonTappedFromCellWithIndex:cellIndex];
};

return cell;
}
-(void)cellButtonTappedFromCellWithIndex:(NSInteger )cellIndex{


SiteObject cellSight = sitesArray[cellIndex];
TOCGSightseeingMapKitViewController *mapKitController = [[TOCGSightseeingMapKitViewController alloc] init];
mapKitController.sightCoordinates = cellSight[@"S_Coodinates"];

    [self.navigationController pushViewController:mapKitController animated:YES];

}

答案 6 :(得分:-1)

您应该在自定义表格视图单元格中添加该方法。

相关问题