ios - 单独类中的 UITableViewDelegate 出错

标签 ios objective-c uitableview

我想将 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/

相关文章:

ios - 如何使用像素化更正 UIView 和 UILabel 文本缩放?

ios - cancelPreviousPerformRequestsWithTarget : isn't working for me

objective-c - IOS中的CALayer可以在z位置变换吗

java - 将Java程序绑定(bind)到Cocoa接口(interface)

iphone - TableView :sectionForSectionIndexTitle:atIndex: for table footer/header

javascript - jquery Pageinit 被多次调用

objective-c - 应用程序中的 iOS 内部代理

应用程序在前台运行时收到 iOS 推送通知

ios - segue 不能与 swift 一起工作

objective-c - 如何将 UIImage 添加到分组的 UITableViewCell,使其圆角?