【问题标题】:Programmatically created tableview delegate not working以编程方式创建的 tableview 委托不起作用
【发布时间】:2018-07-05 17:06:10
【问题描述】:

假设我在情节提要中创建了一个 tableview 并引用为“tableViewJobList”,我想在 tableViewJobList 的每个单元格中生成一个 tableview,所以我在 cellForRowAtIndexPath 中进行。

我的 .h 文件(tableViewJobList 委托和数据源在情节提要中设置为 viewcontroller)

@interface JobByAccountViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableViewJobList;
...
@end

我的 .m 文件

...

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    //realJoblist is my data array
    if (tableView == _tableViewJobList) {
        return [realJobList count];
    }
}

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView == _tableViewJobList) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
        if (!cell) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
        }

        //each cell has a tableview
        UITableView *subTableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
        subTableView.delegate = self;
        subTableView.dataSource = self;

        subTableView.frame = CGRectMake(0, 0, cell.bounds.size.width, cell.bounds.size.height);
        [cell addSubview:subTableView];

        return cell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView == _tableViewJobList) {
        NSLog(@"outer tableview");
    } else {
        NSLog(@"inner tableview");
    }
    ...
}

... 

在 heightForRowAtIndexPath 中,打印语句不应该在 else 块中触发,因为这些是我以编程方式创建的表格视图?

【问题讨论】:

  • tableViewJobList的dataSource和delegate在哪里设置?
  • @Dare 我在情节提要中做到了
  • 很多问题。 1. 细胞被重复使用。您将不断向每个单元格添加越来越多的表格视图。 2. 在每个单元格内创建零高度表视图。那些永远不会显示任何东西。为什么要打扰单元格内的表格视图?这是个坏主意。为什么不只使用一个主表视图并将数据放在不同的部分?
  • 这可能会有所帮助:stackoverflow.com/questions/17398058/…
  • “numberOfRowsInSection”的其他部分在哪里??

标签: ios objective-c uitableview delegates


【解决方案1】:

试试这个,,, 您只需替换您的表出口名称即可。

- (void)viewDidLoad {
    [super viewDidLoad];

        tableViewJobList.delegate = self;
            tableViewJobList.dataSource = self;

}


    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
        //realJoblist is my data array
        if (tableView == _tableViewJobList) {
            return [realJobList count];
        }
    }

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

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        if (tableView == _tableViewJobList) {
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
            if (!cell) {
                cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
            }

            //each cell has a tableview
            UITableView *subTableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
            tableViewJobList.delegate = self;
            tableViewJobList.dataSource = self;

            subTableView.frame = CGRectMake(0, 0, cell.bounds.size.width, cell.bounds.size.height);
            [cell addSubview:subTableView];

            return cell;
        }
    }

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
        if (tableView == _tableViewJobList) {
            NSLog(@"outer tableview");
        } else {
            NSLog(@"inner tableview");
        }
        ...
    }

    ... `enter code here`

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-20
    • 2020-07-01
    • 1970-01-01
    • 2017-08-14
    • 1970-01-01
    相关资源
    最近更新 更多