繁体   English   中英

UITableViewCell中的UITableView委托和数据源

[英]UITableView delegate and datasource in UITableViewCell

我在UITableViewCell中有一个UITableView,我需要弄清楚如何设置子表视图数据源和委托。 目前,这是我所拥有的:

MainTableViewController.h

#import <UIKit/UIKit.h>

@interface MainTableViewController : UITableViewController <UITableViewDataSource, UITableViewDelegate>


@end

MainTableViewController.m

#import "MainTableViewController.h"
#import "TableViewCell.h"

@interface MainTableViewController ()

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation MainTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UITableView delegate functions

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

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

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

    cell.cellLabel.text = [NSString stringWithFormat:@"%ld", (long)indexPath.row];

    return cell;

}


@end

TableViewCell.h

#import <UIKit/UIKit.h>

@interface TableViewCell : UITableViewCell <UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UILabel *cellLabel;
@property (weak, nonatomic) IBOutlet UITableView *subTableView;

@end

TableViewCell.m

#import "TableViewCell.h"

@implementation TableViewCell

-(id)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        self.subTableView.delegate = self;
        self.subTableView.dataSource = self;
    }

    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

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

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

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

    cell.textLabel.text = @"test";

    return cell;
}

@end

因为无法ctrl +从子表视图拖动到TableViewCell类,所以我试图在初始化过程中以编程方式设置委托和数据源,但是它无法正常工作,我只是感到困惑。

我知道我可以设置数据源和委托以连接到第一个类,然后在每个委托函数中检查我正在处理的tableView,但是我尝试执行的操作具有这种特性。确实有效,我已经尝试过。

因此欢迎所有帮助

所以我想通了,好了我想出了一种方法。 在MainTableViewController.m的cellForRowAtIndexPath中,我简单地添加了:

[cell.subTableView setDelegate:cell];
[cell.subTableView setDatasource:cell];

一切都在努力

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM