我在自定义单元格上添加了一个按钮。它插入了一个观点。我在 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/