自定义单元格iphone中带有按钮的transfer变量



大家好!!我有个问题,我不知道该怎么办!!我的目标是在customcell中的buttom中clic时,将一个TableviewController的变量转移到另一个ViewController中。我使用自定义单元格,在自定义单元格中我有两个标签,一个按钮。这是我的代码:

文章Cell.h

#import <UIKit/UIKit.h>
#import "CalculerViewController.h"
@interface ArticlesCell : UITableViewCell
@property (strong, nonatomic) IBOutlet UILabel *lblproduit;
@property (strong, nonatomic) IBOutlet UILabel *lblargent;
- (IBAction)btnincrement:(id)sender;
@property (strong, nonatomic) IBOutlet UIButton *btn;
@property (strong, nonatomic) NSString  *recupererlblproduit;
@end

一个表视图控制器:

文章TableViewController.m

#import "ArticlesTableViewController.h"
#import "ArticlesCell.h"

@interface ArticlesTableViewController ()
@end
@implementation ArticlesTableViewController
@synthesize arrayargent1,arrayproduit1,arrayargent2,arrayproduit2,recuperationproduit;
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
recuperationproduit=nil;
arrayproduit1 = [[NSMutableArray alloc] initWithObjects:@"Câble Ethernet UTP-CAT5 ",@"Câble Ethernet UTP-CAT6 ",@"Carte Réseau",@"Hub",@"Switch",@"Routeur",nil];
arrayargent1 = [[NSMutableArray alloc] initWithObjects:@"10 000",@"15 000 ",@"250 000",@"300 000",@"500 000",@"550 000",nil];
arrayproduit2 = [[NSMutableArray alloc] initWithObjects:@"Ram ",@"Disque Dur",@"Câble d'Alimentation",@"Carte Mere",@"Processeur",nil];
arrayargent2 = [[NSMutableArray alloc] initWithObjects:@"100",@"15 000 ",@"250 000",@"300 000",@"500 000",@"550 000",nil];

}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section == 0)
return self.arrayproduit1.count;
if (section == 1)
return self.arrayproduit2.count;
return 0;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
if (section == 0)
return @"Matériels Réseaux";
if (section == 1)
return @"Matériels Ordinateur";
return @"undefined";
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"seguecalcule"])
{
UINavigationController *navigationController = segue.destinationViewController;
CalculerViewController *calculerViewController = [[navigationController viewControllers] objectAtIndex:0];
calculerViewController.introlblproduit=recuperationproduit;
calculerViewController.delegate = self;
}

}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"CellArticle";

ArticlesCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[ArticlesCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

if (indexPath.section == 0){
cell.lblproduit.text = [arrayproduit1 objectAtIndex:indexPath.row];
cell.lblargent.text = [self.arrayargent1 objectAtIndex:indexPath.row];
}
else if (indexPath.section == 1){
cell.lblproduit.text = [self.arrayproduit2 objectAtIndex:indexPath.row];
cell.lblargent.text = [self.arrayargent2 objectAtIndex:indexPath.row];
}  
return cell;
}
- (void)calculerViewControllerDidCancel:(CalculerViewController *)cancel
{
[self dismissViewControllerAnimated:YES completion:nil];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

}
@end

CalculaterViewController.m

#import "CalculerViewController.h"
@interface CalculerViewController ()
@end
@implementation CalculerViewController
@synthesize display,lbltitre,delegate,introlblproduit;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
lbltitre.text=introlblproduit;

}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)valider:(id)sender {
}
- (IBAction)cancel:(id)sender {
[self.delegate calculerViewControllerDidCancel:self];
}
@end

当我点击底部时,我想转移我的自定义单元格的标签。请帮帮我!!

最简单的答案是将每个单元格中的按钮直接连接到视图控制器中的相同方法,然后使用sender参数访问点击的单元格。

类似的东西(未测试):

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"CellArticle";
ArticlesCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[ArticlesCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// When the user taps the cell button, fire a method on the view controller
[cell.btn addTarget:self action:@selector(cellButtonTapped:) forControlEvents:UIControlEventsTouchUpInside];
...
}
- (void) cellTapped:(id)sender
{
ArticleCell *cell = sender.parent; // XXX: This is where it gets ugly!
// Now send your cell content to your other view controller.
}

但正如您所看到的,这种方法变得非常丑陋,因为您必须了解单元格中的视图层次结构。

因此,更好的方法是使用委托模式:让UITableViewCell子类有一个连接到UIViewController实例的委托。

在你的情况下,它看起来像这样:

// Forward declaration required because protocol references class
@protocol ArticlesCellDelegate;
@interface ArticlesCell : UITableViewCell
@property (strong, nonatomic) IBOutlet UILabel *lblproduit;
@property (strong, nonatomic) IBOutlet UILabel *lblargent;
- (IBAction)btnincrement:(id)sender;
@property (strong, nonatomic) IBOutlet UIButton *btn;
@property (strong, nonatomic) NSString  *recupererlblproduit;
// New delegate property
@property (strong, nonatomic) id<ArticleCellDelegate> delegate;
@end
@protocol ArticlesCellDelegate
- (void) didTapButtonInArticleCell:(ArticleCell*)cell;
@end

然后在您的视图控制器中:

@interface ArticlesTableViewController () <ArticleCellDelegate>
@end
@implementation ArticlesTableViewController
...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"CellArticle";
ArticlesCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[ArticlesCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Make the view controller the cell's delegate
cell.delegate = self;
...
}
- (void) didTapButtonInArticleCell:(ArticleCell*)cell  {
// Now send your cell content to your other view controller.
}

你需要的另一件事是在你的文章单元格类中,当点击按钮时,你需要处理委托。你的提示:方法可以做到:

@implementation ArticleCell
...
- (IBAction)btnincrement:(id)sender;
{
if( self.delegate )
[self.delegate didTapButtonInArticleCell:self];
}

顺便说一句,如果我是你,我会尽可能多地将这些UI元素从界面定义中移出。接口部分正是它所说的:一个定义接口的地方。您通常希望在实现部分封装视图的内部工作,并使接口部分反映您希望它的使用方式。

我怀疑在您的prepareForSegue方法中,您的导航控制器的子视图控制器尚未创建,并且进入导航控制器并试图操纵其根视图控制器无论如何都是糟糕的设计。

我建议将您的导航控制器作为UINavigationController的自定义子类。赋予它属性来保持你的价值,并在你的准备ForSegue中设置它们。

然后在CalculaterViewController的视图DidLoad中,获取导航控制器,将其转换为自定义类,并从导航控制器中读取数据。

最新更新