繁体   English   中英

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

[英]TableViewDatasource and delegate not called when tableview is inside collectionview cell

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

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

发生这种情况是因为尚未在初始化方法上创建对象“自身”。 您可以将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;
}

暂无
暂无

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

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