【问题标题】:UIViewController in Customized UITableViewCell自定义 UITableViewCell 中的 UIViewController
【发布时间】:2015-11-18 08:05:37
【问题描述】:

我知道这是微不足道的,但我把自己搞砸了,花了好几个小时。

我有一个 UIViewController,它由 CategoryTableview 和 ProductsTableView 组成。

ProductsTableView 正在创建名为 ProductTableViewCell 的自定义 TableViewCell。

ProductTableViewCell 有标题标签、细节标签、价格标签和一个自定义的步进器来控制从 UIViewController 子类化的产品的数量。

步进器由两个按钮和一个文本框组成。

我认为我实现程序的方式比它应该的要复杂。

我的问题是如何检测父视图控制器中步进控制器更改的值。

代码是:

ProductionViewController.h (parent view controller for listing everything)

@interface ProductionViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic,strong) UITableView* categoryTable;
@property (nonatomic,strong) UITableView* productsTable;

@property (nonatomic,strong) CheckOutBar* checkOutBar;

//datasources
@property (nonatomic,strong) NSMutableArray* menuLists;
@property (nonatomic,strong) NSMutableArray* detailLists;

//datefield
@property (nonatomic,strong) NSString* startDate;
@property (nonatomic,strong) NSString* endDate;

产品的自定义单元格是

@interface ProductCell : UITableViewCell

@property (nonatomic, strong) UIImageView* eqiupmentImage;
@property (nonatomic, strong) UILabel* Title;
@property (nonatomic, strong) UILabel* details;
@property (nonatomic, strong) UILabel* prices;
@property (nonatomic, strong) MCStepperViewController* stepper;

cell的实现方法是

     @implementation ProductCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    {
        _Title = [[UILabel alloc] init];
        _eqiupmentImage = [[UIImageView alloc] init];
        _details = [[UILabel alloc] init];
        _details.font = kFONT(12);
        _details.textColor = KColor_Gray;

        _prices  = [[UILabel alloc] init];
        _stepper = [[MCStepperViewController alloc] init];
        [self.contentView addSubview:_stepper.view];
        [self.contentView addSubview:_Title];
        [self.contentView addSubview:_eqiupmentImage];
        [self.contentView addSubview:_details];
        [self.contentView addSubview:_prices];
    } 
        return self;
    }

-(void)layoutSubviews
{
    [super layoutSubviews];
    [_eqiupmentImage setFrame:CGRectMake(kLeftMargin, kLeftMargin, 80 ,80)];
    [_Title setFrame:CGRectMake(10+CGRectGetMaxX(_eqiupmentImage.frame), kLeftMargin/2, 60, 20)];
    [_details setFrame:CGRectOffset(_Title.frame, 0, 20)];
    [_prices setFrame:CGRectMake(10+CGRectGetMaxX(_eqiupmentImage.frame), CGRectGetMaxY(_eqiupmentImage.frame)-20, 40, 20)];
    [_stepper.view setFrame:CGRectOffset(_prices.frame, 40, 0)];
}

自定义的stepperview是

MCStepperViewController.h  (for controlling the amount of products)
@interface MCStepperViewController : UIViewController

@property (nonatomic, strong) UITextField  *amountText;
@property (nonatomic, strong) UIButton * plusButton;
@property (nonatomic, strong) UIButton * minusButton;

@property (nonatomic, assign) int max;
@property (nonatomic, assign) int min;

@property (nonatomic, assign) int amount;

谢谢

【问题讨论】:

  • 探索键值观察 (KVO) 的概念。您可以在父视图控制器中观察“数量”变量的值。每次更改值时,父级都会收到通知,您可以指定要调用的方法。
  • @TheAppMentor 谢谢,因为我要观察的值是步进控制器的一个属性,而步进是单元格的属性。我不确定我是否能够直接观察主父控制器中的值,或者我应该在自定义单元格中添加一个观察者?
  • @Mix 是否希望每次值更改时都收到通知?或者您只想检查求和逻辑的当前值?
  • 只要您将金额作为属性(您拥有),您应该能够使用 KVO 观察其值的变化。另一种选择是发布 NSNotification,但我认为 KVO 旨在处理这种情况。

标签: ios objective-c iphone uitableview uiviewcontroller


【解决方案1】:

一种方法是在您的单元格中使用- (void)productCell:(ProductCell *)cell didChangeStepperValue:(NSInteger)stepperValue 之类的方法实现协议。然后你让你的视图控制器成为每个单元格的代表,并在每次步进器改变它的值时调用这个方法。要将单元格彼此分开,您可以将它们的 tag 值分配给模型的相应索引。

另一种方法是使用 KVO,但您应该小心订阅/取消订阅。当然,使用块也是一个可行的选择。

【讨论】:

  • 谢谢,我已经在cell类中实现了一个delegate,但是我不能调用stepper控制器来执行delegate方法,因为stepper中没有cell属性但是stepper属性在cell类中
  • 为什么不在你的步进器类中实现另一个协议并在你的单元格中实现相应的方法(比如,- (void)stepperController:(MCStepperViewController *)stepperController valueChanged:(NSInteger)value;)?是的,它变得非常复杂,但结构非常清晰。另外,您可以在其他地方重用此控件而无需任何耦合。
  • 完成!非常感谢你。并让我对委托有更好的了解。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多