gpt4 book ai didi

ios - UITableview/UIView 委托(delegate)混淆

转载 作者:行者123 更新时间:2023-12-01 18:11:26 25 4
gpt4 key购买 nike

我正在制作自己的下拉菜单,因为我无法找到完全符合我需要的开源菜单。
我已将下拉菜单实现为 ​​UIView,并将其添加到被点击的按钮的 super View 中以显示它。

代码:

ViewController.m

#import "ViewController.h"
#import "MenuView.h"

@interface ViewController () <MenuViewDelegate>
@property (weak, nonatomic) IBOutlet UIView *fakeHeader;
@property (nonatomic, strong) MenuView *menuView;
@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

- (IBAction)btnTapped:(id)sender {
NSArray *array = @[@"Item 1", @"Item 2", @"Item 3", @"Item 4"];
NSArray *imgArray = nil;

if (self.menuView == nil) {
self.menuView = [[MenuView alloc] showDropDownWith:sender txtArr:array imgArr:imgArray direction:@"down" delegate:self];

self.menuView.delegate = self;
} else {
[self.menuView hideDropDown:sender];

self.menuView = nil;
}
}

- (void) menuDelegateMethod:(MenuView *)sender {
self.menuView = nil;
}

@end

MenuView.h

#import <UIKit/UIKit.h>

@class MenuView;

@protocol MenuViewDelegate
- (void)menuDelegateMethod:(MenuView *)sender;
@end

@interface MenuView : UIView
@property (nonatomic, retain) id <MenuViewDelegate> delegate;

- (id)showDropDownWith:(UIButton *)button txtArr:(NSArray *)txtArr imgArr:(NSArray *)imgArr direction:(NSString *)direction delegate:(id)delegate;
- (void)hideDropDown:(UIButton *)button;
@end

MenuView.m

#import "MenuView.h"
#import "QuartzCore/QuartzCore.h"

@interface MenuView () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *table;
@property (nonatomic, strong) UIButton *btnSender;
@property (nonatomic, retain) NSString *animationDirection;
@property (nonatomic, retain) NSArray *list;
@property (nonatomic, retain) NSArray *imageList;
@end

@implementation MenuView

- (id)showDropDownWith:(UIButton *)button txtArr:(NSArray *)txtArr imgArr:(NSArray *)imgArr direction:(NSString *)direction delegate:(id)delegate {
CGFloat width = [UIScreen mainScreen].bounds.size.width;
CGFloat origin = [UIScreen mainScreen].bounds.origin.x;
CGFloat realHeight = 40 * txtArr.count;

self.btnSender = button;
self.animationDirection = direction;
self.table = (UITableView *)[super init];

if (self) {
// Initialization code
CGRect btn = button.frame;
self.list = [NSArray arrayWithArray:txtArr];
self.imageList = [NSArray arrayWithArray:imgArr];

if ([direction isEqualToString:@"up"]) {
self.frame = CGRectMake(origin, (btn.origin.y - btn.size.height) , width, 0);

self.layer.shadowOffset = CGSizeMake(0, 1);
} else if ([direction isEqualToString:@"down"]) {
self.frame = CGRectMake(origin, (btn.origin.y + btn.size.height + 10), width, 0);

self.layer.shadowOffset = CGSizeMake(0, 1);
}

self.layer.masksToBounds = YES;
self.layer.shadowOpacity = 0.2;

self.table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, width, 0)];
self.table.delegate = delegate;
self.table.dataSource = self;
self.table.backgroundColor = [UIColor colorWithRed:0.239 green:0.239 blue:0.239 alpha:1];
self.table.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
self.table.separatorColor = [UIColor lightGrayColor];
self.table.backgroundColor = [UIColor whiteColor];
self.table.userInteractionEnabled = YES;

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];

if ([direction isEqualToString:@"up"]) {
self.frame = CGRectMake(origin, (btn.origin.y - realHeight), width, realHeight);
} else if([direction isEqualToString:@"down"]) {
self.frame = CGRectMake(origin, (btn.origin.y + btn.size.height + 10), width, realHeight);
}

self.table.frame = CGRectMake(0, 0, width, realHeight);
[UIView commitAnimations];
[button.superview addSubview:self];

