在下面的自定义表格单元格(StepperProgressTableViewCell
)中,我已将currentSection
硬编码为 2 进行测试,但是当它命中StepperProgressTableViewCell
awakeFromNib
方法时,它将显示 0 。
TableViewController.m
- (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.stepperTableCellDelegate = self;
cell.currentSection = 2;
return cell;
}
StepperProgressTableViewCell.m
@implementation StepperProgressTableViewCell
@synthesize stepperView, currentSection;
- (void)awakeFromNib {
[super awakeFromNib];
[self setUpViews];
// it always shows 0
NSLog(@"%lu", currentSection);
}
- (void)setUpViews {
self.stepperView = [[AYStepperView alloc]initWithFrame:CGRectMake(0, 0 , [[UIScreen mainScreen] bounds].size.width, kFormStepperViewHeight) titles:@[NSLocalizedString(@"Processing", nil), NSLocalizedString(@"Ready", nil), NSLocalizedString(@"Delivered", nil)]];
self.stepperView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
self.stepperView.userInteractionEnabled = YES;
self.stepperView.currentSection = currentSection
[self addSubview:self.stepperView];
}
AYStepperView.m
- (instancetype)initWithFrame:(CGRect)frame titles:(NSArray *)titles {
self = [super initWithFrame:frame];
if (!self) {
return nil;
}
// ultimate goal is to set AYStepperView's currentSection
// the following also prints 0
NSLog(@"%lu", _currentSection);
return self;
}
最佳答案
您需要添加一个方法来更改此属性,并在实现中将值传递给stepperView
StepperProgressTableViewCell.h
添加类似的方法
-(void)setupCurrentSection:(int)newCurrentSection;
StepperProgressTableViewCell.m
-(void)setupCurrentSection:(int)newCurrentSection{
self.currentSection = newCurrentSection
self.stepperView.currentSection = newCurrentSection
}
在您的cellForRow中,然后添加此行
[cell setupCurrentSection:2];
希望这可以帮助
关于ios - 通过自定义表格单元设置值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45517894/