gpt4 book ai didi

ios - 单独类中的 UITableViewDelegate 出错

转载 作者:行者123 更新时间:2023-11-29 02:58:59 25 4
gpt4 key购买 nike

我想将 UITableViewDataSourceUITableViewDelegate 分离到另一个类中,以减少和更改 UITableView 中的委托(delegate)

这是我的代码GroupTableController.h

@interface GroupTableController : NSObject <UITableViewDataSource, UITableViewDelegate>

{
OutlayGroupsCollection *outlayGroupsCollection;
Car *currentCar;
Period *currentPeriod;
}

-(void) setCar:(Car*) currentCar andPeriod:(Period*) currentPeriod;
-(int) total;
@end

GroupTableController.m

@implementation GroupTableController

-(void) setCar:(Car*) car andPeriod:(Period*) period
{
currentCar = car;
currentPeriod = period;
}

-(int) total
{
if(outlayGroupsCollection == nil){
outlayGroupsCollection = [OutlayGroupsCollection new];
}

NSMutableArray *list = [outlayGroupsCollection list:currentCar forPeriod:currentPeriod];
int result = 0;
for (OutlayGroup *group in list) {
result=result+group.sum;
}
return result;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

OutlayGroupCell *cell = (OutlayGroupCell*)[tableView dequeueReusableCellWithIdentifier:@"OutlayGroupCell"];

if (cell==nil) {
cell = [[OutlayGroupCell alloc] init];
}

OutlayGroup *group = [[outlayGroupsCollection list:currentCar forPeriod:currentPeriod ] objectAtIndex:indexPath.section];
cell.typeView.text = [OutlayType getName:[group type]];
cell.sumView.text = [NSString stringWithFormat:@"%d", [group sum]];

return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 1;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
if(outlayGroupsCollection==nil){
outlayGroupsCollection = [OutlayGroupsCollection new];
}
return [[outlayGroupsCollection list:currentCar forPeriod:currentPeriod ] count];
}


@end

我将其绑定(bind)到下一个:

GroupTableController *gtd = [[GroupTableController alloc] init];

mainTableView.delegate = gtd;
mainTableView.dataSource = gtd;
[(GroupTableController*)mainTableView.delegate setCar: currentCar andPeriod: currentPeriod];

但是,我得到了错误

[GroupTableController numberOfSectionsInTableView:]: message sent to deallocated instance 0x9e4d640

我做错了什么!?(我是 Objective C 的新手!)

最佳答案

问题是UITableViewdelegatedataSource属性类型是assign,也就是说他们不会为您保留 gtd

您需要确保在创建 Controller (gtd) 之后将其保留为属性。

关于ios - 单独类中的 UITableViewDelegate 出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23520662/

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