iosobjective-ciphoneuitableviewios8.3

TableViewDatasource and delegate not called when tableview is inside collectionview cell


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

Solution

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