0
我有一個UITableViewCell內的UITableView,我需要弄清楚如何設置子表視圖數據源和委託。目前,這是我所:UITableView代表和數據源在UITableViewCell
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,但具有我想要的特性做它不會真的工作,我試過了。
因此,所有的幫助是值得歡迎
爲一個UITableViewCell指定的初始化器是initWithStyle:reuseIdentifier - 嘗試設置數據源和委託在這個方法中 – Paulw11
@ Paulw11initWIthStyle僅在以編程方式創建tableViewCell時調用,我在IB內創建它,因此不調用initWithStyle,initWithCoder爲 –
然後,您應該有什麼工作。你是否設置了斷點來驗證發生了什麼? – Paulw11