【发布时间】:2016-12-26 07:38:20
【问题描述】:
我正在学习, 真的需要帮助!
我有 2 个 ViewController:ViewControllerA 和 UITableView(带有 TableViewCell .h .m .xib 文件)和 ViewControllerB。
我在CustomCell 中有“连接按钮”- (IBAction)goButton:(id)sender;。
如何使用委托从TableViewCell 转到ViewControllerB?
TableViewCell.h
#import <UIKit/UIKit.h>
@class TableViewCell;
@protocol TableViewCellDelegte <NSObject>
@required
- (void) customCell:(TableViewCell *)cell button1Pressed:(UIButton *)btn;
@end
@interface TableViewCell : UITableViewCell
- (IBAction)Button:(id)sender;
@property (weak, nonatomic) id<TableViewCellDelegte>delegate;
@end
TableViewCell.m
#import "TableViewCell.h"
@implementation TableViewCell
@synthesize delegate;
- (void) customCell:(TableViewCell *)cell button1Pressed:(UIButton *)btn;{
}
- (IBAction)Button:(id)sender {
}
ViewControllerA.h
#import <UIKit/UIKit.h>
#import "TableViewCell.h"
@interface ViewController : UIViewController <UITableViewDataSource, UITableViewDelegate,TableViewCellDelegte>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, assign) id delegate;
@end
ViewControllerA.m
#import "ViewController.h"
#import "TableViewCell.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize delegate;
- (void)viewDidLoad {
[super viewDidLoad];
[self setAllTableData];
_tableView.delegate = self;
_tableView.dataSource = self;
_tableView.backgroundColor = [UIColor redColor];
_tableView.rowHeight = 100;
}
- (void) setAllTableData {
//[_tableView registerNib:[UINib nibWithNibName:@"TableViewCell" bundle:nil] forCellReuseIdentifier:@"TableViewCell"];
UINib *cellNib = [UINib nibWithNibName:@"TableViewCell" bundle:nil];
[self.tableView registerNib:cellNib forCellReuseIdentifier:@"TableViewCell"];
}
- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TableViewCell"];
//static NSString *CellIdentifier = @"TableViewCell";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"TableViewCell" forIndexPath:indexPath];
if (!cell) {
cell = [[TableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"TableViewCell"];
cell.delegate = self;
}
return cell;
}
- (void) customCell:(TableViewCell *)cell button1Pressed:(UIButton *)btn{
NSLog(@"DELEGATE IS WORK");
}
@end
错误:在“UITableViewCell *”类型的对象上找不到cell.delegate = self;Property 'delegate'
【问题讨论】:
标签: ios objective-c uitableview delegates xib