当tableview在collectionview单元格内时,不调用TableViewDatasource和委托 [英] TableViewDatasource and delegate not called when tableview is inside collectionview cell

查看:168
本文介绍了当tableview在collectionview单元格内时,不调用TableViewDatasource和委托的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在collectionviewcell中放置了一个uitableview,并在collectionviewcell类中进行了如下编码.但是未调用数据源和委托方法可以帮助我解决该问题

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

推荐答案

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

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

这篇关于当tableview在collectionview单元格内时,不调用TableViewDatasource和委托的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