【问题标题】:CustomTableCell Delegate自定义表格单元委托
【发布时间】:2017-08-04 04:45:18
【问题描述】:

我有一个名为AdminOrderViewController 的tableview,它有一个名为StepperProgressCell 的customcell。

此 customcell 有一个名为 AYStepperView 的自定义 UIView。这个 UIView 中有一个按钮,我在它上面实现了一个委托,每当它被点击时,我想在 AdminOrderViewController 上调用这个委托 clicked 方法。

但是,我不知道如何添加cell.delegate??

AdminOrderViewController.m

@interface AdminOrderViewController : UIViewController <AYStepperViewDelegate>

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
        static NSString *CellIdentifier = @"StepperProgressCell";
        StepperProgressTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
        if (!cell) {
            cell = [[StepperProgressTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
        }
        //cell.stepperView.delegate= ???
        return cell;
}

- (void)clicked
{
 // is not getting called?
}

StepperProgressTableViewCell.m

@interface StepperProgressTableViewCell ()

@property (nonatomic) AYStepperView *stepperView;
@property (nonatomic) NSUInteger currentIndex;
@property (nonatomic) NSUInteger currentStep;
@property (nonatomic) UIView *containerView;

@end

@implementation StepperProgressTableViewCell
@synthesize stepperView;

- (void)awakeFromNib {
    [super awakeFromNib];
    [self setUpViews];
}

- (void)setUpViews {

    self.stepperView = [[AYStepperView alloc]initWithFrame:CGRectMake(0, 0 , [[UIScreen mainScreen] bounds].size.width, kFormStepperViewHeight) titles:@[@"Processing",@"Ready",@"Delivered", nil)]];
    [self addSubview:self.stepperView];
}

AYStepperView.m

@protocol AYStepperViewDelegate <NSObject>

@required
- (void)clicked;

@end

- (void)buttonPressed:(UIButton *)sender {
   [stepperDelegate clicked];
}

更新:

【问题讨论】:

  • cell.stepperView.delegate= self 在你的 cellForRow 中,就在你评论的地方
  • cell 无法访问 stepperView ??
  • 你的意思是什么,请解释一下,如果你的 stepperView 是你的单元格的属性,你可以毫无问题地做到这一点,并且你的 slimerView 有一个 id delegate
  • 请检查更新。
  • 你能发一下你的StepperProgressTableViewCell.h

标签: ios objective-c uitableview delegates


【解决方案1】:

您需要在 cell.h 中将您的 stteperView 声明为属性,您的属性在 .m 中声明并且是私有的,您需要在您的 .h 中声明

StepperProgressTableViewCell.h

@interface StepperProgressTableViewCell : UITableViewCell

@property (nonatomic) AYStepperView *stepperView;

@end

之后你可以在你的 cellForRow 中cell.stepperView.delegate= self

希望对你有帮助

【讨论】:

【解决方案2】:

在您的 StepperProgressTableViewCell.m 中隐藏 stepper 属性

 @interface StepperProgressTableViewCell ()

//@property (nonatomic) AYStepperView *stepperView;
@property (nonatomic) NSUInteger currentIndex;
@property (nonatomic) NSUInteger currentStep;
@property (nonatomic) UIView *containerView;

@end

在您的StepperProgressTableViewCell.h 中移动以下属性并检查

@property (nonatomic) AYStepperView *stepperView

然后将您的委托设置为 stepperView;

cell.stepperView.delegate = self

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-26
  • 2022-10-16
  • 1970-01-01
  • 2022-07-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多