self.table.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];

[self addSubview:self.table];
}

return self;
}

- (void)hideDropDown:(UIButton *)button {
CGRect btn = button.frame;
CGFloat width = [UIScreen mainScreen].bounds.size.width;
CGFloat origin = [UIScreen mainScreen].bounds.origin.x;

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];

if ([self.animationDirection isEqualToString:@"up"]) {
self.frame = CGRectMake(origin, btn.origin.y, width, 0);
} else if ([self.animationDirection isEqualToString:@"down"]) {
self.frame = CGRectMake(origin, (btn.origin.y + btn.size.height + 10), width, 0);
}

self.table.frame = CGRectMake(0, 0, width, 0);

[UIView commitAnimations];
}

#pragma mark - Table View DataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.list count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.textLabel.font = [UIFont systemFontOfSize:15];
cell.textLabel.textAlignment = NSTextAlignmentLeft;
}

cell.textLabel.text = [self.list objectAtIndex:indexPath.row];

cell.backgroundColor = [UIColor colorWithRed:48.0f/255.0f green:48.0f/255.0f blue:48.0f/255.0f alpha:1.0f];
cell.textLabel.textColor = [UIColor lightTextColor];

return cell;
}

#pragma mark - Table View Delegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 40;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[self hideDropDown:self.btnSender];

[self myDelegate];
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
// Remove seperator inset
if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
[cell setSeparatorInset:UIEdgeInsetsZero];
}

// Prevent the cell from inheriting the Table View's margin settings
if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
[cell setPreservesSuperviewLayoutMargins:NO];
}

// Explictly set your cell's layout margins
if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
[cell setLayoutMargins:UIEdgeInsetsZero];
}
}

#pragma mark - View Delegate
- (void)myDelegate {
[self.delegate menuDelegateMethod:self];
}

@end

完美显示,但从不调用 didSelect 方法。

我没有任何关于它会窃取触摸事件的观点。

似乎 UIViews 可能无法成为 UITableviewDelegates。如果这是真的,我不知道为什么,当我将调用 View Controller 设置为委托(delegate)时,它仍然无法 didSelect。

注意:我通过不使用更新的方法知道动画失礼。这是基于旧的示例代码。解决此问题后,我将更新动画。

问题:
  • UIViews 不能是 UITableView 委托(delegate)是真的吗?
  • 如果是这样,如何使调用 View Controller 成为驻留在 UIView 中的 TableView 的委托(delegate)?除了将其设置为 UITableViewDelegate 并在创建表时将调用 View Controller 分配为委托(delegate)的过程。
  • 我在设置它的方式中是否遗漏了一些东西,它窃取了单元格的点击,以便在 View 或 View Controller 中都不会调用 didSelect?

  • 谢谢您的帮助。

    最佳答案

    同意@Piotr 的菜单必须是表的委托(delegate),所以替换 self.table.delegate = delegate;self.table.delegate = self;在 MenuView.m 中。

    但此外,MenuView.m 永远不会调用它的委托(delegate),它应该在 tableview 中选择它时......

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self hideDropDown:self.btnSender];

    // remove this, since it does nothing
    //[self myDelegate];

    // replace it with
    [self.myDelegate menuDelegateMethod:self];
    }

    最后一行说,告诉菜单的委托(delegate)发生了什么事。

    另一个问题是菜单并没有真正告诉委托(delegate)人发生了什么。当然,委托(delegate)会对选择哪个项目感兴趣。考虑将协议(protocol)更改为:
    @protocol MenuViewDelegate
    - (void)menuView:(MenuView *)sender didSelectOptionAtIndex:(NSInteger)index;
    @end
    // calling it
    [self.myDelegate menuView:self didSelectOptionAtIndex:indexPath.row];

    另一种选择是将选定的字符串交还给委托(delegate)人。这可以在 indexPath.row 的 tableview 的数据源中找到。

    最后,最好不要保留您的委托(delegate),因为菜单的客户可能会保留它,从而导致保留周期。相反,声明委托(delegate):
    // notice "weak"
    @property (nonatomic, weak) id <MenuViewDelegate> delegate;

    关于ios - UITableview/UIView 委托(delegate)混淆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30465006/

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