将cell.section传递给CustomTableCell
我有一个名为AdminOrderViewController
的tableview,它有一个名为StepperProgressCell
。
这个customcell有一个名为AYStepperView
的自定义UIView。 在这个UIView中有一个button,我实现了一个委托,每当它被点击,我clicked
方法调用AdminOrderViewController
。
但是,我无法弄清楚如何将点击标题cell.section
传递给AYStepperView
?
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= self; return cell; } -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { AdminHeaderFooterView *sectionHeaderView = [self.adminTableView dequeueReusableHeaderFooterViewWithIdentifier:SectionHeaderViewIdentifier]; if (sectionHeaderView == nil) { sectionHeaderView = [[AdminHeaderFooterView alloc] initWithReuseIdentifier:SectionHeaderViewIdentifier]; } UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(selectHeaderAction:)]; [sectionHeaderView addGestureRecognizer:tapGesture]; return sectionHeaderView; } -(void) selectHeaderAction :(UITapGestureRecognizer*) gestureRecognizer { AdminHeaderFooterView* cell = (AdminHeaderFooterView*)gestureRecognizer.view; [self toggleSection:cell withSection: cell.section]; // how to pass clicked section to AYStepperView?? } -(void)clicked :(NSUInteger) currentSection { NSLog(@"Stepper clicked %lu", currentSection); }
StepperProgressTableViewCell.m
@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.h
@property (nonatomic) NSUInteger currentSection;
AYStepperView.m
@protocol AYStepperViewDelegate <NSObject> @required - (void)clicked :(NSUInteger) currentSection; @end - (void)buttonPressed:(UIButton *)sender { [stepperDelegate clicked : currentSection]; }
单元格不需要知道它在哪个行或部分; 您的表格视图控制器可以很容易find这个给定的单元格的引用。
您的视图控制器不应该将自己设置为步进视图的代表。 它应该是单元的代表。 单元格应该是步进视图的代表。 这有点复杂,但它保持了更好的分离关系,并使整个事情更清洁。
AYStepperView.h
@protocol AYStepperViewDelegate <NSObject> @required - (void)clicked; @end
AYStepperView.m
- (void)buttonPressed:(UIButton *)sender { [stepperDelegate clicked]; }
StepperProgressTableViewCell.h
@protocol StepperProgressTableViewCellDelegate <NSObject> @required - stepperChanged: (StepperProgressTableViewCell) cell;
StepperProgressTableViewCell.m
-(void)awakeFromNib { self.stepperView.delegate= self; } - (void)clicked { [self.delegate stepperChanged: self]; }
AdminOrderViewController.m
@interface AdminOrderViewController : UIViewController <StepperProgressTableViewCellDelegate> - (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.delegate= self; return cell; } -(void)stepperChanged:(StepperProgressTableViewCell)cell { NSIndexPath *indexPath = [self.tableView indexPathForCell:cell]; // Now do something with indexPath.section }
在cellForRowAtIndexPath
设置节到步进视图
- (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= self; cell.stepperView.currentSection = indexPath.section;//set section value return cell; }