【问题标题】:Child's dataSource methods not called未调用子级的数据源方法
【发布时间】:2015-02-10 10:12:19
【问题描述】:

我有一个名为 ObjectA 的父 UITableViewController 对象和一个派生的 ObjectB。

我的目标: 覆盖 cellForRowAtIndexPath: 方法来改变单元格的外观

ObjectA 方法被正确调用,所以:

  • numberOfRowsInSection:在父级调用,OK
  • numberOfSectionsInTableView:在父级调用,OK
  • cellForRowAtIndexPath:在父级中调用,错误

这就是我所做的:

/**
 * ObjectA
 */

@interface ObjectA : UITableViewController

@end

@implementation ObjectA

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

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

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    /* Layout by ObjectA rules */
}

@end

/**
 * ObjectB
 */

@interface ObjectB : ObjectA

@end

@implementation ObjectB

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    /* Layout by ObjectB rules */

    NOT CALLED
}

@end

编辑:

对象以编程方式分配,因此不存在与 IB 声明相关的问题。

EDIT2:

我的代码有问题...我编写了一个测试项目,一切正常...抱歉

/**
 * ObjectA
 */

@interface ObjectA : UITableViewController {
    NSArray *list;
}
-(void)setList: (NSArray *)l;
@end

@implementation ObjectA

-(void)setList: (NSArray *)l {
    list = l;
    [self.tableView reloadData];
}

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

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

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];

    if(cell == nil)
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];

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

@end

/**
 * ObjectB
 */

@interface ObjectB : ObjectA

@end

@implementation ObjectB

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];

    if(cell == nil)
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];

    cell.textLabel.text = @"child";
    return cell;
}

@end

/**
 * View controller
 */

@interface ViewController() {
    NSArray *list;
    ObjectA *obj;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    list = @[@"test1", @"test2", @"test3", @"test4", @"test5"];
    obj = [[ObjectB alloc] initWithStyle:UITableViewStylePlain];

    obj.view.frame = self.view.frame;
    [self.view addSubview:obj.view];

    [obj setList:list];
}

【问题讨论】:

  • 你说的错是什么意思?
  • 预期结果是:cellForRowAtIndexPath: call in CHILD, OK

标签: ios objective-c inheritance


【解决方案1】:

在 ObjectB 中覆盖:

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return [super numberOfSectionsInTableView:tableView];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [super tableView:tableView numberOfRowsInSection:section];
}

为我工作:

ObjectB *objA = [[ObjectB alloc] init];
objA.list = @[@"1"];
[self presentViewController:objA animated:YES completion:nil];

【讨论】:

    猜你喜欢
    • 2016-03-03
    • 1970-01-01
    • 2015-07-28
    • 1970-01-01
    • 2016-01-17
    • 2013-07-09
    • 1970-01-01
    • 1970-01-01
    • 2021-08-02
    相关资源
    最近更新 更多