当 tableview 位于 collectionview 单元格内时,不调用 TableViewDatasource 和委托

TableViewDatasource and delegate not called when tableview is inside collectionview cell

我在 collectionviewcell 中放置了一个 uitableview 并在 collectionviewcell 中编码如下 class.But 数据源和委托方法没有被调用可以帮助我解决这个问题

- (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;
}

发生这种情况是因为在 init 方法上尚未创建对象 "self"。您可以将 tableView dataSource 和 Delegate 放在另一个方法中。就像我一样。

@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;
}