TableViewDatasource and delegate not called when t

2019-09-09 22:13发布

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条回答
forever°为你锁心
2楼-- · 2019-09-09 22:49

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;
}
查看更多
登录 后发表回答