ios - 传递从自定义 tableView Cell 定义的对象

标签 ios objective-c uitableview nsmutablearray

为了解释我的情况,我有一个带有自定义 tableViewCelltableView,并且我在这个自定义单元格中创建了一个按钮。我希望此按钮添加一个已定义到 NSMutableArray 的对象 box 并将此数组传递给 restoCardConfirmationViewController 以创建购物车。

问题是,当我尝试在 restoCardConfirmationViewController 中显示 NSMutablearray 时,结果为零。这是我的代码:

boxTableViewCell :

#import "boxTableViewCell.h"
#import "RestoCardConfirmationViewController.h"
#import "RestauCardViewController.h"

@implementation boxTableViewCell {
NSMutableArray *_pickerPlace;
}
- (IBAction)select:(id)sender {
RestoCardConfirmationViewController *restauCardConfirmation = [[RestoCardConfirmationViewController alloc] init];
[restauCardConfirmation.boxesCommande addObject:_box];
NSLog(@"la box choisie est %@",_box);
self.select.enabled = NO;
if(!self.select.enabled){
    NSLog(@"button desabled");
}
}

restoCardConfirmationViewController.h :

#import <UIKit/UIKit.h>
#import <Parse/Parse.h>

@interface RestoCardConfirmationViewController : UIViewController
@property(nonatomic) NSMutableArray *boxesCommande;

restoCardConfirmationViewController.m :

#import "RestoCardConfirmationViewController.h"
#import "ChoisirDateViewController.h"

@interface RestoCardConfirmationViewController ()

@end

@implementation RestoCardConfirmationViewController

- (void)viewDidLoad {
[super viewDidLoad];
NSLog(@"The NSMutableArray in the other %@",self.boxesCommande);
// Do any additional setup after loading the view.
}

最佳答案

一种方法是委派。创建一个委托(delegate),通知每个添加到 RestoCardConfirmationViewController 的对象。

另一种更简单的方法是:

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    return [self.purchases count];
}

在 MainViewViewController 中,添加按钮单击事件,而不是在 tableview 单元格中使用它。

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
     cell.button.tag = indexPath.row;
     [cell.button addTarget:self 
                         action:@selector(select)
               forControlEvents:UIControlEventTouchUpInside];
    // ... 
}

这会调用下面的方法,同样在 MainViewController 中实现

    - (IBAction)select:(id)sender {

     int index = ((UIButton *)sender).tag;//it returns the indexPath

    [self.boxesArray addObject: self.purchases[index]];
    NSLog(@"la box choisie est %@",_box);


    self.select.enabled = NO;
    if(!self.select.enabled){
        NSLog(@"button desabled");
    }
    }

现在在 prepareforSeque 中,将创建的数组传递给 RestoCardConfirmationViewController。

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Make sure your segue name in storyboard is the same as this line
    if ([[segue identifier] isEqualToString:@"YOUR_SEGUE_NAME_HERE"])
    {
        // Get reference to the destination view controller
        RestoCardConfirmationViewController *vc = [segue destinationViewController];

        // Pass any objects to the view controller here, like...
        vc.boxesCommande = self.boxesArray;
    }
}

关于ios - 传递从自定义 tableView Cell 定义的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30168286/

相关文章:

ios - UIPinchGestureRecognizer分别在水平和垂直方向缩放

iphone - 使用 NSMutableArray 中的数据填充 UITableView

objective-c - Objective-C - 如果在 CGRectmake 上声明?

objective-c - AFNetworking 简单 HTTP 请求

ios - iOS 支持哪些 Microsoft SharePoint 身份验证模式

ios - Swift - 在 Xib 中动态设置 UITableView 高度

objective-c - 从另一个 subview 类中隐藏 subview

ios - 如何根据内容动态设置 tableview 的高度,但它应该与底部按钮保持最小距离?

ios - 单元格内带有 UITextField 的 UITableViewCell

ios - 重新加载表后,如何更改indexPath的行高?