簡體   English   中英

如何使用委托Objective c從TableViewCell.xib創建到ViewControllerB的序列?

[英]How create segue to ViewControllerB from TableViewCell.xib using delegates Objective c?

我正在學習,真的需要幫助!

我有2個ViewController:帶有UITableView ViewControllerA (帶有TableViewCell .h .m .xib文件)和ViewControllerB

我有“連接按鈕” - (IBAction)goButton:(id)sender; CustomCell

如何使用委托從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

錯誤: cell.delegate = self; 在'UITableViewCell *'類型的對象上找不到屬性'delegate'

如下更新代碼:

CustomCell.h

#import <UIKit/UIKit.h>
@interface TableViewCell : UITableViewCell
@property (weak, nonatomic) IBOutlet UIButton *btnSegue; //Connet this button outlet to your cell in storyboard
@end

CustomCell.m

#import "TableViewCell.h"

@implementation TableViewCell
- (void)awakeFromNib {
    [super awakeFromNib];

}

ViewControllerA.m

cellForRowAtIndexPath:

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

   static NSString *MyIdentifier = @"CellIdentifier";

   TableViewCell *cell = (TableViewCell *) [tableView dequeueReusableCellWithIdentifier:MyIdentifier forIndexPath:indexPath];
    [cell.btnSegue addTarget:self action:@selector(yourSegue:) forControlEvents:UIControlEventTouchUpInside];

  //If you want some data to get fetch from cell so use this tag concept
     cell.btnSegue.tag = indexPath.row;

  }
  return cell;
}
-(void)yourSegue:(UIButton *)btn
{
 //Write your segue code here
 int tag = btn.tag;
 //Use above tag to get data which is on your selected cell

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM