gpt4 book ai didi

ios - 自定义 HeaderFooterView 错误访问

转载 作者:行者123 更新时间:2023-11-28 19:30:50 26 4
gpt4 key购买 nike

在下面的代码中,viewForHeaderInSection 最后一行,我收到了 EXC_BAD ACCESS 错误。

ViewController.m

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
movies = [[NSArray alloc] initWithObjects:
(NSArray*)[[Section alloc ] init:@"Z" movieNames:@[@"M", @"S"] isExpanded:false],
(NSArray*)[[Section alloc ] init:@"M" movieNames:@[@"Y", @"A"] isExpanded:false],
(NSArray*)[[Section alloc ] init:@"H" movieNames:@[@"M", @"F"] isExpanded:false], nil
];
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
ExpandableHeaderFoorterView* headerView = [[ExpandableHeaderFoorterView alloc] customInit:((Section*)movies[section]).genre withSection:section withDelegate:self];
// BAD ACCESS
return headerView;
}

-(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.text = ((Section*)movies[indexPath.section]).movies[indexPath.row];
return cell;
}

ExpandableHeaderFoorterView.m

- (id)customInit:(NSString*)title withSection: (NSInteger) section withDelegate :(id<ExpandableHeaderFooterViewDelegate>)delegate
{
self.textLabel.text = title;
self.headerSection = section;
self.headerDelegate = delegate;
return self;
}

最佳答案

您对自定义初始化方法的实现完全错误。这不是您在 Objective-C 中编写初始化程序的方式。此外,由于它是一个初始化程序,因此名称应以 init... 开头。

由于您正在扩展 UITableViewHeaderFooterView,因此您需要调用正确的 super 初始化程序。

- (id)init:(NSString*)title withSection:(NSInteger)section withDelegate:(id<ExpandableHeaderFooterViewDelegate>)delegate {
self = [super initWithReuseIdentifier:@"SomeUsefulIdentifier"];
if (self) {
self.textLabel.text = title;
self.headerSection = section;
self.headerDelegate = delegate;
}

return self;
}

关于ios - 自定义 HeaderFooterView 错误访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44990741/

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