gpt4 book ai didi

ios - 将 cell.section 传递给自定义表格单元格

转载 作者:行者123 更新时间:2023-11-28 21:06:24 25 4
gpt4 key购买 nike

我有一个名为 AdminOrderViewController 的 TableView ,它有一个名为 StepperProgressCell 的自定义单元格。

这个 customcell 有一个名为 AYStepperView 的自定义 UIView。这个 UIView 中有一个按钮,我在它上面实现了一个委托(delegate),每当它被点击时,我的 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];
}

最佳答案

单元格不需要知道它在哪一行或哪一节;给定对单元格的引用,您的 TableView Controller 可以轻松找到它。

您的 View Controller 不应将自己设置为步进 View 的委托(delegate)。它应该是单元格的代表。该单元格应该是步进 View 的代表。这有点复杂,但它保持了更好的关注点分离并使整个事情更清晰。

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
}

关于ios - 将 cell.section 传递给自定义表格单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45499259/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com