2

I am using a UIView as a popup in my app.

I have added a UITableviewin to this UIVIew. However, the content of the UITableView doesn't get populated. I have set the delegate and datasource from IB and also from the code as well.

None of theDelegate methods are being called.

Code as follows:

-(id)initWithFrame:(CGRect)frame{

    self = [super initWithFrame:frame];

self.tablev.delegate = self;

        self. tablev.dataSource = self;


        [self addSubview:self. tablev];

}

View .h class

@interface MyView : UIView <UITableViewDataSource,UITableViewDelegate>

UPDATE

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    MyView

 *cell = [tableView dequeueReusableCellWithIdentifier:@"c" forIndexPath:indexPath];

    if (cell == nil)

    {

        cell = [[MyView

 alloc] initWithStyle:UITableViewCellStyleSubtitle

                                             reuseIdentifier:@"c"];

    }

    cell.la.text=@"hiiiii";
Illep
  • 16,375
  • 46
  • 171
  • 302

3 Answers3

0

1) If you have created the UIView popup and Table View using Interface Builder, then just set the delegate and dataSource of the table view in xib and also in this case you need not to add the tableview to the view (like [self addSubview:tableView]), because it's already added via on he view while creating it in xib.

2) If you need to set any thing via code on the views which are created using xib than use

- (void) awakeFromNib { } method of the corresponding view

awakeFromNib is called after the xib is loaded and it's subviews could be populated/customized.

3) Implement the delegate and dataSource methods

Illep
  • 16,375
  • 46
  • 171
  • 302
Sanjay Mohnani
  • 5,947
  • 30
  • 46
0

I think -

1) Your view should be MyView created using xib

2) Add a table view on this view (in xib), and set delegate and data source

3) Crete a class for CustomCell (using which cells will be created)

4) Implement the delegate and dataSource methods in MyView class

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"c" forIndexPath:indexPath];

    if (cell == nil)
    {
        cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"c"];
    }

    cell.la.text=@"hiiiii";

    return cell;
}
Sanjay Mohnani
  • 5,947
  • 30
  • 46
  • That was exactly what i did. – Illep Mar 25 '15 at 08:50
  • you may not have initialized you xib file correctly in your view... http://stackoverflow.com/questions/5056219/uiview-and-initwithframe-and-a-nib-file-how-can-i-get-the-nib-file-loaded – LC 웃 Mar 25 '15 at 09:05
  • above in your code you have added the delegates and datasource on MyView as - @interface MyView : UIView and you are creating a instance of MyView – Sanjay Mohnani Mar 25 '15 at 09:16
  • basically you are adding the table view on a view and creating the cell from same view, this is confusing? – Sanjay Mohnani Mar 25 '15 at 09:17
0

I was doing something similar and faced the same problem. UITableViewDataSource methods were not being called in my case. In my case this happened because the array used for populating the table was weak, I made it strong and problem was solved for me. Hope this will also help you.