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

标签 ios objective-c uitableview

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

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

我想使用 selectedMapButton 在 TableViewController.m 上推送 View
我试过这段代码,但有些不对劲。
- (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];
}

我该如何解决?谢谢。

最佳答案

委托(delegate)/协议(protocol)的替代方式(我最好的方式);
-创建一个“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)
 }

Mert kardeşim 代表 ve 协议(protocol) en doğru çözüm olacaktır ;)

关于ios - 如何使用按钮操作(带参数)从自定义 UITableViewCell 推送 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23103906/

相关文章:

iOS 钥匙串(keychain)值无

ios - 在 Assets 目录中使用矢量图作为 iOS 启动图像?

ios - 单击按钮增加和减少值

ios - indexPathForSelectedRow 始终为所选部分和行返回 0

ios - 在 Swift 2.0 中向 UITableView 添加图像

ios - 关闭 iOS 模式后,产生 WaitForSeconds 中断 (Unity 3D)

ios - 静态库和 XCode 4.x

ios - 无法将 Facebook 和 Twitter 添加到 UIActivityViewController

iphone - 使进度 View 可见

objective-c - objective-c 中的继承无法显示我的类(class)中的值