问题
I have placed a uitableview inside collectionviewcell and have coded as below in the collectionviewcell class.But the datasource and delegate methods are not being called can any help me out to fix the issue
- (id)initWithCoder:(NSCoder *)aDecoder {
if (self = [super initWithCoder:aDecoder]) {
arrayMenuAlerts=[NSMutableArray arrayWithObjects:@"Suri",@"John",@"Phillip",@"Sachin", nil];
[self.contentView addSubview:tableView];
tableView.dataSource=self;
tableView.delegate=self;
}
return self;
}
- (void)awakeFromNib {
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
tableView.dataSource=self;
tableView.delegate=self;
}
回答1:
This happens because on the init Method the object "self" is not created yet. you can put tableView dataSource and Delegate in another method. like i did.
@synthesize tableview;
NSMutableArray *arrayMenuAlerts;
- (void)drawRect:(CGRect)rect{
arrayMenuAlerts = [NSMutableArray arrayWithObjects:@"Suri",@"John",@"Phillip",@"Sachin", nil];
tableview.dataSource = self;
tableview.delegate = self;
[tableview reloadData];
}
- (id)initWithCoder:(NSCoder *)aDecoder {
return [super initWithCoder:aDecoder];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [arrayMenuAlerts count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
cell.textLabel.text = arrayMenuAlerts[indexPath.row];
return cell;
}
来源:https://stackoverflow.com/questions/32586716/tableviewdatasource-and-delegate-not-called-when-tableview-is-inside-collectionv